Visual Basic Tips #2


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

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

Proudly presents:
Visual Basic

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


*1.   DECLARING DLL FUNCTIONS                 
        
If you're new to VB, you may also be new to working with DLLs. 
There are three main types of API libraries: GDI, User, and 
Kernel. GDI functions are related to line, text, and bitmap 
output. User functions cover messaging, dialog boxes, and 
controls. The last library, Kernel, applies to system-related 
information such as memory allocation. Each library contains 
many functions. 

Once you've found an appropriate function in one of the 
libraries, you must declare that function. In doing so, you'll 
need to know the name of the function, the number of arguments 
the function expects and their data types, and how the function 
will return the result and its data type. The general syntax for 
declaring an API function is 

Declare Function functionname Lib "libraryname" [Alias aliasname]
[(argument list)] As resultdatatype 

It's important to note that 32-bit calls are case-sensitive.


*2. SHORTCUT KEYS FOR THE IMMEDIATE WINDOW                 
        
There are several shortcut keys you can use in the Immediate 
window. Some, such as pressing the Enter key, you already know 
about--pressing Enter runs a line of code. In addition, Ctrl-C 
copies selected text to the Clipboard and Ctrl-V pastes the 
contents of the Clipboard into the Immediate window. You can use 
Ctrl-X to delete the selected text (to the Clipboard). More 
shortcut keys you may not be aware of include the following list:

- Ctrl-L displays the Call Stack dialog box. 
- Ctrl-Enter inserts a carriage return. 
- Ctrl-Home moves the cursor to the top of the window. 
- F2 displays the Object Browser. 
- F5 continues to run an application. 
- Shift-F5 restarts an application. 
- Alt-F5 runs the error handler code or returns the error. 
- F6 switches between the Immediate and Watch windows. 
- F8 initiates Single Step mode (executes code one line
  at a time ). 
- Shift-F10 displays the shortcut menu.


*13. WORKING AROUND VERSION NAME CONFLICTS                 
        
You can't use restricted keywords as the name of a form or 
control. Well, you can, but it isn't advisable. For those rare 
occasions when you do, you must enclose the name in a set of 
square brackets for VB to accept the name. 

Although we don't recommend you use this workaround in your 
normal coding, this tip does have its place in your work. 
Sometimes you can find that an accepted name in an earlier 
version is in conflict with a newer version (when upgrading) 
because that name is now a reserved word. When that happens, 
simply enclose the name in square brackets and the new version 
will accept and use the name.


*4. WORKING WITH TRUE/FALSE DATA                 
        
If you're new to VB, you might not realize that you can assign a 
number or even a string to a Boolean variable. Let's take a look 
at a quick and easy example. 
 
Private Sub Form_Click() 
Dim bytOne As Byte 
Dim intTwo As Integer 
 
Print bytOne = True 
Print intTwo = True 
Print bytOne = intTwo 
 
End Sub 
 
In this case, the True Boolean value is assigned to both a Byte 
and Integer variable, which is quite legal. 

Furthermore, you might expect this short exercise to print True, 
True, and True, since if bytOne equals True and intTwo equals 
True, then bytOne must equal intTwo, right? Well, they don't. 
That's because bytOne and intTwo are different data types, so 
they aren't equal in the eyes of VB, even if you assign the 
same value to both.


*5. USING ROUND()                 
        
The Round() function is new to version 6.0 and, as you might 
expect, this new function returns a numeric expression rounded 
to a specified number of decimal places. You'll 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 nExpression is rounded to.


*6. EXPLICIT RETURNS                 
        
As a rule, you'll want to explicitly assign a value for your 
functions to return. If you don't, VB will return the default 
value for the function's type: 
 
Variant functions return Empty 
Integer functions return 0 
Boolean functions return False 
 
You can use this behavior to your advantage, but if you're 
unaware of it, you might get unexpected results down the line.


*7. CENTERING A FORM                 
        
If you want to display a form in the center of your screen, add 
the following procedure to your form's Load procedure. 
 
Private Sub Form_Load() 
Left = (Screen.Width - Width) \ 2 
Top = (Screen.Height - Height) \ 2 
End Sub 
 
This procedure simply subtracts the width and height of the form 
from the width and height of the screen, and then divides that 
result by 2. 

If you're using version 5.0 or 6.0, simply set the forms' 
StartUpPosition property to CenterScreen or CenterOwner.


*8. WORKING WITH THE OBJECT BROWSER                 
        
By default, the Object Browser displays properties, methods, and 
events in alphabetical order. Sometimes this isn't 
convenient--especially if you're looking for something by type 
rather than by name. Fortunately, you can group both the Class 
and the Members lists by type. Simply right-click the 
appropriate window and choose Group Members. To return the 
grouping option to alphabetical, repeat this process and then 
deselect Group Members.


*8. SPEAKING THE ERROR CODE LANGUAGE                 
        
We all--well, nearly all--receive error messages when coding, but 
few of us have the entire list of error codes memorized. A quick 
way to learn more about an error is to run the statement 
 
?Error(Err) 
 
in the Immediate window after you encounter the error. 

The Err object contains information that identifies the most 
recent error, and the Err statement will provide that information. 
However, you must use this simple technique right after the error 
occurs, because the next error will overwrite the existing 
information with the new error's information. If this happens and 
you know the prior error's code number, you can run the statement 
 
?Error(codevalue) 
 
in the Immediate window.


*9. SPECIFYING DEFAULTS WITH INPUTBOX                 
        
You probably use the InputBox function to solicit users for data. 
However, did you realize that you can specify a default value? 
When possible, you'll want to do so, for several reasons:  

1. You'll save your users a little data-entry time  
2. You'll cut down on data-entry errors  
3. You'll avoid run-time errors that might occur due to missing
   data  

To specify a default value, be sure to take advantage of the 
function's third argument--the Default argument--in the form  
  
InputBox("prompt","title",default,xPos, 
yPos,HelpFile,Context As String)  
  
For instance, the following statement would offer an input box 
with a default value of "I'll take the really big one":  
  
InputBox("Please enter the jewel of your choice", "Gems To Go" ,
 "I'll take the really big one")  
  
If the default is a value, omit the quotes.


*10. NESTING FUNCTIONS                 
        
Normally, you probably assign the results of a MsgBox function 
to a variable and then use that variable in a Select Case 
statement. However, you can display and process the results of 
a message box in one step instead of two by nesting a MsgBox 
function in a Select Case statement. For example, try the 
following form:  
  
Private Sub Form_Click()  
  
Select Case MsgBox("Please choose Yes, No,  or Cancel to continue.", vbYesNoCancel)  
    Case vbYes  
        Print "Yes"  
    Case vbNo  
        Print "No"  
    Case vbCancel  
       Print "Cancel"  
End Select  
  
End Sub  
  
This nested arrangement omits the need for separate 
structures--you get the same result by nesting the functions. 
However, if you need to refer to the result of the MsgBox 
function later in your application, you can't. So if that's the 
case, you'll need to stick to using two separate functions.
