Visual Basic Tips #46


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

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

Proudly presents:
Visual Basic

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


*1. USING ROUND()

The Round() function was introduced in version 6.0 and, as you 
might expect, this function returns a numeric expression 
rounded to a specified number of decimal places. You use the 
function in the form

Round(nExpression, nDecimalPlaces)

where nExpression is the value (or result of an expression) 
that you want to round and nDecimalPlaces specifies the number 
of decimal places to which nExpression is rounded.


*2. DISPLAYING THE HOURGLASS MOUSE POINTER

As you know, processing can tie up any application for a while. 
When this happens, you can alert your users by displaying the 
hourglass mouse pointer. To do so, simply include the statement

Screen.Mousepointer = vbHourglass

in your code--right before the lengthy task begins. Your users 
are probably familiar with the hourglass icon and will know 
that they must stop trying to enter data or otherwise interact 
with the application. Of course, you'll want to let your users 
know when the task is complete so they can return to work. 
To do so, include a second statement:

Screen.Mousepointer = vbDefault

This returns the mouse pointer to the default after completion 
of the code that performs the time-consuming task.

Several mouse pointer constants are available. For more 
information, search Help for Mouse Pointer Constants.


*3. DECLARATION SHORTCUT

Most of the time, we declare variables explicitly by 
identifying the data type. For instance, to declare a variable 
as an Integer, you'd use the statement

Dim i As Integer

However, there is a shortcut method when declaring a String, 
Integer, Long, Single, Double, or Currency data type: You can 
use the data type's character. The Integer data type's 
character is %. That means the statement

Dim I%

accomplishes the same task as our previous statement. 
Other data type characters are

String--$ 
Integer--% 
Long--& 
Single--! 
Double--# 
Currency--@

For the most part, we don't recommend you use these characters. 
We just want you to be aware of them in case you run into them.


*4. ELIMINATING A LITTLE CODE

There are several ways you can optimize code by eliminating 
unnecessary statements. My favorite is to delete declaration 
statements for unnecessary integers. For instance, the 
following procedure is fairly common--it declares a variable, 
assigns a value to it, and then returns it:

Function NumberTest(number As String) As Boolean 
Dim boo As Boolean 
boo = IsNumeric(number) 
NumberTest = boo 
End Function

This procedure declares the variable boo as a Boolean data 
type, assigns the result of a function to boo, and then returns 
the value of boo as the function results. There's nothing wrong 
with this code--it works fine. But we can eliminate two lines. 
The next procedure accomplishes the same end:

Function NumberTest(number As String) As Boolean 
NumberTest = IsNumeric(number) 
End Function

The difference here is that we've cut out an unnecessary 
variable. The function is already declared as a Boolean data 
type, so we really don't need boo at all.

You'll find many developers who insist that such shortcuts are 
"bad" programming... and you might not want to eliminate boo 
from your procedure for several reasons. First, if you need to 
refer to boo more than once, you should retain the variable. 
Second, the variables make the procedure more readable. 
However, if your procedure is short and sweet, eliminating 
these few extra lines shouldn't be a problem.


*5. CONTROL CONFUSION

Ever have trouble deciding whether you need checkboxes 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.

Checkboxes allow you to flag an option as True or False, Yes or 
No, and so on. You can select more than one checkbox. On the 
other hand, option buttons offer choices, and you can click 
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 checkboxes. If you need to limit the 
choice to just one item, use an option group.


*6. 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--and 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 weigh 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.


*7. 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.


*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--especially if the code contains more than one For 
statement--you'll find it easier to locate the end of your loop 
if you include the variable in the Next statement.


*9. NAME THAT LOOP

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 it easier to find the end of that particular loop.

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 iForCounterAdd 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 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 a function's argument, you must pass a 
value or your procedure will return an error. In other words, 
the call

=DoMyWork()

won't work because Visual Basic expects an argument.

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 of the following 
calls will work:

=DoMyWork(data) 
=DoMyWork()
