Visual Basic Tips #4


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

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

Proudly presents:
Visual Basic

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


*1. DOWNLOAD CONTROL PAD                 
        
If you use VB to work with Web pages, you should consider 
downloading a free authoring tool named Microsoft ActiveX 
Control Pad. Using this tool, you can add controls and scripting
to your HTML with a simple click. For more information, visit 

http://msdn.microsoft.com/workshop/misc/cpad/default.asp 

Although this tool has been around for awhile, it's worth 
mentioning if you're developing for the Internet.


*2. PRINTING SMALL FORMS                 
        
Have you ever needed to print a form, but couldn't use the result
because it was too small? There's no quick and easy feature that 
will print a larger view of your form. However, you can work 
around this limitation. In the print event, hide the form; resize
it until it's large enough to produce a readable hard copy. Then 
print the form, return the size to its appropriate size, and 
unhide it. You'll need to spend a little time experimenting with 
the form until you find just the right size (enlarged). Simply 
maximizing the form may be a consideration.


*3. RESIZING THE FLEXGRID CONTROL                 
        
The FlexGrid control displays tabular data, lending flexibility 
to your sort and merge tasks. If the text in a particular cell 
is too long and the WordWrap property is set to True, the text 
will wrap to the next line within the same cell. However, you 
may still need to increase the cell's row height to read the 
entire entry. 

Another consideration is to allow the user to resize the height 
(and/or width) of the cell as they find necessary. To do so, 
simply set the FlexGrid's AllowUserResizing property to the 
appropriate setting: flexResizeColumns, flexResizeRows, or 
flexResizeBoth. You can set this property at runtime using 
the statement 

MSFlexGrid1.AllowUserResizing = flexResizeBoth


*4. ACTIVEX CLASS                 
        
When loading an ActiveX control at runtime, you probably use 
CreateObject. However, if you don't know the control's class 
name, this function will return an error. To learn the 
control's class name, add the control to a temporary form. 
Then, press F2 to open the Object Browser. In the Libraries 
combo box, locate the control and select it. The Object Browser
will update the Classes and Members of controls with not only 
your control's class name, but also its methods and properties 
(if applicable).


*5. CONVERTING 6.0 PROJECT TO 5.0                 
        
You can share a 6.0 project with someone who's still using 5.0 
if your 6.0 project doesn't take advantage of any new 6.0 
features. When this is the case, open the 6.0 project's .VBP 
file in a text editor--Notepad will do. Locate the statement 

Retained = 0 

which will probably be close to the bottom. Delete this line and 
then save the file. You should be able to open the project in 
5.0 after making this small change.


*6. TESTING FOR A BLANK STRING VARIABLE                 
        
Checking a variable to see if it's blank can be done in several 
ways. For instance, you can use a simple expression to check the 
variable for Null or "". The expression 

sVariable = vbNullString 

returns True if sVariable is a null string and False if it isn't. 
(The vbNullString constant represents an empty string.) 
Similarly, the expression 

sVariable = "" 

returns True if sVariable is blank and False if sVariable 
actually stores a string. 

However, there is a faster expression. Tests show that 
the expression 

iResponse = Len(sVariable) = 0 

is considerably faster than these methods.


*7. TOGGLING CODE--THE EASY WAY                 
        
Many controls act as a toggle switch--clicking it simply reverses
its state. For instance, if the control is off (equals false), 
clicking the control turns it on (that is, changes the value to 
true). Likewise, if the control is on, clicking it will turn it 
off. To use a toggle control, you simply add code that responds 
to the control's current state. If the control is on, do such and
such; if the control is off, do something else. 

You might be surprised to learn that most of the time you won't 
need a lot of code for your toggle controls--that is, if your 
control's purpose can be reduced to a simple Boolean value (True
or False). When this is the case, use the Not operator to switch
between the two values. For instance, let's suppose you want to 
toggle a control's Enabled property between True and False. To do
so, you'd simply use the Not operator in the form 

control.Enabled = Not control.Enabled 

where control represents the name or reference of your toggle
control. If the control's current state is true and the control's
Enabled property is on (True), then clicking the control will 
reverse that situation by turning the Enabled property off 
(False). You aren't limited to using this technique with 
properties, but the control's state must reduce to a 
Boolean value.


*8. WRITING MORE EFFICIENT CODE                 
        
Using nesting functions can help you eliminate unnecessary 
statements in your code. Let's suppose that you want your 
users to choose an option from a message box. Then, you want 
to run that response through a series of conditional tests. 
To attack such a problem, you might use the MsgBox function to 
display the choices and then assign the users' response to a 
variable. You could use a Select Case or an If statement to 
determine what happens next--which is dependent on your users' 
choice. In this case, your code might resemble the following: 

Dim iResponse As Integer 
iResponse = MsgBox("Select from the following:", vbYesNoCancel) 
Select Case iResponse 
 Case vbYes 
        Print "1" 
    Case vbNo 
        Print "2" 
    Case vbCancel 
        Print "3" 
End Select 

A more efficient way to attack this task is to nest the MsgBox 
and Select Case functions as shown below: 

Private Sub Form_Click() 
Select Case MsgBox("Select from the following", vbYesNoCancel) 
    Case vbYes 
        Print "1" 
    Case vbNo 
        Print "2" 
    Case vbCancel 
        Print "3" 
End Select 
End Sub 

(We attached this procedure to a form's Click event so you could 
easily verify that the nested function works.) As you can see, 
we've deleted a declaration and an entire line of code. This 
method has one limitation, however. You can't retain the result 
of the MsgBox function as a variable for later use. So, if you'll 
need that value again, stick with the first form.


*9. TO USE OR NOT TO USE PARENTHESES?                 
        
Have you ever noticed that sometimes functions and methods contain
parentheses and sometimes they don't? For instance, you've 
probably seen statements that resemble the following: 

MsgBox "Do you wish to continue?" 
iResponse = MsgBox("Do you wish to continue?") 

The first statement doesn't enclose its arguments in parentheses, 
but the second one does. The reason is simple. If you're just 
executing the function, as we did in the first statement, you 
don't need the parentheses. If you're assigning the result of a 
function to a variable, as we did in the second statement, 
you enclose the arguments in parentheses. 

If the Auto Syntax Check option is selected (the default), 
entering the statement incorrectly--whether or not the entry 
includes parentheses--will return a syntax error. If this option 
is turned off, it won't. To check your settings, select Options 
from the Tools menu and review the Editor tab.


*10. CORRECT DECLARATIONS                 
        
If you're new to VB, you may already know about declaring 
variables, but try not to make assumptions that could cause 
you trouble. For instance, some programmers (when new to VB) 
try to declare variables using the following form: 

Dim iOne, iTwo, iThree As Integer 

which is incorrect if you think you're declaring all three 
variables as integers. In fact, VB defines only iThree as an 
integer. The first two integers are defined as Variants because 
you didn't specify a data type. 

There are two syntax forms for declaring variables: 

Dim iOne As Integer 
Dim iTwo As Integer 
Dim iThree As Integer 

And 

Dim iOne As Integer, iTwo As Integer, iThree As Integer
