
		       Nod Programming Inc. VB Help Index
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
This is intended for free use.  The code here is for various skill levels, 
anyone from beginers to advanced programers can use these.  Do what you wish 
with the code it is free for you to use and manipulate!

****************************************************************************

Use FreeFile to Prevent File Open Conflicts:

Both Access and VB let you hard code the file numbers when using the File
Open statement. For example:

     Open "myfile.txt" for Append as #1
     Print #1,"a line of text"
     Close #1

The problem with this method of coding is that you never know which file
numbers may be in use somewhere else in your program. If you attempt to use
a file number already occupied, you'll get a file error. To prevent this
problem, you should always use the FreeFile function. This function will
return the next available file number for your use. For example:

     IntFile=FreeFile()
     Open "myfile.txt" for Append as #intFile
     Print #intFile,"a line of text"
     Close #intFile

****************************************************************************

Confirm Screen Resolution:

Here's a great way to stop the user from running your application in the
wrong screen resolution. First, create a function called CheckRez:

Public Function CheckRez(pixelWidth As Long, pixelHeight As Long) As Boolean
    '
    Dim lngTwipsX As Long
    Dim lngTwipsY As Long
    '
    ' convert pixels to twips
    lngTwipsX = pixelWidth * 15
    lngTwipsY = pixelHeight * 15
    '
    ' check against current settings
    If lngTwipsX <> Screen.Width Then
        CheckRez = False
    Else
        If lngTwipsY <> Screen.Height Then
            CheckRez = False
        Else
            CheckRez = True
        End If
    End If
    '
End Function

Next, run the following code at the start of the program:

    If CheckRez(640, 480) = False Then
        MsgBox "Incorrect screen size!"
    Else
        MsgBox "Screen Resolution Matches!"
    End If

****************************************************************************

Quick Text Select On GotFocus:

When working with data entry controls, the current value in the control
often needs to be selected when the control received focus. This allows the
user to immediately begin typing over any previous value. Here's a quick
subroutine to do just that:

Public Sub FocusMe(ctlName As Control)
    
    With ctlName
        .SelStart = 0
        .SelLength = Len(ctlName)
    End With
    
End Sub

Now add a call to this subroutine in the GotFocus event of the input
 controls:

Private Sub txtFocusMe_GotFocus()
    Call FocusMe(txtFocusMe)
End Sub

****************************************************************************

Use ParamArray to Accept an Arbitrary Number of Parameters:

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
	
****************************************************************************
 
Use FileDSNs to ease ODBC Installs:

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.

****************************************************************************

Opening a browser to your homepage

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 =3D 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 =3D Space(255)
FileName =3D "C:\temphtm.HTM"

FileNumber =3D FreeFile()     ' Get unused file number

Open FileName For Output As #FileNumber  ' Create temp HTML file
  Write #FileNumber, "<HTML> <\HTML>"  ' Output text
Close #FileNumber   ' Close file

' Then find the application associated with it.
  RetVal =3D FindExecutable(FileName, Dummy, BrowserExec)
  BrowserExec =3D Trim$(BrowserExec)
  ' If an application is found, launch it!
  If RetVal <=3D 32 Or IsEmpty(BrowserExec) Then ' Error

    Msgbox "Could not find a browser"

  Else
    RetVal =3D ShellExecute(frmMain.hwnd, "open", BrowserExec, _
      "www.myurl.com", Dummy, SW_SHOWNORMAL)
    If RetVal <=3D 32 Then        ' Error
      Msgbox "Web Page not Opened"
    End If
  End If

Kill FileName  ' delete temp HTML file

****************************************************************************

Creating a incrementing number box

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.

****************************************************************************

Measuring a text extent:

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

****************************************************************************

Importing Registry settings

You can use just a few lines of code to import Registry settings. If you
have an application called myapp.exe and a Registry file called myapp.reg,
the following code will put those settings into the Registry without
bothering the user.

Dim strFile As String
strFile =3D App.Path & "\" & opts.AppExeName & ".reg"
If Len(Dir$(strFile)) > 1 Then
    lngRet =3D Shell("Regedit.exe /s " & strFile, vbNormalFocus)
End If

****************************************************************************

Labeling your forms:

Do you have a ton of screens in your application? Do you also have plenty
of users who want to "help you" by pointing out buttons that are one twip
out of place? Sometimes it's hard to know what screen users are talking
about when they're trying to communicate a problem--particularly if they're
in a different location than you.

To reduce the pain of this process, I add a label (called lblHeader) to the
top of each GUI window, nominally to hold start-up information for users
when they first open the window. You can also use this label to hold the
name of the window the user is looking at, by using the following code:

Private Sub Form_Load()
    SetupScreen me
End Sub


Public SetupScreen (frm as Form)
    ' Do other set-up stuff here (fonts, colors).
    HookInFormName frm
End Sub

Public Sub HookInFormName(frm As Form)
    ' The Resume Next on Error allows forms that do not use a standard
    ' header label to get past this.
    On Error Resume Next
    frm.lblHeader.Caption = "(" & frm.Name & ") " & frm.lblHeader.Caption
End Sub

Note that if you don't want to use a label, that you can also use code like

    frm.print frm.name

to print to the back of the window itself.

****************************************************************************

    	               End of Help 5 of how many I do!!