Visual Basic Tips #49


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

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, this procedure 
will return an integer, even if the number is declared as 
a string:

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

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. Accordingly, this procedure can handle numeric or 
string values:

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

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. NAVIGATING THE INTERFACE

Do you find it distracting when you have to take your hands off 
the keyboard and use the mouse for most standard tasks? It can 
be very disruptive to your train of thought. Fortunately, there 
are keyboard shortcuts for navigating the different Visual Basic 
windows. Here are a few of the most useful:

- 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
 
Now, when you need to hop from one window to another, there's 
no need to take your hands off the keyboard. 


*3. DEBUGGING SHORTCUTS

In the previous tip, we showed you a few keyboard shortcuts for 
navigating the Visual Basic windows. Now, here's a handful of 
shortcuts you can use while debugging (tracking) your code:

- F8--Step Into (keep pressing to step through each 
  line of code) 
- 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


*4. HIDING THE MOUSE POINTER

In a previous tip, we showed you how to change the mouse 
pointer to an alternate icon. 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 standard module 
(as a general rule, you can't use API's in object modules):

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

Now, 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 
aren't careful, you may hide the mouse pointer without giving 
the user or the application the opportunity to unhide it. For 
example, check out this 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! (In this case, 
since there's only one command button, you can press Enter in 
lieu of clicking the button and the procedure will redisplay 
the mouse pointer.)


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

Be sure the control's Sorted property is set to False, or the 
months will appear in alphabetical order.


*6. CREATING AN INSTANCE OF AN APPLICATION

If you've installed Office on your system, you can access all 
the application models through Visual Basic. The first thing 
you have to do is to set a reference to the model you want to 
run from inside Visual Basic. To do this, first choose 
References from the Project menu. In the resulting dialog box, 
locate and set the appropriate library reference and 
then click OK.

Once the reference is set, you use code to create an instance 
of the application and assign it to an object variable. There 
are two ways to do this. The first way is to use this code:

Dim objApp As New Excel.Application 
ObjApp.Visible = True

The declaration reserves memory for a new instance of Excel, 
but it doesn't create one. The second line creates a new 
instance of Excel and makes it visible.

The other method uses the New keyword in a Set statement:

Dim objApp As Excel.Application 
Set objApp = New Excel.Application

Keep in mind that when the applications launch, some will be 
visible by default and some won't.


*7. QUITTING AN APPLICATION

In the previous tip, we showed you how to initiate a new 
instance of an Office application from Visual Basic. Of course, 
opening another application consumes a lot of memory--which is 
why you should also follow any Automation task with a Quit 
method in the form:

ObjApp.Application.Quit

When you do this, you free up the memory previously used by 
the application. Don't forget to also set the object 
variable to Nothing.


*8. UNDERSTANDING THE PRINTER OBJECT

Using the Printer object, you can print simple reports without 
a third-party report-generating application--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


*9. 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 another way to determine 
the same result, since we're not checking for nulls, but a 
blank string. Consider the simple expression:

booResponse = Len(sVariable) = 0

If sVariable equals a zero-length string ("") or is blank, the 
Len() function returns 0 and the expression returns True. When 
sVariable equals a data string, booResponse equals False.


*10. PADDING NUMERICAL STRINGS WITH ZEROS

Adding leading zeros to a numerical string is a fairly common 
task and can be accomplished with formats. However, the 
procedure shown here is more flexible than a format, since you 
can change the number of zeros at any time:

Function ZeroPad(pad As String, places As Integer) 
While places > Len(pad) 
  pad = "0" & pad 
Wend 
ZeroPad = pad 
End Function

This code will pass the string you want to pad and the number 
of places you want the finished string to contain. Then, as 
long as the number of places is larger than the string, the 
procedure adds a zero. For instance, if you want to pad the 
value 2 with enough zeros to make the final string three 
characters, you'd pass this function the values 2 and 3.

The procedure will cycle through the While loop twice, each 
time adding a 0 character to the value 2. The final value 
will be 002.

You could enhance the procedure by adding a third argument to 
pass the padding character instead of hard-coding it. (In our 
example, the pad character is 0.)

As I mentioned, there are a number of ways to pad a string, 
and this is just one of them.
