Visual Basic Tips #31


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

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 the 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. HIDING THE MOUSE POINTER                 
        
In an earlier tip, we showed you how to change the mouse pointer.
Today, we're going to show you how to hide the mouse pointer 
using an API call. Fortunately, it's one of the more simple 
calls and the declaration is short. To begin, add the following 
declaration to a module: 

Declare Function ShowCursor Lib "user32" (ByVal bShow As Long)
 As Long 

To hide the mouse pointer, simply make the following call: 

boo = ShowCursor(False) 

(Be sure to declare boo as a Boolean data type.) To unhide the 
mouse pointer, set ShowCursor to True using the call 

boo = ShowCursor(True) 

It's important to position both calls appropriately. If you're 
not careful, you may hide the mouse pointer without giving the 
user or the application the opportunity to unhide it. If you'd 
like a simple example, try the procedure 

Function HideMouse() 
Dim boo As Boolean 
boo = ShowCursor(False) 
MsgBox "Click to get the mouse back", vbOKOnly 
boo = ShowCursor(True) 
End Function 

This procedure hides the mouse pointer and then displays the 
message Click To Get The Mouse Back. However, you can't click 
the button because there's no mouse pointer. Simply press 
Enter, and the procedure will continue as if you'd clicked the 
button and redisplay the mouse pointer.


*3. ADDING MONTHS TO A COMBO OR LIST BOX                 
        
A combo or list box can easily display the months of the year. 
Unfortunately, coding all 12 months is a bit tedious--it's also 
not necessary! The next time you need to display all 12 months 
in a list or combo box, use this simple procedure: 

Private Sub Form_Load() 
Dim i As Integer 
For i = 1 To 12 
lstMonths.AddItem Format("28/" & i & "/1999", "mmmm") 
Next I 
End Sub 

The For loop cycles through the integer representations for 
each month--1 through 12. Then, the Format() function uses that 
value to return the appropriate month by name.


*4. HOW LOW CAN YOU GO?   
 
How does a 9.9% FIXED APR Visa sound? Transfer balances from 
other high rate credit cards and save! Find out more: 
http://www.pcworld.com/r/tw/1%2C2061%2Ctw-nextcard5-e45%2C00.html


*5. MORE ON AUTOMATION                 
        
All applications aren't equal in the eyes of Automation. There 
are subtle differences between the ways the 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.


*6. DIFFERENT MOUSE POINTERS                 
        
Last month 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. 
We thought you might like to see 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


*7. UNDERSTANDING THE PRINTER OBJECT                 
        
You can print simple reports without a third-party 
report-generating application. Simply use the Printer object. 
But there's a trick to it: This object allows you to select a 
paper bin, and your bins will most likely hold different-sized 
paper. Let's suppose your upper bin holds letter size (8.5 x 11)
and the lower bin holds legal size (11 x 14). If you specify the
upper bin for letter size, you must also specify the paper size
in the form 

PrinterPaperBin.vbPRBNUpper 
PrinterPaperSize.vbPRPSLetter 

To specify the lower bin with legal size paper, you'd use 
the statements 

PrinterPaperBin.vbPRBNLower 
PrinterPaperSzie.vbPRPSLegal


*8. GOOD, FREE DOWNLOADS                 
        
If you're learning about VB and the Internet, you might try 
checking out some of the downloads from 

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

Once you're at the home page, click Downloads and then click 
Free Area. Here you'll find sample code, demos, and 
documentation on VB and Internet features, DHTML, and ASP.


*9. 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 and 
searching on the right words. If you have this problem too, try 
using the glossary. First, open the Help system and select the 
Index tab. Enter the word  

glossary 

You can 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 words in alphabetical order. 
Simply locate the most appropriate word for the subject you're 
searching and click it. You might have to click a few words, but 
you're bound to find what you're looking for.


*10. TESTING FOR A BLANK STRING VARIABLE                 
        
There's little doubt that empty variables can wreak havoc on your
code. Fortunately, there are several easy ways to test a variable
before using it. For instance, the expression 

sVariable = vbNullString 

will return True if sVariable equals "" or False if sVariable has
value. The vbNullString constant represents an empty string ("").
Similarly, the expression 

sVariable = "" 

will return True if sVariable is empty and False if sVariable 
contains a string. However, there's an even faster expression. 
Consider the simple expression 

booResponse = Len(sVariable) = 0 

If sVariable is blank, its length equals 0, and the expression 
returns True. When sVariable equals a data string, booResponse 
equals False.
