Visual Basic Tips #30


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

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

Proudly presents:
Visual Basic

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


*1. CHECKING FOR NUMBERS                 
        
If you're tempted to use Cint() to check for numbers, don't. It 
doesn't work that way. Cint() simply returns an integer, even if 
the number is a string. For instance, the procedure 

Function NumberTest1(number) As Integer 
NumberTest1 = CInt(number) 
End Function 

will return an integer, even if the number is declared as a 
string. If we pass the value 1.5, the procedure returns the 
integer 2; if we pass the string "1.5," the procedure returns the 
integer 2. Unfortunately, if you pass the procedure a non-numeric 
value, such as "a," the procedure will return a Type 
Mismatch error. 

When checking a value for numberness (okay, there's really no 
such word), IsNumeric is your best choice. This function returns 
a Boolean value--True if the value is a number, False if it 
isn't. The procedure 

Function NumberTest2(number) As Boolean 
NumberTest2 = IsNumeric(number) 
End Function 

can handle numeric or string values. If you pass the value 1.5, 
the procedure returns a True value. If you pass the string 
"1.5," the procedure returns a True value--both 1.5 and "1.5" 
are numbers, even though VB treats "1.5" as a string. However, 
if you pass the procedure the letter "a," the procedure will 
return the False value.


*2. CONTROL CONFUSION                 
        
Ever have trouble deciding whether you need check boxes or option
buttons? They may appear similar, but their behavior is very 
different and they aren't interchangeable. The two sets of 
controls act the same in that they offer a number of items. But
they differ in the way they process your choice. Once you 
understand their purpose, you should have no trouble deciding 
between the two. 

Check boxes allow you to flag an option as True or False, Yes or
No, and so on. You can check more than one check box. On the 
other hand, option buttons offer choices, and you can choose 
only one. Your choice will determine the value of the option 
group (group of buttons). 

If you need to offer several options and the user can choose any
number of them, use check boxes. If you need to limit the choice
to just one item, use an option group.


*3. STICKING WITH NATIVE CONTROLS                 
        
There are a ton of third-party controls on the market, and most 
are dependable and make short work of complicated tasks. However,
any time you include a third-party control in an application, you
take a risk if you plan to upgrade. That's because there's no way
to guarantee that the control will work in the next 
version--chances are it will not. If the company that wrote your 
control doesn't upgrade it, you're stuck between a rock and a 
hard place. Either you don't upgrade or you upgrade and revamp 
the application. Neither choice paints a pretty picture. 

Now, we can hear third-party representatives growling, and we 
don't mean to imply that you should never use a third-party 
control. We simply recommend that you consider all the 
possibilities and your options before going that route. Here are 
a few questions to ask yourself: 

- How much time will the control save me? 
- How long has the third-party company been around? 
- Does that company have a consistent history of upgrading
  controls? 

 
If you stand to save a lot of time, and if the company is well 
established and has been consistently reliable in the customer 
and product support arena, then your risk is low. If the 
application is a single-user application and you doubt the 
application will ever be upgraded, by all means save yourself as 
much time as possible.


*4. HIGH-PERFORMANCE DATABASES                 
        
No matter what the application's task, performance is always an
issue. If you're working with databases, there are a few things 
you can do to speed things up a bit: 
- Avoid opening recordsets as a table type--there are other
  options, such as snapshot. 
- If you're searching in SQL tables, use indexes. 
- Avoid including functions in SQL statements. 
- Rely on stored procedures as often as possible. 

Adding these guidelines to your design should help improve 
performance in any database application.


*5. NAVIGATING THE INTERFACE                 
        
Do you find grabbing the mouse to reposition the cursor a bit 
distracting? It can be very disruptive to your train of thought.
Fortunately, there are keyboard shortcuts for navigating the 
different Visual Basic windows. We've listed just a few: 

F7--Jump to the Code window 
F4--Jump to the Properties window 
Ctrl-R--Jump to the Project window 
Ctrl-G--Jump to the Immediate window 

The next time you need to move the mouse, try the keyboard.


*6. MORE SHORTCUTS                 
        
In our last tip, we showed you a few keyboard shortcuts for 
navigating the Visual Basic windows. There are also several 
shortcuts you can use while debugging (tracking) your code. 
We've listed a few below: 

F8--Step Into 
Shift-F8--Step Over 
Ctrl-Shift-F8--Step Out 
Ctrl-F8--Run to Cursor 
F5--Run 
Ctrl-Break--Break 
Shift-F9--Quick Watch 
F9--Toggle Breakpoint 
Ctrl-Shift-F9--Clear All Breakpoints


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

For i = 1 To 10  
...  
Next  

is a legitimate use of the Next statement. However, you will 
probably find the code  

For i = 1 To 10  
...  
Next i  

more readable. It's easy to see where that particular For loop 
ends. I know this doesn't seem too obvious in such a simple 
example. But if your code includes a complex loop with many 
lines, you'll find spotting the end of your loop a little easier 
if you include the variable in the Next statement.


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

For i = 1 To 10  
...  
Next  

is a legitimate use of the Next statement. However, you will 
probably find the code  

For i = 1 To 10  
...  
Next i  

more readable. It's easy to see where that particular For loop 
ends. I know this doesn't seem too obvious in such a simple 
example. But if your code includes a complex loop with many 
lines, you'll find spotting the end of your loop a little easier
if you include the variable in the Next statement.


*9. MORE ON FOR'S NEXT                 
        
In our previous tip, we told you that it's okay to omit the For 
loop's variable in the Next statement. However, including the 
variable makes the loop a little easier to read and certainly 
makes finding the end of that particular loop easier. 

If you'd like to make your code even more readable, give your 
loop variable a descriptive name instead of using the old i 
variable. The name should reveal the loop's purpose or origin. 
For instance, the variable iAddForCounter indicates that the 
loop is adding values.


*10. PASSING OPTIONAL ARGUMENTS                 
        
Passing arguments from one procedure to another is how functions 
talk to one another. Passing an argument is simple--you simply 
specify the argument(s) in the main function using the syntax 

Function DoMyWork(argument As datatype) 

Then, you pass the necessary data (via the argument variable) to 
the function using the syntax 

=DoMyWork(data) 

Once you've declared an function's argument, you must pass a 
value or your procedure will return an error. In other words, 
the call 

=DoMyWork() 

won't work. 

It stands to reason that we don't always know if an argument's 
going to exist--no data may be just as important to the 
procedure as a passed argument. When this is the case, you can 
declare an optional argument using the syntax 

Function DoMyWork(Optional argument As datatype) 

Visual Basic will execute this function with or without the 
passed argument. This means that either call 

=DoMyWork(data) 
=DoMyWork() 

will work.
