Visual Basic Tips #47


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

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

Proudly presents:
Visual Basic

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


*1. MORE ON OPTIONAL ARGUMENTS

In our previous tip, we talked about using the Optional keyword
when passing arguments. If you declare an argument as Optional,
then the function will work whether or not you pass it an 
argument. We only talked about one argument, though. Since a 
function can have any number of arguments, how do you specify 
both optional and required in the same function?

Simply specify the required arguments first in the form:

Function DoMyWork(argument1 As datatype, argument2 As datatype, 
Optional argument3 As datatype)

When calling this function, you will need to specify two 
arguments, but the function will accept three. In other words, 
the function will accept either of the following calls:

=DoMyWork(data1, data2) 
=DoMyWork(data1, data2, data3)

It's important that you specify required arguments first. Once 
Visual Basic encounters the Optional keyword, it assumes that 
all following arguments are optional. That means the function

Function DoMywork(Optional argument1 As datatype, argument2 As
datatype)

won't have the intended result. You might think argument1 is 
optional and argument2 is required, but you'd be wrong. In this 
function, both arguments are optional.


*2. AUTOMATION

All applications aren't equal in the eyes of Automation. There 
are subtle differences between the ways applications respond 
when called. Today, we'll discuss a few of those differences.

Automation will launch Access as an icon. You must change the 
Visible property to True to restore the main window. Changing 
that property to False will minimize the window. Setting the 
object variable to Nothing will terminate the instance of 
Access; you can use Quit as well.

Excel will launch as a hidden window; you must set the visible 
property to True to unhide that window. You must use Quit to 
terminate Excel; setting the object variable to Nothing won't 
do the trick.

Like Excel, PowerPoint launches as a hidden window, and setting
the Visible property to True will unhide that window. In 
addition, you must use Quit to terminate the application.

Word also launches as a hidden window with a Visible property 
of False, and you must use Quit to terminate the application.

Outlook launches as a hidden window, but you must use the 
Windows API to unhide the window. You must use the Quit method 
to terminate the application.


*3. DIFFERENT MOUSE POINTERS

In past tips, we showed you how to display the hourglass mouse 
pointer by using the statement

Screen.Mousepointer = vbHourglass

The Screen object isn't limited to the hourglass mouse pointer. 
Here, then, is a list of all the different mouse pointers you 
can display using the Screen object's Mousepointer property:

               *Pointer*--*Constant* 
                   Arrow--vbArrow 
                   Cross--vbCrosshair 
                  I Beam--vbIbeam 
                    Icon--vbIconPointer 
                    Size--vbSizePointer 
             Size NE, SW--vbSizeNESW 
               Size N, S--vbSizeNS 
             Size NW, SE--vbSizeNWSE 
               Size W, E--vbSizeWE 
                Up arrow--vbUpArrow 
               Hourglass--vbHourglass 
                 No drop--vbNoDrop 
     Arrow and Hourglass--vbArrowHourglass 
 Arrow and Question mark--vbArrowQuestion 
                Size all--vbSizeAll


*4. LEARN THE VALUE OF A VARIABLE--FAST

When you run code for debugging purposes, you can quickly learn 
the value of a variable in the Debug or Immediate window--or 
you can position the cursor over any variable (that's been 
evaluated) in the actual code, and Visual Basic will display 
that variable's value in a Tip control. This feature can be 
extremely helpful when a variable changes its value or if a 
procedure is returning erroneous data. You can set a breakpoint 
right after the statement that contains the variable so you can 
check the variable's value.


*5. FREE DOWNLOADS  
  
If you're new to Visual Basic and the Internet, VB-Bootcamp is a
great site to find a wide range of downloadable goodies. Once 
you're at the home page, click the Downloads link. You'll need 
to enter your email address to visit the free area. Here you'll 
find sample code, demos, and documentation on VB and Internet 
features, including DHTML and ASP.

VB-Bootcamp

http://www.vb-bootcamp.com/


*6. USING THE GLOSSARY

Do you have as much trouble finding information in the Help 
system as I do? Sometimes, no matter what I enter, I get 
nothing, but I know there's bound to be useful information--I'm 
just not asking the right questions or searching for the right 
words. If you have this problem too, try using the glossary. 
First, open the Help system and select the Index tab. Then, 
simply enter the word

glossary

You can then select the glossary at large, or you can go to a 
specific section of the glossary. Regardless of how you open the
glossary, Help displays a list of keywords in alphabetical 
order. Simply locate the most appropriate word for the subject 
you're searching and click it. You might have to click through 
a few words to find the appropriate information, but you're 
bound to find it sooner or later.


*7. FINDING AN OBJECT'S DEFAULT PROPERTY

You probably know that you can omit an object's default 
reference. In fact, we've discussed this referencing shortcut 
in previous tips. But you may be wondering how you learn an 
object's default property. There's an easy way to find out: 
Just open the Object Browser and select the object in the 
Classes list. The Members list will update accordingly, and the 
default property will display a small blue circle right above it.


*8. WHERE TO USE NOTHING  
  
You often see code that sets all the object and DAO (or ADO) 
variables to Nothing at the end of a procedure, using the syntax

objVariable = Nothing

However, perhaps the best place to reset a variable to Nothing 
is when the function is done with the variable--not at the end 
of the procedure. There's really no reason to wait until the 
end of the procedure to free up those resources. Instead, free 
them as soon as the variables are no longer needed.


*9. ACCOMMODATING DECIMAL VALUES

When a value doesn't display or consider a decimal component in 
a calculation--and you know it should--check that value's data 
type. The value must be a Single or Double data type to store a 
decimal component. This is a common mistake and it's easy to 
make. While it's obvious that the Integer and Long Integer data 
types both store only integers, we use them so often that it's 
easy to enter Integer or Long when the variable really should 
be a Single or Double. Another problem is that we don't always 
know during the design stage that a variable might need to 
accommodate a decimal value.

In either case, simply changing the data type should solve the 
problem. However, we suggest you make sure that the data type is
the actual problem before doing so--changing a data type can 
have far-reaching consequences.


*10. CHANGING DATA TYPES

Previously, we reminded you that any time you might need to 
store decimal values, you must remember to declare the field or 
variable's data type as Single or Double. If you forget, you 
can always change the data type later. However, we would like 
to offer one word of caution about doing so: If you change a 
data type after you've entered data, you risk losing that data, 
and you can't undo the damage.

Here's what happens: If an existing value is too large for the 
new data type, Access may truncate, round, or even delete that 
value. This caution should be applied any time you change a 
field property when data already exists.
