
by Deborah Kurata; InStep Technologies, Inc.; www.insteptech.com
Sometimes when you display a form, only some of the controls appear. After a pause, the
remaining controls appear. Such partial painting doesn't look professional. (Fortunately,
this problem is much less apparent in VB 5.0 because of dramatic improvements in screen
painting.)
To avoid partially painted windows when showing a non-modal form, use the following
code:
frmPerson.Show vbModeless
frmPerson.Refresh
The Refresh method will ensure that the form repainting is complete before executing
any other code in the routine.

by Gerardo G. Villeda,
www.netreach.net/~gvilleda
Starting with Visual Basic 4.0, you could define optional parameters. There was only
one problem: They could be only of type Variant. With VB 5.0, you can define typed
optional parameters. However, you must be careful when doing so, because you can't check
whether a typed optional parameter was received. Consider this sample code:
Public Sub SubX(Optional b As Boolean)
If IsMissing(b) Then
MsgBox "b is missing"
Else MsgBox "b is not missing"
End If
End Sub ... 'Call SubX with no parameters SubX
You'd expect to see a message box indicating that b is missing, but no box appears. The
reason lies in the definition of IsMissing: "Returns a Boolean value indicating
whether an optional Variant argument has been passed to a procedure." If you don't
use a Variant argument, IsMissing won't provide the expected value.
A typed optional parameter is never missing; it's always set to the default value for
each type (False for Boolean parameters, 0 for numbers and zero-length strings).
Another option is to add the default value in the declaration of the procedure, as
follows:
Public Sub SubX(Optional i As Integer = 1)

From John Slaughter
If you use random numbers to a large extent, you probably get tired of always having to
put in Randomize and then the equation. This simple subroutine handles both chores for
you. First, put the following routine in your project's main module.
Public Function GenRndNumber(Upper As Integer, Lower As Integer) As Integer
Randomize
GenRndNumber = Int((Upper - Lower + 1) * Rnd + Lower)
End Function
To get a random number between 99 and -99, just enter:
RandomNumber = GenRndNumber(99, -99)
You can get a random letter between "A" and "M" with this:
RandomLetter = Chr$(GenRndNumber(asc("M"), asc("A")))
You can eliminate the middle of a range by putting the call to GenRndNumber inside a DO
Loop. The following will get a random number from 50 to 99 or -50 to -99.
'Initialize the number to be generated within the area you want excluded.
RandomNumber = 0
Do Until Abs(RandomNumber) > 49
RandomNumber = GenRndNumber (99, -99)
Loop
Be sure to declare RandomNumber and RandomLetter as appropriate. This procedure has a
minor benefit for anyone who uses a lot of calls to the random number generator. It
actually generates slightly less machine code than calls to the RND function. While not
generally a concern nowadays, there may be times when a programmer will need to find a way
to cut down on the amount of generated machine code.

From Michael C. Amundsen [http://www.amundsen.com]
You can use the ParamArray keyword in the declaration line of a method to create a
subroutine or function that accepts an arbitrary number of parameters at runtime. For
example, you can create a method that will fill a list box with some number of items even
if you do not know the number of items you will be sent. Add the method below to a form:
Public Sub FillList(ListControl As ListBox, ParamArray Items())
'
Dim i As Variant
'
With ListControl
.Clear
For Each i In Items
.AddItem i
Next
End With
'
End Sub
Note that the ParamArray keyword comes BEFORE the parameter in the declaration line.
Now add a list box to your form and a command button. Add the code below in the
"Click" event of the command button.
Private Sub Command1_Click()
'
FillList List1, "TiffanyT", "MikeS", "RochesterNY";
'
End Sub

From Michael C. Amundsen [http://www.amundsen.com]
If you're using an ODBC connection to your database, you can ease the process of
installing the application on workstations by using the FileDSN (data source name) instead
of the more-common UserDSN. You define your ODBC connection as you normally would with
UserDSNs. However, the resulting definition is not stored in the workstation registry.
Instead it gets stored in a text file with the name of the DSN followed by
".dsn" (i.e. "MyFileDSN.dsn"). The default folder for all FileDSNs is
"c:\program files\common files\Odbc\data sources". Now, when you want to install
the VB application that uses the FileDSN, all you need to do is add the FileDSN to the
Install package and run the install as usual. No more setting up DSNs manually!
NOTE: FileDSNs are available with ODBC 3.0 and higher.

From Michael C. Amundsen [http://www.amundsen.com]
You can create your own VB Templates quickly and easily. If you find that you are
adding the same routines to your forms, classes, BAS modules, etc. you can build generic
versions and place them in the Templates folder tree of VB5. By placing the coding module
(frm, bas.cls, etc.) in the proper subfolder of the Templates folder, you'll see the new
item appear whenever you select the Add... dialog box. You can add as many controls,
library references, and lines of code as you wish to the templates.
CAUTION: If you uninstall VB5, you may loose your Templates folder and all its contents.
Be sure to keep a secured copy of all your template files in a safe location.

From Dan Newsome, D&D Information
Professionals
You can use code like the following to open a browser to your homepage. Modify
filenames, paths, and URLs as necessary to match the values on your system.
Dim FileName As String, Dummy As String
Dim BrowserExec As String * 255
Dim RetVal As Long
Dim FileNumber As Integer
Const SW_SHOWNORMAL = 1 ' Restores Window if Minimized or
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As _
String) As Long
'Code ---------
BrowserExec = Space(255)
FileName = "C:\temphtm.HTM"
FileNumber = FreeFile() ' Get unused file number
Open FileName For Output As #FileNumber ' Create temp HTML file
Write #FileNumber, "& <\HTML>" ' Output text
Close #FileNumber ' Close file
' Then find the application associated with it.
RetVal = FindExecutable(FileName, Dummy, BrowserExec)
BrowserExec = Trim$(BrowserExec)
' If an application is found, launch it!
If RetVal >= 32 Or IsEmpty(BrowserExec) Then ' Error
Msgbox "Could not find a browser"
Else
RetVal = ShellExecute(frmMain.hwnd, "open", BrowserExec, _
"www.myurl.com", Dummy, SW_SHOWNORMAL)
If RetVal >= 32 Then ' Error
Msgbox "Web Page not Opened"
End If
End If
Kill FileName ' delete temp HTML file

From Bryan Shoemaker
You can't increment a vertical scroll bar's value--a fact that can become annoying. For
example, start a new project and place a text box and a
vertical scroll bar on the form. Place the vertical scroll bar to the right of the text
box and assign their Height and Top properties the same values. Assign the vertical scroll
bar a Min property value of 1 and a Max value of 10. Place the following code in the
vertical scroll bar's Change event:
Text1.Text = VScroll1.Value
Now press [F5] to run the project. Notice that if you click on the bottom arrow of the
vertical scroll bar, the value increases; if you click on the top arrow, the value
decreases. From my perspective, it should be the other way around.
To correct this, change the values of the Max and Min properties to negative values. For
example, end the program and return to the design environment. Change the vertical scroll
bar's Max value to -1 and its Min value to -10. In its Change event, replace the line you
entered earlier with the following:
Text1.Text = Abs(Vscroll1.Value)
Now press [F5] to run the project. When you click on the top arrow of the vertical
scroll bar, the value now increases. Adjust the Height properties of the text box and the
scroll bar so you can't see the position indicator, and your number box is ready to go.

From Nenad Cus Babic
It's very simple to determine the extent of a string in VB. You can do so with WinAPI
functions, but there's an easier way: Use the AutoSize property of a Label component.
First, insert a label on a form (labMeasure) and set its AutoSize property to True and
Visible property to False. Then write this simple routine:
Private Function TextExtent(txt as String) as Integer
labMeasure.Caption = txt
TextExtent = labMeasure.Width
End Function
When you want to find out the extent of some text, simply call this function with the
string as a parameter.
In my case it turned out that the measure was too short. I just added some blanks to the
string. For example:
Private Function TextExtent(txt As String) As Integer
labMeasure.Caption = " " & txt
TextExtent = labMeasure.Width
End Function