Visual Basic Tips #3


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

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

Proudly presents:
Visual Basic

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


*1. 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 (such as decimal and thousand separators). 
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 that 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. The functions 
 
CDbl("1234)" 
 
and 
 
CDbl("1,234") 
 
both return the value 1234.


*2. TURNING OFF THE SYNTAX ERROR DIALOG BOX                 
        
When entering code, VB 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 
Options...   from the Tools menu, select the Editor tab, and then 
uncheck the auto Syntax Check item in the 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 before doing so.


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


*4. READ ONLY TEXT BOX CONTROL                 
        
You can quickly and easily make a text box control 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.


*5. HELPFUL ERROR MESSAGES                 
        
Almost every application passes information to the user--we often 
use the MsgBox function to relay specific information. Knowing how 
to pass the information is just part of the job. You should 
consider the message itself. Especially when the information is 
the result of an error. In this case, you want to share the 
information without assigning blame. Here are a few guidelines 
you can adopt to make sure your messages are effective, but not 
inflammatory:

- Forget the geek words such as invalid, aborted, failed, fatal,
  and illegal. These words aren't just overly technical--they're
  often negative. Phrases such as "doesn't work" and "isn't
  available" will get your point across without intimidating
  your user.
- Don't be overly dramatic and use exclamation points. They
  might cause unnecessary alarm. Use a normal tone of voice.
- Don't accuse the user of creating the error with messages such
  as "You have failed to" or "You tried to." Leave the user out
  of it. Instead, try phrases such as "The current operation,"
  or "Please enter the data in the #### format."
  
In fact, the last guideline is the most useful. Users don't need 
to know what they did wrong; they just need to know how to 
resolve the problem.


*6. RETURNING THE COMPUTER NAME                 
        
You can return the name of the current system by calling the 
GetComputerName API function. This function requires two 
arguments: a buffer that holds the returned name and the maximum 
size of the buffer. In addition, this function returns a 
Null-terminated string, so you must trim the extra characters 
from the name before you try to use it.  

You can create an example by positioning a command button on a 
blank form and adding this code to the form's module:  
  
Private Declare Function GetcomputerName Lib "kernel32" Alias  
"GetComputerNameA" (ByVal _ lpBuffer As String, nSize As Long)
 As Long  
  
Function GetComputer() As String  
Dim temp As Long  
Dim Name As String * 255  
Name = Space(255)  
temp = GetcomputerName(Name, 255&)  
GetComputer = Left$(Name, InStr(Name, vbNullChar) - 1)  
End Function  
  
Private Sub Command1_Click()  
Dim Name As String  
Name = GetComputer  
MsgBox "The computer name is " & Name  
End Sub  
  
If you're new to APIs, the Declare statement goes in the form's 
General Declarations area. Once you've added the code, simply run
the form and click the command button to return the computer's
name.


*7. JUMPING TO FUNCTIONS                 
        
It's common to find function calls in your code. When VB encounters 
a function call, it routes the flow to that function and then 
returns to the calling function once the task is complete. If you 
want to view the called function quickly, simply right-click the 
function name in your code. Then select Definition from the 
resulting shortcut menu. VB will give focus to the selected 
function--even if it's in a different module. You can also choose 
Definition from the View menu or press Shift-F2. Pressing 
Ctrl-Shift-F2 will return you to the calling procedure.


*8. AN EASY DATE MISTAKE                 
        
When working with date functions such as DateAdd, DateDiff, and 
DatePart, you need to pay close attention to the time arguments. 
Even though "h" and "s" represent hours and seconds, 
respectively, you can't use "m" to represent minutes. That's 
because VB assigns the "m" setting to months. When working with 
time, be sure to use the appropriate minute setting, which is 
"n." this is an easy mistake to make and a hard one to find, 
because the "m" seems like such a natural and correct setting 
for the minute component.


*9. FINDING REGISTERED ACTIVEX CONTROLS                 
        
ActiveX controls (.ocx's) are one type of COM component--one 
that has a user interface associated with it. (DLLs and EXEs are 
two others.) If you'd like to know which ActiveX controls are 
registered on your local system, choose the Component option 
from the Project menu (or press Ctrl-T). VB will display the 
Components dialog box. From here, you can find and register any 
ActiveX control on your system.


*10. OBJECT VERSUS COMPONENT                 
        
In a previous tip, we told you how to find the class name for an 
ActiveX control. In doing so, we mentioned that ActiveX controls 
are COM components. You might think that the terms COM object and 
COM component are interchangeable. On the contrary: a component 
is typically an ActiveX control, a DLL, or an EXE. A COM object 
is an instance of a COM component. It's a nit-picky difference, 
but technically, they aren't the same. 

In regards to the components: ActiveX controls expose themselves 
to numerous applications. DLLs are COM-enabled files that contain 
COM components. That leaves us with EXE files. They are similar 
to DLLs, but they don't run in the same memory space that calls 
them--DLLs do. 

By now, you're probably wondering why you should care about COM. 
If you want to succeed as a VB developer, you need to know what's
going on beneath all that code, and COM is the key that will 
unlock that door for you. If you aren't proficient with COM 
technology, it's worth learning about. For a list of white papers
on the subject, visit 

http://www.microsoft.com/com/wpaper/default.asp
