Visual Basic Tips #29


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

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

Proudly presents:
Visual Basic

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


*1. USING QUICK VIEW                 
        
If you want to learn about those DLLs you're using, you may 
already have the means installed. The Quick View accessory lists 
all the routines supported by a DLL. All you have to do--if the 
accessory is installed--is right-click the DLL file. You'll find 
DLLs distributed through your system, but most are in your 
Windows and Windows\System folders. For instance, find 
Kernel32.dll (ours is in the System folder). 

If you can't find any DLLs, you may need to unhide them. To do 
so, select Folder Options from the View menu in the Explorer 
window. Next, click the View tab and, under Files And Folders, 
select the Show All Files option. 

Once you've located Kernel32.dll (or another DLL) in the 
Explorer, choose Quick View from the File menu. Or right-click 
Kernel32.dll and choose Quick View from the shortcut menu. Quick 
View will display a report-type look at Kernel32.dll (or the DLL 
you've selected). In this report, you'll find information about 
the DLL. 

If you find undocumented functions, you're free to try them out. 
Just remember, Microsoft doesn't support undocumented functions.


*2. MORE ON QUICK VIEW                 
        
In our previous tip, we showed you how to discover useful and 
undocumented information in DLLs using Quick View. If you can't 
find Quick View on your menu, you must install it. Simply insert 
your Windows CD and choose Start from the taskbar. Next, select 
Settings and then click Control Panel. In the resulting window, 
double-click Add/Remove Programs. When the Add/Remove Programs 
Properties box appears, select the Windows Setup tag and 
highlight Accessories. At this point, click the Details button. 
Now you can check the Quick View box. Click OK twice, and 
Windows will install Quick View.


*3. USING THE AUTO LIST MEMBER LIST                 
        
Memorizing all the different functions and statements--and their 
different arguments, methods, and properties--is nearly 
impossible. In fact, who would even try? Fortunately, you don't 
have to, because VB remembers all the details for you. When 
entering code, the Auto List Members list will display a 
drop-down list of object types, properties, and methods. 
For example, if you enter 

Dim frm As 

the Auto List Members feature will display the different object 
types. At this point, you can select an item from the list, or 
you can give the list more information to narrow its search. If 
you enter F, the list will display the possibilities--those 
items that begin with the letter F. You can continue to enter 
letters until the list finally selects the item you want. At 
this point, you can press Enter or Tab to complete your code. 

These keyboard actions go with the Auto List Members feature: 
- Tab or Ctrl-Enter--Enters the selected item 
- Enter--Enters the selected item and positions the cursor on 
  the next line 
- Spacebar--Enters the selected item and a space character 
- Esc--Closes the list


*4. SORTING ITEMS IN A LIST BOX                 
        
Did you know you can sort the items in a list box by simply 
changing a control property? The list box control's Sorted 
property is set to False by default. That means the control will 
display items in the order they are added by code. If you set 
the property to True, the control will display the items in 
sorted order. 

For the most part, this means that you can enter items in any 
order as far as the code is concerned and still display them in 
sorted order in the control. The following procedure is a 
simple example: 

Private Sub Form_Load() 
lstSort.AddItem "dog" 
lstSort.AddItem "cat" 
lstSort.AddItem "zebra" 
lstSort.AddItem "ant" 
End Sub 

This simple procedure will display the four items (dog, cat, 
zebra, and ant) in that order--that is, unless you set lstSort's 
Sorted property to True. Then, lstSort will display the same 
items in alphabetical order--ant, cat, dog, and zebra.


*5. DISPLAYING THE HOURGLASS MOUSE POINTER                 
        
Processing can tie up any application for a while. When this 
happens, you can alert your users by displaying the hourglass 
mouse pointer. To do so, simply include the statement 

Screen.Mousepointer = vbHourglass 

in your code--right before the lengthy task begins. Your users 
are probably familiar with the hourglass icon already and will 
know that they must stop trying to enter data or otherwise 
interact with the application. Of course, you'll want to let 
your users know when the task is complete so they can return 
to work. To do so, include a second statement: 

Screen.Mousepointer = vbDefault 

This returns the mouse pointer to the default mouse pointer 
after the code that performs the time-consuming task. 

There are several mouse pointer constants. For more information, 
search Help for Mouse Pointer Constants.


*6. COMPARING DATES                 
        
Ever need to know if two dates fall within the same month? Well, 
there's an easy way to compare two dates for this purpose. This 
simple date task can be accomplished with the simple 
procedure below: 

Function MonthCheck(date1 As Date, date2 As Date) 
MonthCheck = DateDiff("m", date1, date2) 
End Function 

If MonthCheck returns a 0, the two dates are in the same month; 
any result other than 0 means the dates aren't in the same month.


*7. DECLARATION SHORTCUT                 
        
Most of the time, we declare variables explicitly by identifying 
the data type. For instance, to declare a variable as an Integer,
you'd use the statement 

Dim i As Integer 

However, there is a shortcut method when declaring a String, 
Integer, Long, Single, Double, or Currency data type: You can 
use the data type's character. The Integer data type's 
character is %. That means the statement 

Dim I% 

accomplishes the same task as our previous statement. Other data
type characters are 

String--$ 
Integer--% 
Long--& 
Single--! 
Double--# 
Currency--@


*8. DELETING ITEMS FROM A LIST BOX                 
        
After selecting items in a multiselect list box, you can delete 
those items with a short procedure. The following procedure 
relies on the control's Select property: 

Private Sub cmdDelete_Click() 
Dim i As Integer 
For i = lstDelete.ListCount - 1 To 0 Step -1 
If lstDelete.Selected(i) Then lstDelete.RemoveItem i 
Next i 
End Sub 

When an item has been selected, the lstDelete.Selected(i) 
property returns True. When this property is True, the If 
statement deletes that item using the statement 

lstDelete.RemoveItem i 

The For loop does the trick by looping through the selected 
items backward. You can't delete the items in numeric order, 
because doing so constantly resets the index values. 

Keep in mind that this procedure doesn't delete the items 
permanently. It only deletes the items until you reload the form.


*9. USING SQL BETWEEN                 
        
Did you realize that you can't use the Between...And operator in
your VB code? Between...And is a SQL operator and would be a 
great addition, if it were available. Anytime you need to 
express such a condition, you'll have to rely on logical 
operators such as >= and <=. For instance, let's suppose you'd 
like a simple While loop to continue processing as long as i in 
an Integer between the values 2 and 5. If you could use 
Between...And, you'd use the statement 

While i Between 2 And 5 

Since you can't use Between...And in this way, you'll need 
the statement 

While i >= 2 And i <= 5 

Our second statement accomplishes the same thing using logical 
operators. (Of course, you can use Between...And in any 
legitimate SQL statement or clause.)


*10. ELIMINATING A LITTLE CODE                 
        
There are several ways you can optimize code by eliminating 
unnecessary statements. My favorite is to delete declaration 
statements for unnecessary integers. For instance, the following 
procedure is fairly common--it declares a variable, assigns a 
value to it, and then returns it. 

Function NumberTest(number As String) As Boolean 
Dim boo As Boolean 
boo = IsNumeric(number) 
NumberTest = boo 
End Function 

This procedure declares the variable boo as a Boolean data type, 
assigns the result of a function to boo, and then returns the 
value of boo as the function results. There's nothing wrong with 
this code--it works fine. But we can eliminate two lines. The 
next procedure accomplishes the same end: 

Function NumberTest(number As String) As Boolean 
NumberTest = IsNumeric(number) 
End Function 

The difference is that we've cut out an unnecessary variable. The 
function is already declared as a Boolean data type, so we really 
don't need boo at all. 

You'll find many developers who insist that such shortcuts are 
"bad" programming. And you might not want to eliminate boo from 
your procedure for several reasons. First, if you need to refer 
to boo more than once, you should retain the variable. Second, 
the variables make the procedure more readable. However, if your 
procedure is short and sweet, eliminating a few extra lines 
shouldn't be a problem.
