Visual Basic Tips #10


----------------------------------------------

TipWorld - http://www.tipworld.com
The Internet's #1 Source for Computer Tips, News, and Gossip

Proudly presents:
Visual Basic

----------------------------------------------

*1. USING THE FILE OBJECT                 
        
Did you know you could work with files as objects? You can, but you 
may need to reference the SCRRUN.DLL library (Windows\System) first. 
If you're running Windows 98 or NT with the Option Pack, or if you've 
installed VBA6, the library should already be installed. If you're 
working with VBA5 or Windows 95, you need to install the dll. If you 
can't find it on your system, you can download the file from 

http://msdn.Microsoft.com/scripting/ 

Once you know you have the dll on your system, create a reference to 
it by choosing References from the Project menu and choosing Microsoft 
Scripting Runtime. 

If you use the Scripting Runtime on a distributed product, make sure 
each system that runs your program has the dll installed.


*2. NEW RELEASE OF SCRIPT DEBUGGER                 
        
In our last tip, you learned how to install the Scripting Runtime so 
you can work with files as objects. If you're using Script Debugger, 
you should download the new release 1.0a, which repairs some known 
issues. Even if you're running Windows NT or Windows 2000 with 
Internet Explorer 5, you should still download version 1.0a. You can 
download both versions from 

http://www.pcworld.com/r/tw/1%2C2061%2Ctw-vb12-4%2C00.html


*3. A FEW CODE LIMITATIONS                 
        
VB is a very generous development tool. For the most part, few 
developers will ever tip the scales on any of the VB limitations. We 
mentioned in an earlier tip that a form could have as many as 254 
names, but no more than 254. Almost all controls and objects have 
limitations. Even modules have limitations, although we doubt many 
developers ever cause VB to tilt in this particular area. For the 
record, module limitations are as follows:  
  - A module can contain only 65,534 lines of code.  
- A single line of code can consist of no more than 1,023 bytes.  
- You can insert only 256 blank spaces before a line of text.  
- Each complete line of code can contain no more than 25 
line-continuation characters.


*4. HOW FAST DOES YOUR APPLICATION LOAD?                 
        
Let's face it: Appearances count, and if your application appears to 
load fast, it will make a great first impression. One way to improve 
load time is to trim down your startup form. Here are a few guidelines 
you can follow to streamline your startup forms and help your 
application load quickly:  
  - Use as little code in your startup form as possible.  
- Store all the code that your startup form requires in that form's 
module.  
(Don't call function procedures from a standard module.)  
- ActiveX controls require more time to load, so avoid using ActiveX 
controls in your startup form.  

  
If you follow these simple guidelines, your startup form should load 
quickly. Of course, other objects may still be loading, but perception 
is the key in this discussion. The faster that first form loads, the 
faster the user perceives he or she is getting to work.


*5. ACTIVEX LEGALITIES                 
        
You may not need to purchase an ActiveX control to have it. You see, 
any application that uses an ActiveX control also registers that 
control on your system. Once the control is registered, you're free to 
use it, even though you didn't acquire it by purchasing the control 
outright. You'll find these additional controls on your Toolbox--just 
click the More Controls tool to see what's available on your system. 

Although you can use these ActiveX controls in your own applications, 
you can't distribute them. If you decide you want to distribute one of 
these controls, you'll need to acquire a license to do so.


*6. ACTIVEX HELP                 
        
In our last tip, we told you it's fine to use your registered ActiveX 
controls in your own applications, even if you didn't pay for them 
outright. Unfortunately, unless you have the documentation and Help 
files that you get when you purchase the control, taking advantage of 
the control may not be an easy task. You'll need to know about the 
control's properties and methods to use it. 

If you're lucky, the Help files may be installed. To find out, insert 
the control in a form and then open that control's property sheet. 
Select any property and press F1. If the files are there, pressing F1 
should access them and, of course, you're free to use them.


*7. WORKING WITH EXTERNAL DATA                 
        
Many software applications incorporate the COM object model so you can 
use their objects in your applications. Here's how to see what foreign 
objects are available. Once you've installed an application, check 
your references by choosing References from the Project menu. Check 
the Available References list box for the name of the application you 
want to borrow from. If it's listed, check it and click OK. Once 
you've referenced the application, open the Object Browser to see 
which of the application's objects are available to you.


*8. MULTIPLE LINES IN TEXTBOX                 
        
Most VB users will already know this short tip. However, new users who 
are familiar with VBA and the Office controls may be confused by the 
VB TextBox control. Both the Office and the VB TextBox control can 
display multiple lines of text. Using the Office control, you must 
press Ctrl-Enter at the end of each line of text to position the 
cursor at the beginning of a new line. You don't have to do this using 
the VB control. If you want the text to wrap in a VB TextBox control, 
simply set the control's MultiLine property to True. 

If you want to force a new line in the VB TextBox control, just press 
Enter.


*9. LOOK MA! NO ARRAY!                 
        
In earlier versions of VB, you added controls programmatically using 
control arrays. This meant the form had to have at least one control 
for you to create and add others at runtime. If you're using VB 6.0, 
you're no longer limited by this requirement. You can add a control at 
runtime using the Controls collection Add method. The code below is a 
simple example of how to add a command button to a form using 6.0. 

Option Explicit 
Private WithEvents cmdButton As CommandButton 

Private Sub Form_Load() 
Set cmdButton = Controls.Add("VB.CommandButton", "Button") 
With cmdButton 
    .Visible = True 
    .Width = 3000 
    .Caption = "I'm the first." 
End With 
End Sub 

Sub cmdButton_Click() 
cmdButton.Caption = "Look Ma! No array!" 
End Sub 

When you run the form, VB will display a command button with the 
caption "I'm the first." in the upper-left corner of the form. When 
you click the command button, the caption will change to "Look Ma! No 
array!" Use the .Top and .Left properties to specify an exact position 
for the control.


*10. POSITIONING YOUR FORMS THE EASY WAY                 
        
So much of what we do in VB is drag and drop--wouldn't it be nice if 
you could determine your form's position on screen with a simple 
drag-and-drop task? Well, you can if you open the Form Layout window 
in the IDE. To do so, simply choose Form Layout Window from the View 
menu. In response, VB will open the Form Layout window in the 
bottom-right corner of your screen. This window displays a simple 
monitor graphic and a representation of the active form in its current 
position. Set the form's position at runtime by simply dragging the 
form image around the Form Layout window. 

If you right-click the window, VB will display a shortcut menu. From 
this menu, you can easily set the form's startup position: 
 - Manual: Allows you to move the forms around the Form Layout window 
manually. 
- Center Owner: Positions the form in the center of the monitor with 
the host or owner window underneath it. 
- Center Screen: Positions the form in the center of the monitor. 
- Windows Default: Positions the form in the top-left corner of the 
monitor.