Visual Basic Tips #8


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

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

Proudly presents:
Visual Basic

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


*1. INSERT WHITESPACE                 
        
You can use whitespace to make your code more readable. The most
logical place for inserting whitespace (a blank line) is at the 
end of a task. Some developers enter blank lines only between 
sub and functions. Others enter a blank line before or after 
comments. Still others also separate each section of code with 
a blank line (by section, we mean task). 

Where to insert blank lines is up to you, but doing so can 
improve the readability of your code. Eventually, you'll be able
to find sections at a glance by skimming through the blank lines.


*2. QUICKLY VIEWING CONSTANTS                 
        
A constant is a type of variable--generally a numeric or string 
value that doesn't change. You can define your own constants by 
declaring the variable using the Const keyword. However, you'll 
probably use intrinsic (built-in) constants most often. 

Remembering all the constants is probably out of the question 
for most of us. The next best solution is to use the Object 
Browser. Press F2 to open the Object Browser and choose All 
Libraries (or the library that's specific to your application) 
from the Project/Library control. Then, select Constants in the 
Classes control to display a list of constants for the library 
in the Members control.


*3. ANOTHER TIMER PROCEDURE                 
        
In a previous tip, we talked about using the API's Sleep function 
to pause your application for short periods of time. If you want 
to avoid APIs, you can use this procedure instead: 

Function Waiting(intTimeOut As Integer) 
Dim lNow 
lNow = Timer 

Do While Timer - lNow < intTimeOut 
DoEvents 
Loop 

MsgBox "We're done" 
End Function 

Pass the amount of time you want to pause things, in seconds, to 
the procedure. For instance, open the Immediate window and enter 
the statement 

?Waiting(3) 

VB will seem to do nothing for three seconds and then display the 
message "We're done." That's all there is to it!


*4. DISPLAYING THE ACTIVE PRINTER                 
        
Displaying or changing printer information can mean lots of 
tedious code--depending on the chore at hand. However, if you 
can reference the Word Object Library, displaying the name of 
the active printer takes only one quick property--ActivePrinter. 
Of course, this solution isn't for every application, especially 
if you'll be distributing the application in question. Remember, 
not everyone has Office or Word installed on their system. 

If you can use our quick solution, be sure to add the Word Object 
Library by choosing References from the Project menu and 
selecting Microsoft Word 8.0 Object Library in the Available 
References control. Then, click OK to return to your application. 
At this point, you can add the statement 

Word.Application.ActivePrinter 

to almost any Click event. Or you can use it in a MsgBox s
tatement as follows: 

MsgBox Word.Application.ActivePrinter 

If you just need to check on the active printer, try the 
following statement in the Immediate window: 

?Word.Application.ActivePrinter 

Doing so is especially helpful when your system is networked to 
several printers.


*5. A MOVING COMMAND BUTTON                 
        
This tip is just for fun--we're going to show you a procedure 
that makes a command button move. Now, we don't really have any 
practical suggestions for how to put this technique to work, but 
we thought we'd pass it along just the same. 

To get started, add a command button to a form and name the 
button cmdMove. Then, enter the following sub as the button's 
Click event: 

Private Sub cmdMove_Click() 
Dim iStart As Integer, i As Integer 
iStart = cmdMove.Left 
For i = iStart To iStart + 500 
cmdMove.Left = i 
Refresh 
Next i 
End Sub 

Run the form by pressing F5 or clicking Start. Simply click the 
button and watch it creep to the right. You can easily adjust 
the movement by substituting the Top property for the Left 
property. If you do, the button will move down. You can also 
adjust the effect by changing the loop's Step argument--doing so 
will affect how much or how little the button moves with 
each click.


*6. PROTECTING GLOBAL VARIABLES                 
        
It's easy to make a variable available to your entire 
procedure--simply declare the variable as a public variable. 
You should do so with caution. Why? Because not only is your 
variable available to all procedures, it's also updateable by 
all procedures. When one procedure updates a public variable, 
that variable is updated for all the procedures--whether or 
not you meant to do so. So, if your strategy's not solid, one 
procedure could affect another in a negative way. 

An easy way to avoid this problem is to copy the public variable
to a local variable (at the procedure level). Doing so will 
protect the variable--at the local level now--from further 
updates. Any updates will affect only the public variable, not 
the local copy. 

This tip is for the odd times when you run into this 
situation--not for every public variable you write. Of course, 
the best solution is to avoid the public variable altogether if 
updating it in one procedure has a negative effect on 
another procedure.


*7. SORTING OUT THE SORT                 
        
Sorting numbers rarely holds any surprises. If you can count, you 
can predict the outcome of your sort. Unfortunately, sorting 
string characters isn't always so easy, as long as the values 
you're sorting are stored as numerical data types and not text. 
The general rule is that characters sort alphabetically, with 
uppercase taking precedence over lowercase; digits sort before 
alphabetic characters. This order is known as the 
collating sequence. 

If you're not familiar with this sequence, you should keep a 
copy of the sorted characters and refer to it as needed. To view 
the sequence, open a module and enter the following procedure: 

Function Collate() 
Dim intCount As Integer 
For intCount = 0 To 255 
Debug.Print Chr(intCount) 
Next intCount 
End Function 

Then, enter in the Immediate window the statement 

?Collate 

The Chr() codes 0 through 255 represent the characters in this 
sequence--in their sorted order.


*8. USING + TO COMBINE ICONS AND BUTTONS                 
        
The MsgBox() function has several arguments and constants. The 
function's type argument defines the buttons displayed in the 
resulting message box. This setup is a bit unusual, but it's 
easily maintained because we can combine constants using the + 
operator. As an example, let's suppose you wanted to display a 
Yes, No, Cancel button set. Furthermore, let's say you want the 
No button to be the default. You can set both attributes using 
constants in the function's type argument as follows: 

MsgBox "message text", vbYesNoCancel + vbDefaultButton2 

The first constant, vbYesNoCancel, specifies the Yes, No, Cancel 
button set; vbDefaultButton2 declares the second button in the 
set, No, as the default. 

You're not limited to two constants, either. The statement 

MsgBox "Do you want to delete this record?", vbYesNo + 
 vbDefaultButton2 + vbQuestion 

will display the Yes, No button set and make the No button the 
default. In addition, the message box will display a question 
mark icon.


*9. STORING API DECLARES                 
        
Many developers use APIs extensively, while others avoid them 
like the plague. There's no right or wrong to the decision, but 
regardless of your API persuasion, you'll probably need to rely 
on them occasionally. If you regularly include APIs in your 
applications, you should consider creating one module just for 
your API declarations. Doing so will make them easier to find 
and manage. Another benefit is that you won't have to repeat 
them if you use the same API in more than one module.


*10. FINDING CONSTANTS                 
        
In a previous tip, we showed you how to quickly find a list of 
constants in the Object Browser. However, if you need to 
pinpoint which constant belongs to which function, this list 
isn't so helpful. 

For the most part, constants are clumped together in constructs 
called Enums (short for Enumerations). Some Enums are explicitly 
named--the prefix Enum is attached to the construct's name--and 
therefore easy to find. Depending on the class libraries you've 
referenced, you may find a variety of prefixes. VB constants 
begin with the prefix Vb. 

To identify the available constants for a property or method, 
open the Object Browser and choose the function in the Classes 
list. Then, choose the appropriate method in the Methods list. 
The lower pane will display the arguments and, where appropriate,
list the Enum. At this point, all you need do is click the Enum 
in the lower pane, and VB will update the classes and members 
lists accordingly. The members list will contain all the 
constants, and selecting a constant in the members list will 
update the contents of the lower pane. Specifically, the pane 
will display the constant, its value equivalent, and the name of 
the host Enum.
