Visual Basic Tips #11


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

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

Proudly presents:
Visual Basic

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

*1. NEW WAY TO VALIDATE DATA                 
        
VB 6.0's new Validate event and CausesValidation property simplify 
data validation. Before 6.0, you had to attach data-verifying code to 
the control's LostFocus event and then use the SetFocus method to keep 
the user from selecting another control. Unfortunately, this generally 
meant the user couldn't do anything at all until he or she entered the 
correct data. 

The Validate and CausesValidation properties are much more flexible 
than the previous solution. When you enter data, you fire the Validate 
event. If you set the event's Cancel parameter to True, the user can't 
select any other controls except controls with the CausesValidation 
property set to False if the entered data doesn't match the conditions 
in your Validate event code. That means the user has access to other 
controls even though the application is verifying data. 

In a nutshell, you'll use the Validate event to test entries and block 
access to other controls when data isn't appropriate. The exception 
will be the controls you want them to access, and you'll set the 
CausesValidation properties of those controls to False. Most likely a 
control used in this manner will display more information or allow the 
user to opt out of the form.


*2. NOW IT'S A COMMENT, NOW IT'S NOT                 
        
If you're moving to VB from Office and VBA, you'll be glad to know 
that commenting and uncommenting is automated with VB. It's much 
easier in VB to comment or uncomment a block of code, since you don't 
have to enter or delete each apostrophe character manually. To comment 
a block of text, highlight the text, then click the Comment Block 
button on the Edit toolbar. 

Similarly, to uncomment a block of text, highlight the text, then 
click the Uncomment Block button on the Edit toolbar. 

If you're a veteran VB user, you've known about this feature for a 
long time, but VBA users who are taking the plunge into VB will find 
this great news!


*3. THREE TYPES OF DIVISION                 
        
VB supports three types of division: floating-point division, integer 
division, and modulus. Floating-point division is what you learned in 
grade school--you simply divide one number by another and return an 
integer and a decimal value, when appropriate. For instance, the 
expression 

10 / 4 

will return the value 2.5. Integer division divides one number by 
another but returns only the integer position of the result. When 
using integer division, use the backward slash instead of the forward 
slash in the form 

10 \ 4 

which will return just 2. The final method, modulus, divides one 
number by another but returns only the remainder (or the decimal 
portion). It also requires the Mod operator. Our previous expression 
looks like 

10 Mod 4 

using modulus division, which will return the value 5.


*4. CONCATENATION OPERATORS                 
        
If you convert older applications, you may run into the plus sign used 
as a concatenation character. Older versions of VB (and BASIC) used 
the plus sign before the ampersand became the prevalent concatenation 
operator. VB continues to support the plus sign for the sake of 
backward compatibility. If you still use the plus sign as your 
operator of choice, we recommend that you begin using the ampersand 
instead, even though VB still supports the plus sign. 

Each new version usually brings replacements, and as a rule, VB will 
continue to support replaced keywords, operators, etc. for a while. 
Eventually, Microsoft usually drops the originals to make room for 
newer features. This means that someday, VB may no longer support the 
plus sign as a concatenation operator. We recommend that you 
familiarize yourself with replacements when a new version is released 
and start using those replacements right away.


*5. NEW PROPERTY FOR SLIDER CONTROL                 
        
The Slider control (or Trackbar control) isn't new to VB 6.0, but the 
Text property is. This property will display a string in a ToolTip 
window--either just above or below the slider thumb. The setup is 
simple to use: 
 - Enter the string you want to display in a ToolTip window as the 
Slider control's Text property. 
- Use the Slider control's TextPosition property to determine whether 
VB displays the ToolTip window above or below the slider thumb.


*6. MOVING AROUND                 
        
When working with database tables and recordsets, you may use the Move 
method to change the current record. Specifically, use this method's 
NumRecords argument to specify the number of rows you want to move. 
For instance, to move forward two records, you'd use the statement 

rst.Move 2 

where rst is the name of your recordset. Similarly, to move backward 
two records, you'd use the statement 

rst.Move -2 

If you use 0 as the Move method's argument, VB will retrieve the 
latest data from the current record. This behavior is helpful when you 
want to make sure that you retrieve the most recent data.


*7. MOVING BACKWARD                 
        
In our previous tip, we showed you how to move backward in a recordset 
by specifying a negative value as the Move method's NumRecords 
argument. This behavior is especially convenient when you're working 
with a forward-only recordset, because it allows you to move backward 
in an otherwise forward-only set of records. The one stipulation is 
that the record you're trying to access must be in the current set of 
cached records. If the Move method tries to move to a position before 
the first cached record, VB will return an error. 

CORRECTION: 
MOB FORMING...stop SEND REINFORCEMENTS... stop 
You transform a few values and suddenly they want to lynch you. All 
kidding aside, we did make a mistake in our division tip awhile back. 
We said the result of the expression 10 Mod 4 was 5. Of course, that's 
incorrect. The Mod operator always returns the remainder, which in 
this expression is 2. We apologize for any inconvenience. Thank you to 
everyone who gently pointed out our error.


*8. KEEPING UP WITH FORMS                 
        
A lot of forms in the same application can be a little difficult to 
keep up with. One way to keep track of your forms while testing and 
debugging is to check for the number of open forms. You can do so by 
pausing the current form (click Break) and then entering the statement 

?Forms.Count 

in the Immediate window. This statement will return the number of 
loaded forms, including the paused form.


*9. RETURNING A FORM'S NAME                 
        
In our previous tip, we showed you how to use the Forms.Count 
statement to return the number of loaded forms (in the Immediate 
window). If you want to identify your forms by name, open the 
Immediate window and run the statement 

?Forms(0).Name 

This statement will return the name of the first form in the 
collection. If there's more than one form, replace the index value 0 
with the value 1 and rerun the statement. Continue in this manner 
until you've identified all the loaded forms.


*10. DIFFERENT VIEWS FOR THE IDE                 
        
By default, VB's IDE is in what's known as a multiple document 
interface (MDI) view. This view shows all the distinct windows within 
one large IDE window. If you find this setup difficult to work in, try 
switching to single document interface (SDI) view. All the windows are 
still displayed; however, they exist independently of each 
other--there is no larger containment window. To change from MDI view 
to SDI view, first select Tools, Options. Then, click the Advanced 
tab, select SDI Development Environment, and click OK. 

The next time you run VB, it will display the different IDE windows in 
SDI view. There's no right or wrong to which view you work in--it's a 
matter of personal preference.
