Visual Basic Tips #48


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

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

Proudly presents:
Visual Basic

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


*1. CALCULATORS ONLINE

Normally, we like to share expressions and formulas that you 
can use in Visual Basic. However, we've found a Web site that 
may make some of your work unnecessary. The Calculators On-Line
Center offers more than 5,000 Web calculators. You'll find 
calculators to handle all sorts of tasks, from a lye 
calculator (for making soap) to a capital gains calculator.

You can easily convert and incorporate these calculators into 
your VB projects using Microsoft Web components.

Calculators On-Line Center

http://www-sci.lib.uci.edu/HSG/RefCalculators.html


*2. MORE ON CALCULATIONS

In our previous tip, we told you about a Web site with 
downloadable calculators for all kinds of tasks. However, most 
of the time, you'll have to rely on your own expressions to get 
the results you need. Occasionally your expressions return 
errors--and there are several reasons why Visual Basic can't 
evaluate your expression. Here are a few quick checkpoints to 
review before you start pulling out your hair:

- Make sure you've included the right number of parentheses 
  (each opening parenthesis requires a closing parenthesis). 
- Make sure you've supplied all the required arguments for any 
  functions or procedures. 
- Check all your object and variable references to make sure 
  they're correct.


*3. SPLITTING YOUR SCREEN

Do you sometimes wish you had two monitors and two pairs of 
hands when working in the module window? If you need to view 
different parts of your code at the same time, simply split the 
worksheet into two panes. I find a horizontally split module 
particularly useful when I need to return to the beginning of a 
function to add a declaration. I just hop up to the top pane, 
add the declaration, and then hop back down to the bottom window 
and return to my code. This isn't the only use for a split 
module; it just happens to be my favorite.

If you want a horizontal split, drag down the split box (the 
small rectangle that rests on top of the vertical scroll bar). 
You'll take similar steps to create a vertical split, except 
drag the split box that's to the right of the horizontal scroll 
bar. Once you've split your module into two panes, you can 
scroll either pane to find any section of the same module.


*4. DELETING A SPLIT SCREEN  
  
In our previous tip, we showed you how to split a module into 
two scrollable windowpanes. This tip is particularly useful 
when you're working with a large module. To return your view to 
just one pane, simply remove the split. To do this the hard way,
drag the split bar back to its originating split box. The easier
way to eliminate a split module is to simply double-click the 
split bar.


*5. STEALING FROM HELP

The Help system is full of code fragments that you can put to 
good use, as is or with a few minor adjustments. Before you try 
to reinvent the wheel, do a quick search in Visual Basic's Help 
system for any code that you can use as the basis for your 
current task. When you find something you can put to use, 
highlight those lines, right-click the selection, and choose 
Copy (or press Ctrl-C). Then, switch to a module and click the 
Paste button on the Standard toolbar, or press Ctrl-V. You'll 
probably have to revamp the code a bit, but that's easier than 
starting from scratch.


*6. 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 and still display them in sorted order in the control. 
The following procedure shows a simple example:

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

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


*7. 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 and still display them in sorted order in the control. 
The following procedure shows a simple example:

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

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


*8. 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 
following procedure:

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

If MonthCheck returns 0, the two dates are in the same month; 
any result other than 0 means the dates are not in the same 
month. At first glance, this function appears to return a 
Boolean result, but that's not the case. The result equals the 
number of months between the two dates.

*9. DELETING ITEMS FROM A LIST BOX

After selecting items in a list box, you can delete those items 
with one short procedure, which 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 its trick by looping backward through the 
selected items. 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. In addition, this procedure will work with a multiselect 
list box, but you'll want to use a different event to trigger 
it as the Click event will react as soon as you click the 
first item.


*10. 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 apart from an SQL 
statement. Instead, when you need to express such a condition, 
you'll have to rely on logical operators such as >= and <=. For 
instance, let's take a look at a simple While loop that will 
continue processing as long as i is an Integer between the 
values 2 and 5. If you could use Between...And, you'd use 
this statement:

While i Between 2 And 5

Since you can't use Between...And in this way, you use a 
statement like this:

While i >= 2 And i <= 5

This second statement accomplishes our task using logical 
operators. (Of course, you can use Between...And in any 
legitimate SQL statement or clause.)
