Visual Basic Tips #35


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

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

Proudly presents:
Visual Basic

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


*1. STEALING FROM HELP                 
        
The Help system is full of code fragments that you can put to 
good use as is or with a few minor adjustments. So before you 
try to reinvent the wheel, do a quick search in VB's Help system 
for any code that you can use as the basis for your current task.
When you find something you can put to use, highlight those lines
with your mouse, right-click the selection, and choose Copy (or 
press Ctrl-C). Then, switch to a module and click the Paste 
button on the Standard toolbar, or press Ctrl-V.


*2. THE FASTEST COUNT                 
        
You probably know that you can count records in a recordset by 
using the RecordCount property. However, to do so, you must 
populate the entire recordset by executing the MoveLast method. 
If you recordset is large, this is inefficient and unnecessary,
because the SQL Count function is faster. For instance, the code 

strSQL = "SELECT Count(*) FROM table3" 
Set rst = db.OpenRecordset(strSQL) 
Debug.Print rst(0) 

is faster than 

Set rst = db.OpenRecordset("table", dbOpenDynaset) 
rst.MoveLast 
Debug.Print rst.RecordCount 

Granted, in a small database, you may not notice the difference. 
However, if you have thousands of records, you should definitely 
notice an improvement.


*3. THE TWO COUNTS                 
        
In our last tip, we showed you how to use the SQL Count(*) 
statement to return quickly the number of records in a 
recordset. If you're working with a Jet database and you're 
tempted to replace the * character with a field name because 
you think referencing a specific field will be faster, don't. 
You see, the Jet has special optimization rules for the * 
character, and it runs faster than specifying a field name. 
Specify a field name in a SQL Count function only when you need 
a count of that particular field (in a Jet database).


*4. ADDING AN ITEM TO A LIST BOX                 
        
To fill a list or combo box, you use the AddItem method. Did you 
know that you can add an item to a specific position within the 
list? You can if you include the method's index value in the form 

List1.AddItem "One", 0 

where 0 represents the first position in the list. For instance, 
the following procedure will display the items "Two" and "Three" 
in a list box named List1: 

Private Sub Form_Load() 
List1.AddItem "Two" 
List1.AddItem "Three" 
End Sub 

This second procedure will add the item "One" to the beginning 
of that same list when you click the form. 

Private Sub Form_Click() 
List1.AddItem "One", 0 
End Sub 

(Just remember that the index values begin with 0, not 1.)


*5. WORKING WITH ENUMERATIONS                 
        
An enumeration is a special type of constant that automatically 
assigns values to its members. Using an enumeration is an easy 
way to work with a set of related constants. For instance, VB has 
a built-in enumeration named vbDayOfWeek, which contains 
constants for each day of the week. To see how it works, open 
a module and type  

vbDayOfWeek  

followed by a period. VB will automatically display a list of 
vbDayOfWeek's constants. 

To create your own enumeration, declare an enumeration type 
using the Enum statement in the Declarations section of a 
standard or public class module using the syntax 

Private Enum WorkDays 

or 

Public Enum WorkDays 

Then list the constant names. VB will assign the value 0 to the 
first constant named in the list and increase the value by 1 for 
each subsequent constant in the list. For instance, 
the enumeration 

Private Enum WorkDays 
Monday 
Tuesday 
Wednesday 
Thursday 
Friday 
End Enum 

would assign the values 0 through 4, respectively, 
to the items listed. 

To use the enumeration, declare a variable using the 
enumeration in the form 

Dim DayOff As WorkDays 
DayOff = Monday


*6. ENUM DATA TYPES                 
        
In our last tip, we showed you how to create your own enumeration
constant. Although it appears that you're creating a unique data 
type, you really aren't. Visual Basic treats all constant values 
in an enumeration as Long integers. If you assign a decimal 
value to an enumeration constant, Visual Basic will round that 
value to the nearest Long integer.


*7. CANCEL UNLOAD                 
        
We've talked about using the Unload method to close all open 
forms before quitting an application. However, you can override 
that method using the form's QueryUnload event. This is the very 
last event triggered when you unload a form. If you change this 
event's Cancel argument to True, VB won't unload the form. This 
is a good way to avoid losing unsaved data. Simply set this 
argument to True right before you enter data and change it to 
False once the data is saved.


*8. WHEN CANCEL DOESN'T WORK                 
        
In our last tip, we talked about using a form's QueryUnload event
to cancel an Unload method. Unfortunately, setting the event's 
Cancel argument to True is not a foolproof strategy. If you use 
the End statement to quit your program, VB won't trigger your 
form's QueryUnload event. The same is true if you click the End 
button or choose End from the Run menu in the 
development environment.


*9. CONTROL LIMITS                 
        
Few of us ever reach the limit for controls on a form, which 
happens to be 254. Actually, that's not technically correct. 
Each form is limited to 254 control names. However, one control 
array serves as only one control name because all the controls 
in an array share the same name. Therefore, you can stretch that 
254-control name limit quite a bit--as long as your resources 
hold out.


*10. VIEWING A FORM'S DESCRIPTION                 
        
Most VB files are saved in binary format, which makes reading 
these files a bit difficult. Forms and projects, however, are 
saved as ASCII text and are easily readable in a text viewer. 
Simply open the .frm or .vbp file in your word processor the 
same way you'd open any file to display a text version of your 
form that contains the following:

- The version number of the file format  
- The form's description  
- The form's attributes  
- The form's VB code  

  
The following is the form description of a form with one 
command button:  
  
VERSION 6.00  
Begin VB.Form Form1  
   Caption       =   "Form1"  
   ClientHeight  =   3195  
   ClientLeft    =   60  
   ClientTop     =   345  
   ClientWidth   =   4680  
   LinkTopic     =   "Form1"  
   ScaleHeight   =   3195  
   ScaleWidth    =   4680  
   StartUpPosition =  3  'Windows Default  
End  
Attribute VB_Name = "Form1"  
Attribute VB_GlobalNameSpace = False  
Attribute VB_Creatable = False  
Attribute VB_PredeclaredId = True  
Attribute VB_Exposed = False  
Option Explicit  
 
Private Sub Form_Click() 
MsgBox Dir ("C:\rt\tipworld") 
End Sub
