Visual Basic Tips #17


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

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

Proudly presents:
Visual Basic

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

*1. DATA LINKS STORED IN ENVIRONMENT, NOT PROJECT                 
        
In our opinion, one of the best features of the VB 6 environment is 
the support for Data Links. No longer do you have to keep both VB and 
a database tool open--you can do it all within VB. Part of this 
functionality is the ability to "memorize" database connections in 
Data Links. The important thing to remember is that these Data Links 
are not stored with your project; instead, they are stored as part of 
your development environment on a single machine. This means that 
you'll have to either copy or re-create them if you change machines.


*2. USE FORWARD-ONLY RECORDSETS WHENEVER POSSIBLE                 
        
If you're doing a lot of data manipulation, try to use forward-only, 
read-only recordsets as much as possible. These recordsets are 
optimized for fast access and don't require the resource overhead that 
either static or dynamic recordsets take. Also, when you're done, be 
sure to Close the recordset and set it to Nothing. This will release 
the resource back to the system. Supposedly, the system will 
automatically clean up these references, but we always like to 
explicitly close our objects personally, just to make sure they're 
closed.


*3. FAVORITES IN MSDN LIBRARY                 
        
Ever since Microsoft did away with the traditional help files, we have 
found ourselves pulling our hair out whenever we have to look up 
material in MSDN. It always seems that the keywords we pick bring up 
the wrong topics. However, there is a feature we recently noticed in 
MSDN: Favorites. This lets you find a topic, such as the ADO 
Programmer's Reference, and create a bookmark to it. You can then go 
directly to that topic just by picking it from the Favorites tab. This 
will make navigating the MSDN Library a bit easier in the future.


*4. REMOVE EXTRA REFERENCES AND CONTROLS                 
        
When you're finishing up an application, be sure to remove any 
controls you haven't used from your toolbox. Also, remove any 
libraries you have referenced but currently aren't using. If you're 
not sure, try to remove the library (or control). If it's in use, VB 
will tell you so. Otherwise, you'll save yourself some disk space when 
you attempt to deploy the application.


*5. MOVING CONTROLS INTO A FRAME                  
         
If you find that your form has gotten too complicated, or you need to 
make another group of OptionButton controls, for instance, you'll need 
to move your controls inside a container control, such as a frame. 
Before you delete your controls and re-create them, follow these 
steps. 

First, draw the Frame control somewhere on your form where you can see 
the controls that you want to put in the frame. Next, highlight all 
the controls you want to put in the frame and select Edit, Cut, or 
press Ctrl-X. Then, click on the Frame control and select Edit, Paste, 
or press Ctrl-V. 

The controls will be dropped inside the frame and can be repositioned 
within the frame borders. Any code you've written or any property 
values you've set for the controls will be preserved.


*6. WHY I USE OBJECT-ORIENTED PROGRAMMING                  
         
A reader recently asked me why he should program using object-oriented 
programming techniques. The main reason I use them is because I can 
map real-world objects, like customers and orders, to computer 
programming. Instead of thinking about creating records in tables to 
represent a customer, I simply use the CreateNew method of the 
Customer object. It's also easier to understand from a process or 
conceptual basis, since you don't have to get into the implementation 
details of how something is done. Finally, you can also change how a 
function works without affecting everything else in the system. In 
short, it makes my job as a programmer easier, even though there is 
often more code to write.


*7. DYNAMIC FORMATTING                  
         
One thing I like to do in my data entry forms is to be as flexible as 
possible in allowing data entry. For instance, dates can be entered in 
many different ways, all of which are valid. To allow for this, I add 
a little code in the LostFocus event of the text box in question: 

 
Private Sub txtBox_LostFocus() 
   If txtBox <> "" Then 
      txtBox = Format(CDate(txtBox), "mm/dd/yyyy") 
   End If 
End Sub 
 

As long as the user enters something that vaguely resembles a date to 
Visual Basic, it will convert the entry to mm/dd/yyyy format and put 
it back in the box. This allows for flexibility but makes it easy to 
let the user see whether the value he/she typed was actually valid. 
You can also add your own error handling to handle cases in which the 
date entered isn't valid.


*8. DYNAMIC FORMATTING                  
         
One thing I like to do in my data entry forms is to be as flexible as 
possible in allowing data entry. For instance, dates can be entered in 
many different ways, all of which are valid. To allow for this, I add 
a little code in the LostFocus event of the text box in question: 

 
Private Sub txtBox_LostFocus() 
   If txtBox <> "" Then 
      txtBox = Format(CDate(txtBox), "mm/dd/yyyy") 
   End If 
End Sub 
 

As long as the user enters something that vaguely resembles a date to 
Visual Basic, it will convert the entry to mm/dd/yyyy format and put 
it back in the box. This allows for flexibility but makes it easy to 
let the user see whether the value he/she typed was actually valid. 
You can also add your own error handling to handle cases in which the 
date entered isn't valid.


*9. SUB MAIN NOT NEEDED FOR DLLS                  
         
In a previous tip, I stated that you should add a code module with an 
empty Sub Main to your DLL projects. It turns out that this is no 
longer (or possibly never was) necessary. You can just add your 
classes, compile, and go. I often have a code module with shared 
routines used by all the classes, but Sub Main does not need to be one 
of those routines.


*10. DECOMPILING VISUAL BASIC                  
         
A question I often get from readers is whether there is a decompiler 
for Visual Basic--that is, a program that can look at an .exe file and 
extract the source code from it. After doing a bit of research, I 
found that there no longer is a VB decompiler available for recent 
versions of VB. 

This question comes up a lot when people have lost their source code 
but still have the executable file. The best solution to this is to 
make lots of backup copies or to use a tool like SourceSafe (or any 
other version control software) to make it easier to manage your 
source code. If you were looking for a decompiler to take someone 
else's code, guess what? That's illegal... and you're still out of 
luck.
