Visual Basic Tips #43


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

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

Proudly presents:
Visual Basic

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


*1. INFORMATION ABOUT ADO  
  
If you're still waiting for the ADO (ActiveX Data Objects) 
train, you may find yourself left at the station! Office 2000 
includes an enhanced version of ADO, and ADO is fast becoming 
the model of choice (among serious developers, it already is).

ADO was designed as a client-server system and should seriously
increase usage of Microsoft products in Internet applications, 
because ADO can access many types of data--not just 
relationship database data. Now you can use ADO to access Web 
pages, spreadsheets, and documents. Eventually, ADO will replace
DAO and RDO, which access only relational databases.

You can learn more about ADO and how it fits into your
developing needs by visiting these Web sites:

Microsoft ActiveX Data Objects 
http://www.microsoft.com/data/ado/

OLE DB Technology 
http://www.oledb.com/ole-db/guide.html

Using ActiveX Data Objects (ADO) via Visual Basic 
http://support.microsoft.com/support/kb/articles/Q168/3/35.asp


*2. SOLVE DLL REGISTRATION PROBLEMS

Normally, registering a DLL is automatic--Windows searches the 
registry database for the DLL's identification number; if it 
doesn't find the number, Windows executes the DLLSelfRegister 
function. If, however, the DLL has been registered incorrectly, 
Windows returns an error and doesn't load the DLL. At this point,
you can try to register the DLL manually.

To register a DLL manually, you need regsvr32.exe, which is 
available on the MSDN Library disk that comes with Visual Basic 
6.0. Once you have the program, choose Start, Run and type

path\regsvr32.exe nameofdll

If this doesn't work, you may need to remove the old 
registration details by typing the following (using Run):

path\regsvr32.exe /u nameofdll

Once you've unregistered the incorrectly registered DLL, you 
should be able to reregister it by repeating the instructions 
above. If all else fails, try locating a newer version 
of the DLL.

By the way, if you don't have regsvr32.exe, you can perform the 
tasks with a handful of similar utilities, such as DLL/OCX 
Register, available at your favorite shareware site.


*3. FORM ICONS

You can use an icon to represent a form when the form is 
minimized. To do so, simply set the form's Icon property to the
icon file (ICO) of your choice. You can do the same with a 
project, and the specified icon will be displayed to represent 
the distributed EXE file.

If you find that the icon is small and looks bad, you're 
probably running in 256-color mode. If you must retain the 256 
mode, then choose another icon--one that uses a 16 x 16 image.


*4. WORKING WITH DEFAULTS IN A COMBO BOX

When you add items to a combo box, Visual Basic doesn't 
automatically display one of those items in the control's text 
box as the control's default. You can, however, specify a 
default by adding the statement

   comboboxname.ListIndex = x

where x represents the list item you want displayed as the 
control's default. (Remember that the index values 
begin at 0, not 1.) 


Using this method has one drawback--specifying the default in 
this manner will fire the control's Click event. So, if you want
to use the control's Click event elsewhere, you might want to 
bypass this method.


*5. HELPFUL ERROR MESSAGES  
  
Almost every application passes information to the user--and we 
often use the MsgBox function to relay this 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."
 
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 A COMPUTER'S 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 a value 
for 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 Visual 
Basic 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 context menu. VB will give focus to the 
selected function--even if it's in a different module. You can 
also choose View, Definition or press Shift-F2. Pressing 
Ctrl-Shift-F2 will return you to the calling procedure.


*8. A COMMON 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 Visual Basic 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) 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 Project, Component (or 
press Ctrl-T). Visual Basic will display the Components dialog 
box. Here, you can find and register any ActiveX control 
on your system.


*10. OBJECT VS. COMPONENT

In a previous tip, we told you how to find the class name for an
ActiveX control. 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--as do DLLs.

By now, you're probably wondering why you should care about COM.
If you want to succeed as a Visual Basic 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 this site:

Microsoft COM White Papers 
http://www.microsoft.com/com/wpaper/default.asp
