Visual Basic Tips #44


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

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

Proudly presents:
Visual Basic

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


*1. DATA INTEGRITY VERSUS RESOURCES  
  
Visual Basic offers two ways to pass data between routines. You 
can pass the actual data, or you can pass a pointer to the data.
The first method--ByVal--passes a parameter by value. The 
second--ByRef--passes a parameter by reference. Each has its 
advantages and disadvantages.

ByRef saves on resources, but you'll find that some developers 
consider the use of ByRef bad programming because ByRef passes 
only a pointer to the variable. While this option uses less 
memory, it doesn't protect the original variable, which can be 
altered. If you want the option to change the original value, 
then ByRef is an adequate choice, especially since it 
saves on resources.

On the other hand, ByVal passes the actual value. VB makes a 
copy of the variable for temporary use, and maintains and uses 
this copy as long as the routine is running. So ByVal, while 
requiring more memory, protects the integrity of your data.

You must also conform to what the API function expects--so if 
you're working with API functions, stick with the 
declared reference.


*2. VARIANT $TRINGS

If you've ever wondered what the $ sign means in string 
functions such as Trim$, Str$, and InStr$, it probably doesn't 
help much to know that these have seemingly identical 
functions--Trim, Str, and InStr. How do you know which to use? 
The difference is the data type of the returned value. The plain
versions (the functions without the $) return a variant data 
type. If you've declared the string, then Visual Basic must 
convert the returned variant to a string data type. However, 
the $ version will return a string without first having to 
convert from the variant. The time you save is miniscule, but 
if you're dealing with lots of strings, that time can add up. 
In addition, the variant data type requires more memory than 
does the string data type.


*3. JUSTIFYING TEXT USING FORMAT()

Did you know you can use the Format() function to justify text?
You can, by specifying the number of characters, the underlying
field, and the ! character. For instance, if the field has a 
width of seven characters, the function

Format$("abc", "@@@@@@@")

would right-justify the string abc. If you want to left-justify 
a string, simply add the ! character to your format 
code in the form

Format$("abc", "!@@@@@@@")


*4. SAVING TIME WITH AUTOREDRAW

Visual Basic handles graphics in two ways: permanent and 
temporary. For the most part, all controls and pictures are 
considered permanent. Lines and circles are temporary, which 
means VB redraws them as necessary.

The AutoDraw property is False by default. If you set it to 
True, everything becomes permanent, which means that VB won't 
redraw anything on the fly. Instead, VB will keep a copy of 
the entire object (form) in memory.

When AutoDraw is False, a form will redraw twice as fast as a 
form with an AutoDraw property set to True. However, you'll 
need to add code that redraws the erased components in the 
form's Paint event.


*5. QUICKLY ROUNDING DECIMAL VALUES

There are many approaches to rounding. Perhaps the easiest 
way to round decimal values is to assign them to 
an Integer value:

iOne As Integer, iTwo As Integer

iOne = 1.6 
iTwo = 7.3

Print iOne, iTwo

The results are 2 and 7. Of course, if you need to retain 
rounded decimal values, this quick solution won't work.


*6. KEEP IT SIMPLE

When designing applications, you may be tempted to add lots of 
bells and whistles--but you're well advised to carefully 
consider each option. For instance, if you create a data entry 
form, you'll most likely insert Add, Edit, Delete, and Exit 
command buttons. You might consider adding the usual OK and 
Cancel buttons. You might even want to add a button to confirm 
the other action tasks--adding, deleting, and so on--but are 
these extra options necessary?

First, let's consider the OK and Cancel buttons: Just when will 
the user use them? Probably never, so why include them? In 
addition, a Confirm button may prove more of an annoyance to 
your user than a help. It presents an extra click--or 
interruption--each time your user tries to do something. 
Besides, if you make the Add button the default, the user can 
avoid stopping for a click until he or she needs to edit or 
delete a record. This setup is more efficient since it doesn't 
call for two clicks for every record.

Your best bet is to include only the functionality you 
really need


*7. REFERENCING FIELDS IN RECORDSETS

When working with recordsets, you can access your data in 
several ways. First, you can use the recordset object's Field 
property using the form

Field = DataControl.Recordset.Fields("fieldname")

where Field represents your field variable, DataControl is the 
name of your Data Control control, and fieldname is the name of 
the underlying field you're accessing.

Fortunately, you don't have to know the names of the fields 
because you can access them by order, using the form

Field = DataControl.Recordset.Fields(0)

to access the first field in the table. If you want to access 
the second field, specify the value 1; to access the third 
table, use the value 2, and so on.

You can also take advantage of a default shortcut: Since the 
Fields property is the recordset object's default, you can omit 
the Fields component using the form

Field = DataControl.Recordset("fieldname")

or

Field = DataControl.Recordset(0)


*8. DISPLAY A PICTURE ON THE STATUS BAR

Did you know that you can display a picture in the status bar? 
Simply right-click the status bar and select the Properties 
command. Then, click the Panels tab and add a new panel. Next, 
click the panel's Picture area, choose Browse, and locate the 
picture you want to display. It couldn't be simpler!


*9. SELECT TEXT IN A TEXTBOX

You may want a control to select the contents of a TextBox 
control automatically once Visual Basic gives that control 
focus. To do so, attach the following procedure to the 
control's Got Focus event:

  Private Sub Text1_GotFocus() 
  SendKeys "{Home}+{End}" 
  End Sub

When VB selects the control, the GotFocus event will select the 
control's text value.


*10. ADD INFORMATION WITH COLOR

Typically, the more information you can give your users, the 
better. One easy way to quickly convey information is with 
color. For instance, if you want users to know a particular 
field is required--meaning they must enter data in that field 
before they can go to the next record--you can create a label 
control and enter the message

This is a required field, you must enter data.

But that solution requires that your user stop to read the 
message, which can waste time and be a nuisance.

Instead, use the same color consistently throughout your 
application to send a signal to your users that they must enter 
data in that field to continue. (Red works great.) Your users 
will learn and adjust quickly, and at a glance they will know 
that a field requires an entry. On the other hand, if most of 
your fields are required, it might be just as effective to apply
special coloring to the fields that aren't required.

You can use color to denote all kinds of information. 
Just be sure to use it consistently and sparingly.
