Visual Basic Tips #42


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

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

Proudly presents:
Visual Basic

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


*1. EXPLICIT RETURNS

As a rule, you'll want to explicitly assign a value to be 
returned by your functions. If you don't, Visual Basic 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 what's going on, you might get unexpected results 
down the line.


*2. CENTERING A FORM

To display a form in the center of your screen, add the 
following 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 two.

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


*3. 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 order, repeat this process and 
deselect Group Members.


*4. 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, in the window 
immediately after you encounter the error, this statement:

  ?Error(Err)

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 this statement in the Immediate window:

  ?Error(codevalue)


*5. SPECIFYING DEFAULTS WITH INPUTBOX  
  
You've probably used 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:

- You'll save your users a little data entry time. 
- You'll cut down on data entry errors. 
- You'll avoid run-time errors that might occur due to 
  missing data.
 
To specify a default value, just 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 text is a value, omit the quotes.


*6. 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.


*7. CDBL VERSUS VAL  
  
The Val function converts digits stored as text to their numeric
value. However, you need to be careful when using this function
with formatted text (which contains decimal and thousand 
separators, for example). For instance, the function

  Val("1234")

will return the value 1234. However, the function

  Val("1,234")

will return the value 1.

The Val function truncates all the digits following the 
thousands separator (the comma character). That's because the 
function grabs only those values that fall to the left of the 
first text character in the string. The Val function interprets
the comma as a text character--totally ignoring its formatting 
potential as a thousands separator.

If the text you need to convert may contain formatting, you 
should use the CDbl function instead of the Val function. CDbl 
recognizes the different separators and responds accordingly. 
Accordingly, both these functions

  CDbl("1234)" 
  CDbl("1,234")

return the value 1234.


*8. DISABLE SYNTAX ERROR DIALOG BOX

When you're entering code, Visual Basic will warn you when the 
statement syntax is incorrect. By default, VB displays the 
syntax error dialog box and then displays the offending statement
in red. If you find the dialog box annoying and unnecessary, you
can turn it off. Select Tools, Options; click the Editor tab; and
then deselect Auto Syntax Check under Coding Options. VB will 
still display the statement in red until you correct it, but you
won't have to dismiss the syntax error dialog box first.


*9. QUICKLY LOOPING THROUGH A RECORDSET

A common database task is to loop through all the records in a 
recordset. Typical code for completing such a task might 
resemble the following:

  Do While Not rst.EOF 
  ...task 
  rst.MoveNext 
  Loop

However, this structure can be slow because the code checks for
the end of the file at the beginning of each loop. You can speed
things up by eliminating this check using the following setup:

  rst.MoveLast 
  iTotal = rst.RecordCount 
  rst.MoveFirst

  For iCounter = 1 To iTotal 
  ...task 
  rst.MoveNext 
  Next iCounter

Instead of checking to see if you've reached the end of the file,
the For loop simply ticks off the appropriate number of cycles. 
This structure will speed up your search a great deal--as much 
as 30 percent.


*10. READ ONLY TEXT BOX CONTROL

You can quickly and easily make a text box control read only by 
setting its Lock property to True. If you should need an 
alternative to the property setting, you can enter the statement

  KeyAscii = 0

as the control's KeyDown event.
