Visual Basic Tips #13


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

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

Proudly presents:
Visual Basic

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

*1. UNDERSTANDING RND                 
        
In our previous tip, we used the Rnd function to return random values. 
If you don't know what to expect, the Rnd function can return a few 
surprises. You see, the function's argument changes the seed value, 
and the seed value determines where VB begins to generate random 
values. The Rnd function accepts just one argument, in the form 

Rnd(value) 

Here are a few rules you should know about value if you want to use 
Rnd: 
 - If value is greater than zero (or not supplied), Rnd returns the 
next random number in the sequence. 
- If value is less than zero, Rnd returns the same number. 
- When value equals zero, Rnd returns the most recently generated 
number.


*2. LIST BOX ALTERNATIVES                 
        
It's fairly common to store a record's identification value and a 
descriptive text field. For instance, if the list box displays 
employee names, the list box probably contains the employee names and 
the employee identification values. The list box displays the names 
because they're easier to recognize and work with. But the list box 
actually stores the identification value of the selected name and not 
the name. If you're using the standard ListBox, you might want to 
consider the Data ListBox instead. 

The Data ListBox provides a simplified method to accomplish the same 
thing. Simply set the DataField property to the descriptive text field 
(the employee name). Then, set the BoundColumn property to the 
record's identification value field (the employee's identification 
number). When you select an employee name from the control, the 
BoundText property will equal the record's identification value for 
that employee.


*3. AVAILABLE FONT SIZES                 
        
The Font Size tool on most Formatting toolbars lists sizes 8 to 72. 
Officially, you can specify a font size of 1 to 127 using the FontSize 
property. However, just because VB will allow you to specify a 
specific font size doesn't mean your printer can print it. When 
working with unusual font sizes, you should test the results to make 
sure your printer and the current font can accommodate that size.


*4. RESIZING AN ARRAY                 
        
VB allows you to resize an array. By resize, we mean you can change 
the number of elements the array stores. For instance, the statement 

Dim iMyArray(3) As Integer 

declares an integer array with three elements. If you should need to 
change the number of elements in an existing array, you should use the 
Redim statement in the form 

Redim arrayname(x) As datatype 

For instance, if we wanted to resize iMyArray to handle 10 elements, 
we'd use the statement 

Redim iMyArray(10) As Integer 

When you resize an array, the elements lose their value.


*5. MORE ON RESIZING AN ARRAY                 
        
In our previous tip, we showed you how to use the Redim statement to 
resize an array. We also mentioned that when you resize an array, the 
elements lose their values. Fortunately, you can retain the element 
values using the Preserve keyword in the form 

Redim Preserve arrayname(x) As datatype 

If you use the Preserve keyword in your Redim statement, VB will 
retain the value of each existing element in your array.


*6. A FASTER LOOP                 
        
All the loop statements need a way to know when to stop. The 
For...Next uses a value and Do...While uses a condition. You can 
specify a specific value, or you can use an expression. A good way to 
speed up your loop is to use variables instead of expressions as your 
loop's stop value. For instance, the following For loop uses the 
number of forms as its stop value: 

For lCounter = 0 To Forms.Count - 1 

Next lCounter 

Unfortunately, VB must evaluate the Count property before executing 
each loop, which will slow things down a bit. 

A faster alternative is to assign the result of the Count property to 
a variable and then use the variable as the loop's stop value as shown 
below: 

lCount = Forms.Count - 1 
For lCounter = 0 To lCount 

Next lCounter 

Now, VB evaluates the Count property only once. The result is a faster 
loop. (If your loop deletes or adds forms, you may need to reevaluate 
the Count property with each loop.)


*7. LEADING ZEROS                 
        
There are several solutions for adding leading zeros to a value, and 
most of them are more convoluted than they need to be. One of the 
simplest methods for adding leading zeros that we've found isn't all 
that intuitive, but it's simple and it works. You see, we'll be using 
the Right function to add leading zeros. In a nutshell, you add the 
value to a 10-based number that's one place larger than the number of 
characters you need for each entry. For instance, if you want all 
values to have five characters, using leading zeros to fill in as 
needed, you'd use the number 100000--that's one place more than five. 
The function 

Right(value + 100000, 5) 

will return five characters from the result of adding value to 
100000--including leading zeros. If value equals 30, the function will 
return 00030; a value of 4321 will return 04321, and so on.


*8. ANNOYING HELP                 
        
An annoying feature of VB 6.0 (or Visual Studio 6.0) is the online 
Help. Every time I need help, I have to insert one of my MSDN Library 
CDs. Doing so isn't a big deal, but it's a disruption I can do 
without. If it bothers you too, you can install the help files 
directly to your hard disk. To do so, rerun the install again, except 
this time choose the Custom option. At this point, simply select the 
Help files and continue. The next time you need help, you won't have 
to reach for your CDs. (Keep in mind that these files require about 12 
MB.)


*9. WHERE IS THAT WIZARD                 
        
You know that VB6 comes with a Toolbar Wizard, but you can't find it, 
right? You thought you'd find it in the Add-Ins Manager dialog box, 
but it's not there. That's because you have to install the Application 
Wizard first, because the Application Wizard contains the Toolbar 
Wizard. Once you've installed the Application Wizard, you can access 
the Toolbar Wizard from the Add-Ins menu.


*10. SELECT CASE WITHOUT CASE ELSE                 
        
The Select Case statement allows you to run an expression or condition 
by any number of different possibilities and assign a unique action 
for each using the form 

Select Case expression 
    Case x 
         x action 
    Case y 
         y action 
    Case z 
         z action 
    Case Else 
         else action 
End Select 

The Case Else action acts as a net for expression when it doesn't 
equal x, y, or z. The problem is, lots of folks don't bother to use 
it. After all, if you've provided a Case for all the possibilities, 
isn't it unnecessary? You might think so, but unexpected things do 
happen, and using the Case Else statement will help you catch 
unplanned errors. Simply add a Case Else action that alerts the user 
that expression doesn't fall within the expected parameters.
