Visual Basic Tips #32


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

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

Proudly presents:
Visual Basic

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


*1. PADDING NUMERICAL STRINGS WITH ZEROS                 
        
Adding leading zeros to a numerical string is a fairly common task
and can be accomplished with formats. However, the procedure shown
here is more flexible than a format, since you can change the 
number of zeros at any time: 

Function ZeroPad(pad As String, places As Integer) 
While places > Len(pad) 
pad = "0" & pad 
Wend 
ZeroPad = pad 
End Function 

You'll pass the string you want to pad with zeros and the number 
of places you want the finished string to contain. Then, as long 
as the number of places is larger than the string you want 
padded, the procedure adds a zero. For instance, if you want to 
pad the value 2 with enough zeros to make the final string three 
characters, you'd pass this function the values 2 and 3. 

The procedure will cycle through the While loop twice, each time 
adding a 0 character to the value 2. The final value will be 002.


*2. FINDING AN OBJECT'S DEFAULT PROPERTY                 
        
You probably know that you can omit the object's default 
reference. In fact, we've discussed this referencing shortcut in 
earlier tips. But you may be wondering how you know what an 
object's default property is. There's an easy way to find out. 
Just open the Object Browser and select the object in the 
Classes list. The Members list will update accordingly, and the 
default property will display a small blue circle right above it.


*3. DO YOU REALLY NEED THAT PUBLIC FUNCTION?                 
        
You probably see constants declared in the General Declarations 
section from time to time. This is particularly true when your 
code contains API calls. However, sometimes we find these 
constants are misplaced because they're used by only one 
function. When this is the case, you should consider moving 
your constant(s) to the procedure that uses it (or them). That's 
because Access will automatically release the memory used to 
declare those constants when the procedure ends. 

On the other hand, constants declared at the module level remain
in memory for the lifetime of the application. As you can see, 
if the constant is used by only one function, you could be 
wasting resources. 

If you must leave the constant in the General Declarations 
section, you can still free up those resources. Simply reset the
constants in the last procedure that refers to the constant. For
instance, if your constant is a string variable, you'd use the 
statement 

strMyString = "" 

to free up any resources previously assigned to strMyString.


*4. WHERE TO USE NOTHING                 
        
We often recommend that you set all your object and DAO (or ADO) 
variables to Nothing at the end of a procedure using the syntax 

objVariable = Nothing 

However, the best place to include this statement is when the 
function is done with the variable--not at the end of the 
procedure. There's really no reason to wait until the end of the 
procedure to free up those resources. Instead, free up those 
resources as soon as the variables are no longer needed.


*5. SETTING PROPERTIES                 
        
VB allows you to set a control's properties via a property sheet 
or Properties window during design time. After locating the 
appropriate property field, you can type the setting. Some 
properties have predefined settings. When this is the case, you 
can choose the setting from a list of items. To do so, simply 
click the field's drop-down list and choose an item. 

When a property has predefined settings, you can also toggle 
through those settings by clicking the property field label (the 
property name to the left of the property field). Click this 
label and VB will toggle through the different property settings.
Stop toggling when you reach the setting you want.


*6. A FEW DIR() TRICKS                 
        
VB users can use the Dir() function to check paths for valid 
files. This function uses the syntax 

Dir("path") 

where path is the file's entire path and filename, including the 
file's extension. If the file doesn't exist, the Dir() function 
returns a zero-length string (""). If the file does exist, the 
function returns the file's name. 

You might not realize that there's a little more to Dir() than 
just checking for a valid file. The form 

Dir() 

will return the name of the first file in the current directory. 
Each time you repeat this function, it returns the next file in 
the current directory. This behavior can be a big help, or a 
nuisance, depending on what you want to accomplish. 

You must have the VBA library referenced for this function to work.


*7. VARIABLE DOESN'T WORK?                 
        
When a value doesn't display or consider a decimal component in 
a calculation--and you know it should--check that value's data 
type. The value must be a Single or Double data type to store a 
decimal component. This is a common mistake and it's easy to 
make. While it's obvious that the Integer and Long Integer data 
types both store only integers, we use them so often that it's 
easy to enter Integer or Long without realizing the 
repercussions. Another problem is that we don't always know 
during the design stage that a variable might need to 
accommodate a decimal value.


*8. CHANGING DATA TYPES                 
        
A few tips ago we reminded you that any time you might need to 
store decimal values, you must remember to declare the field or 
variable's data type as Single or Double. If you forget, you can 
always change the data type later to accommodate decimal values. 
However, we would like to offer one word of caution about doing 
so: If you change a data type after you've entered data, you risk 
losing that data, and you can't undo the damage. Here's what 
happens: If an existing value is too large for the new data type,
Access may truncate, round, or even delete that value. This 
caution should be applied any time you change a field property 
when data already exists.


*9. UNDERSTANDING NULLS                 
        
Don't let Null values intimidate you--they can be confusing, 
even for the experts. Fortunately, a few basic principles 
should help you out. 
A Null value indicates one of two conditions: 

The value is missing or unknown. 
The value doesn't apply. 

When a value is missing or unknown, it simply means the 
information may exist, but we just don't know it. That doesn't 
mean it DOES exist--we simply don't know. Occasionally, the 
information doesn't apply to a particular record and you must 
leave a field blank. Let's suppose you've got a table of phone 
numbers and two fields are blank. In one case, you know the 
person has a phone; you just haven't been able to acquire the 
number yet. Eventually, you may fill that field. In the second 
case, you know the person doesn't have a phone; that field will 
remain empty (unless the person gets a phone). In each case, 
the field returns a Null value.


*10. MORE ON NULLS                 
        
In our last tip, we talked about Null values as values that are 
unknown or don't apply. We used phone numbers to illustrate our 
point. If the phone number is unknown or if someone doesn't have 
a phone, you'll leave the phone field blank for that person. 

However, if you know the person has no phone, you might want to 
consider an alternative to Null--to avoid confusion. When there 
is no phone (versus no phone number), consider using an empty 
string ("") or an actual string value, such as "no phone" or 
"N/A", so you can readily tell the difference between an unknown 
phone number and no phone.
