Visual Basic Tips #1


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

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

Proudly presents:
Visual Basic

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


*1.   DISPLAYING A PICTURE ON THE STATUS BAR                 
        
Did you know that you can display a picture in the Status Bar? 
Simply right-click the Status Bar and select the Properties 
command. Then, click the Panels tab and add a new panel. Next, 
click the panel's Picture area, choose Browse, and go find the 
picture you want to display. It couldn't be simpler!


*2.   SELECTING TEXT IN A TEXTBOX                 
        
You may want a control to select the contents of a TextBox 
control automatically once VB gives that control focus. To do 
so, attach the following procedure to the control's Got 
Focus event: 

Private Sub Text1_GotFocus() 
   SendKeys "{Home}+{End}" 
End Sub 

When VB selects the control, the GotFocus event will select the 
control's text value.


*3.   ADD INFORMATION WITH COLOR                 
        
The more information you can give your users, the better. An 
easy way to convey information quickly is with color. For 
instance, if you want users to know a particular field is 
required--meaning they must enter data in that field before they 
can go to the next record--you can create a label control and 
enter the message "This is a required field, you must enter 
data." But that solution requires that your users stop and read 
that message, which can waste time if they must do so repeatedly. 

Instead, use the same color consistently throughout your 
application to send a signal to your users that they must enter 
data in that field to continue. Your users will learn and adjust 
quickly, and at a glance they will know that a field requires an 
entry without having to interrupt their work. On the other hand, 
if most of your fields are required, it might be just as 
effective to apply special coloring to the fields that 
aren't required. 

You can use color to denote all kinds of information. Just be 
sure to use color consistently and sparingly.


*4. WHY YOU SHOULD CARE ABOUT A VARIABLE'S DATA TYPE                 
        
If you don't declare a variable's data type, VB will do so for 
you and assign the Variant data type to your variable. Now, you 
may not care, or you may not think it matters. You're right, to 
a degree. However, assigning the most appropriate data type to 
your variable gives you control over that variable. What do we 
mean by control? 

First, a data type restricts a variable to certain types of data. 
If you're storing values to use in a later calculation, you 
certainly wouldn't want VB to accept a string value. In this 
case, you'd declare your variable as one of the many value data 
types such as Byte, Integer, Long Integer, Single, Double, and 
so on. If you do so, VB will return an error if you attempt to 
enter a string value instead of an appropriate number value. This 
quality extends to dates as well--if you declare a variable as a 
date, it won't accept anything other than a date. So, declaring 
the appropriate data type is one way to validate data. 

Second, try to assign the data type that meets the needs of your 
variable but uses the least amount of resources, because each 
data type requires a certain number of bytes, and of course, the 
fewer you expend, the more efficient your application. A Variant 
data type requires 16 bytes while an Integer data type requires 
only 2. If the Integer data type is adequate but you use the 
Variant data type, you've wasted 14 bytes. This may not seem like 
a big deal, but if your application is large and your resources 
are limited, these unnecessary expenditures can add up quickly 
and affect the performance of your application. 

Don't go overboard and be too stingy about your declarations. 
After all, sometimes more is better. For instance, if you're 
using a function to store a value between 0 and 255, you might 
want to use the Byte data type. However, be sure you won't ever 
need to store a negative value. Also, consider the way your 
function will use these Byte values. If the result of calculating 
these Byte values might return a value other than one that falls 
between 0 and 255, you can't use the Byte data type to store the 
results just because the Byte data type was appropriate for the 
numeric elements you used in the calculation.


*5. AVOIDING GoTo                 
        
VB's GoTo statement allows you to control the flow of your code. 
However, you'll want to use this statement as little as possible. 
In fact, GoTo is easily the black sheep of the VB statements, 
since some developers refuse to use the statement except when 
referring to an error handler. 

Now, we're not trying to pass judgment on those who use the GoTo 
statement. If you use it with no problems, then continue with 
what works. However, this statement can cause problems. First, 
it can be difficult to debug a module that jumps around a lot. 
Second, the statement requires no conditions; it simply jumps 
without looking first, so there's no control. 

If you're using GoTo a lot, you might want to review some of 
the conditional structures such as If...Then, Select Case, 
Do...Loop, and so on. Then, try to substitute some of your 
GoTo's with one of these more stable statements.


*6. REPEAT i IN NEXT LOOP                 
        
When using the For statement, you can omit specifying the loop's 
variable name in the Next statement. For instance, 

For i = 1 To 10 
  ... 
Next 

is a legitimate loop. However, the following code 

For I = 1 To 10 
  ... 
Next i 

is more obvious. It is easy to see where the For loop ends. This 
may not seem important in such a simple example, but in a 
complex loop with many lines of code, it can make spotting the 
end much easier. 

If you want the loop to be even more meaningful, use a 
descriptive name--instead of I--for the looping variable. Simply 
name the variable as you would any other--by describing its 
purpose or origin.


*7. LEAVING OLD CODE                 
        
It may be tempting to delete lines of code as you replace them. 
However, you should consider commenting out the old code instead. 
Doing so uses few resources and can prove useful. Suppose you 
find a bug in your code and replace it with code that works. You 
delete the buggy code. Later you discover that the original code 
was correct after all. If you'd simply commented out the original 
code, you could uncomment that section and go back to work. If 
you've deleted the code, you'll need to remember it and 
re-enter it. 

Another benefit of retaining old code is the historical record 
you create. Having a complete history of where your code has 
been and how it's been updated can cut down on confusion and 
lend better understanding to the overall design and purpose of 
your application.


*8. THE FOR...LOOP VARIABLE                 
        
The For Each...Next loop provides a convenient method for looping 
through each member of a collection or a normal array. Before the 
For Each...Next loop, the process was a bit more complex. Just 
remember this one detail--you must reference each element as an 
Object or Variant variable when working with collections. If 
you're looping through an array, however, you must use a Variant.


*9. IF...THEN VERSUS IIF()                 
        
If you routinely assign the result of an Iif() statement to a 
variable, you should consider replacing those statements with 
an If...Then construct. Why? First, the Iif() statement is a 
little harder to decipher because of its one line structure. 
Second, the If...Then construct is faster than the Iif() 
statement. That's right, an If...Then is more than twice as fast 
as an Iif() statement that performs the same task.


*10. FIXED-LENGTH VERSUS VARIABLE-LENGTH                 
        
We're often asked what the difference is between a fixed-length 
string and a variable-length string. The difference is just as 
the name suggests. A fixed-length is fixed in size. That means, 
it can handle only so many characters and no more. For instance, 
the following statement 

Dim strFixed As String * 10 

limits strFixed to 10 characters or fewer. On the other hand, 
the statement 

Dim strVariable As String 

allows strVariable to grow (or shrink) as necessary.
