Visual Basic Tips #6


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

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

Proudly presents:
Visual Basic

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


*1. FINDING THE ADO DATA CONTROL                 
        
VB 6.0 introduces an enhanced data control named the ADO data 
control. As you might expect, this control--with the addition of 
ADO--is much more flexible than the previous data control. 
Specifically, the enhanced control allows you to create a 
connection to a database quickly using ADO. 

By default, the older data control is on the Toolbox for backward 
compatibility; the new ADO data control isn't. To add this 
control to the Toolbox, open the Components dialog box by 
pressing Ctrl-T. Next, select MS ADO Data Control 6.0 (OLEDB), 
click Apply, and then click Close. When you return to the VB 
Project window, check your Toolbox for the Adodc (ADO data 
ontrol) icon.


*2. RETURNING A BIMONTHLY INTEGER                 
        
Lots of us do business in bimonthly increments--which means we 
divide our year into two segments per month, for a total of 24 
segments. You can easily track these segments using the 
following function: 

Function bimonthly(datestring As Date) 
Dim d As Integer, m As Integer, s As Integer 
d = Format(datestring, "d") 
m = Format(datestring, "m") 
If d > 15 Then 
    s = m * 2 
Else 
    s = m * 2 - 1 
End If 
Debug.Print s 
End Function 

where datestring is the date value in question. Of course, you'll
want to replace the Debug.Print statement with a more appropriate
task for returning the results. 

The first two lines of this function assign the day and month 
values to the variables d and m. The If...Else statement then 
determines whether d is in the first or last half of the month. 
If d is in the last half, the first conditional action 

s = m * 2 

multiplies the month integer (1 through 12) by 2. If d is in the 
first half of the month, the Else action 

s = m * 2 - 1 

multiplies the month integer by 2 and then subtracts 1.


*3. VIEWING CODE                 
        
You may not be aware of it, but VB allows you to organize the 
code in your module. The default view is Full Module View and 
displays all the procedures in a module. You may have to scroll 
through the code to see it all, but it's all easily accessible 
from one window. VB separates the functions with a line, so you 
can quickly see where one function ends and the next begins. The 
alternative to the default is Procedure View mode, which displays
only one function at a time. To switch between the two modes, 
simply click the appropriate button at the bottom left of the 
module window.


*4. AVOIDING THE ENDLESS LOOP                 
        
VB offers several looping structures: Do...Loop and While...Wend,
to name a couple. These structures repeat a task until a 
condition is met. Unfortunately, these loops can hang your 
application, if you inadvertently create an endless loop by 
creating an exiting condition that can never be met. It's easier 
than you might think! For instance, the following code creates 
a problem: 

Dim iCount As Integer, iInterval As Integer 
iCount = 1 
iInterval = 2 
Do Until iCount = 100 
    Debug.Print iCount 
    iCount = iCount + iInterval 
Loop 

Our condition looks for a very exact condition--that iCount equal 
100. However, iCount might never equal 100. You might not realize 
this at first, because it seems like a legitimate condition. But, 
if you run this code, you'll see that it cycles endlessly--or at 
least until it creates an overflow error. (To break the loop, 
simply press Ctrl-Break.) 

The correction to this loop's logic is simple. Replace the 
current Do...Until statement with 

Do Until iCount >=100 

The addition of the greater than sign makes all the difference 
in the world. Once iCount reaches the value 99, the loop stops 
because the next iCount value is 101--which is, of course, 
greater than 100. You might be surprised at how easy making a 
simple mistake like this is, and how difficult it can be to find!


*5. SETTING LIBRARY PRIORITIES                 
        
If you plan to use a class from another model, you must reference 
that library by choosing References from the Project menu. Once 
you've referenced more than one library, it's possible to create 
a conflict when using a class--if more than one library uses the 
same class. VB doesn't know which class to use. 

There are two ways to resolve a class conflict. First, when 
setting a reference to a library, you can set the priority. 
You'll find two priority buttons in the References dialog box. 
After setting a reference, you can move that reference up or 
down in the hierarchy. 

Perhaps the safer method is simply to define the library in your 
code. For instance, the following statement references the Excel 
application, leaving VB no doubt as to which library to reference: 

Dim objExcel As Excel.Application 

It's a good idea to declare the library explicitly when working 
with common class names.


*6. ELIMINATING UNNECESSARY IF STATEMENTS                 
        
If you're using an If statement to assign a True or False value, 
then you're working too hard. For instance, the following If 
statement assigns the True or False value to the variable 
booResult, depending on the value of iTest: 

If iTest = 3 Then 
	booResult = True 
Else 
	booResult = False 
End If 

The same thing can be done much more quickly. 
Simply use the statement 

BooResult = iTest = 3 

If iTest equals 3, the statement is True and booResult will equal 
True. If iTest doesn't equal 3, booResult will equal False.


*7. UNDERSTANDING OPTION EXPLICIT                 
        
By default, all modules contain the Option Explicit statement, 
which determines the declaration mode explicit. When in explicit 
mode, you must declare a variable to use it, which is a good 
idea in general. Using explicit mode, you will catch most errors 
before they crash your code. 

If you misspell a variable, explicit mode will point it out 
because (most likely) that variable hasn't been declared. For 
instance, if you declare the variable iValue but then refer to 
that variable in your code as iVale, VB will stop and highlight 
the line of code that contains the misspelled variable when you 
attempt to compile the code. At this pint, VB won't tell you 
exactly what's wrong with the code, but at least you've 
narrowed down the problem to one specific line. Without this 
feature, you might spend some serious time tracking down a 
simple misspelled variable. 

If you don't like explicit mode, you can turn it off. Select 
Options from the Tools menu, click the Editor tab, and then 
deselect the Require Variable Declaration option in the Code 
Settings section. If you prefer to work with this feature off, 
you can still take advantage of the Option Explicit environment 
in a module. Simply enter the statement 

Option Explicit 

in the module's General Declarations section.


*8. CHECKING FOR THE WEEKEND                 
        
There are numerous tests for determining whether a date falls 
during the business week or the weekend. This quick expression 
returns True if the date is a weekend date and False if it 
falls during the business week (Monday through Friday). 

booDate = Weekday(dDate) Mod 6 = 1 

Just remember, this function assumes that Sunday is the first 
day of the week.


*9. USING WORD FORMATS                 
        
Applying professional-looking formats to a document is fairly 
easy--even from inside VB. The following code will open a new 
document in Word from your VB application: 

Dim objWord As New Word.Application 

objWord.Visible = True 
objWord.Documents.Add 

At this point, you can work in Word manually. Or you can send 
text from VB to Word using the statement 

objWord.Selection.TypeText "yourtext" 

where yourtext represents the text you want to format. If you 
want to select the entire document, use the statement 

objWord.Selection.WholeStory 

To modify the font size, use the statement 

objWord.Selection.Font.Size = sizeinteger 

where sizeinteger is the font size you require. 

Of course, for this code to work, you must reference the Word 
library. To do so, choose References from the Project menu and 
select Microsoft Word 8.0 Object Library. Once you do so, use 
the Object Browser to find the dozens of properties and methods 
you can use to alter your document. 

Be sure to close your function with the 

ObjWord = Nothing 

statement.


*10. USING SPLIT                 
        
If you're still struggling with string parsing, you can relax. 
VB 6.0 introduces a new function--Split()--which makes parsing 
much easier. The Split() function returns a one-dimensional 
array containing a specified number of substrings. 
It uses the syntax 

Split(expression[, delimiter[, count[, compare]]]) 

where expression is a string expression containing substrings 
and delimiters. The delimiter argument is optional; if you omit 
it, the function uses the space character as the delimiter. The 
count argument is optional and represents the number of 
substrings to be returned; the value -1 returns all substrings. 
Finally, the compare argument is also optional; it's a numeric 
value that determines the type of comparison when evaluating 
the substrings. 

The following function is just a simple example of how you can 
use this new function to make short work of your parsing tasks: 

Function SplitString() 
Dim sSet As String, iCounter As Integer 
Dim arrSet() As String 

sSet = "We,parsed,this,string" 

arrSet = Split(sSet, ",") 

For iCounter = LBound(arrSet) To UBound(arrSet) 
  MsgBox arrSet(iCounter) 
Next iCounter 

End Function 

The Split() function parses the different substrings from the 
string "We,parsed,this,string"--using the comma character as the 
delimiter. Then, we displayed each string in a simple message box.
