Developers Dev Header Right.gif (1326 bytes)


bullet_brown.gif (945 bytes)Developer Home
bullet_brown.gif (945 bytes)Tricks and Tips
Page 1
Page 2
Page 3
Page 4
bullet_brown.gif (945 bytes)Submissions
bullet_brown.gif (945 bytes)Developer Links
bullet_brown.gif (945 bytes)Graphic Art Links

bullet_brown.gif (945 bytes)Home Page

Developers

Tricks and Tips

PAGE 2 - Click on 'Tricks and Tips' to return to the index page.

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

3rd October 1998
Anonymous

Tricks and Tips Index                  Top of Page

 

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.

April 27, 1998
Submitted by Michael C. Amundsen.

Tricks and Tips Index                  Top of Page

 

Creating Your Own VB5 Templates

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.

April 20, 1998
Submitted by Michael C. Amundsen.

Tricks and Tips Index                  Top of Page

 

Increment and decrement dates with the [+] and [-] keys

If you've ever used Quicken, you've probably noticed a handy little feature in that program's date fields. You can press the [+] key to increment one day, [-] to decrement one day, [PgUp] to increment one month, and [PgDn] to decrement one month. In this tip, we'll show you how to emulate this behaviour with Visual Basic.

First, insert a text box on a form (txtDate). Set its text property to "" and its Locked property to TRUE.

Now place the following code in the KeyDown event:

Private Sub txtDate_KeyDown(KeyCode As Integer, Shift As Integer)
'
' 107 = "+" KeyPad
' 109 = "-" KeyPad
' 187 = "+" (Actually this is the "=" key, same as "+" w/o the shift)
' 189 = "-"
' 33 = PgUp
' 34 = PgDn
'
Dim strYear As String
Dim strMonth As String
Dim strDay As String
'
If txtDate.Text = "" Then
  txtDate.Text = Format(Now, "m/d/yyyy")
  Exit Sub
End If
'
strYear = Format(txtDate.Text, "yyyy")
strMonth = Format(txtDate.Text, "mm")
strDay = Format(txtDate.Text, "dd")
'
Select Case KeyCode
  Case 107, 187 ' add a day
    txtDate.Text = Format(DateSerial(strYear, strMonth, strDay) + 1, "m/d/yyyy")
  Case 109, 189 ' sbutract a day
    txtDate.Text = Format(DateSerial(strYear, strMonth, strDay) - 1, "m/d/yyyy")
  Case 33 ' add a month
    txtDate.Text = Format(DateSerial(strYear, strMonth + 1, strDay), "m/d/yyyy")
  Case 34 ' subtract a month
    txtDate.Text = Format(DateSerial(strYear, strMonth - 1, strDay), "m/d/yyyy")
End Select
'
End Sub

The one nasty thing about this is that if you have characters that are not the characters usually in a date (i.e., 1-9, Monday, Tuesday, or /) you get errors in the format command. To overcome this, I set the Locked property to True. This way, the user can't actually type a character in the field, but the KeyDown event still fires.

March 2, 1998
By Mike Coleman.

Tricks and Tips Index                  Top of Page

 

Add Dithered Backgrounds to your VB Forms

Ever wonder how the SETUP.EXE screen gets its cool shaded background colouring? This colour shading is called dithering, and you can easily incorporate it into your forms. Add the following routine to a form:

Sub Dither(vForm As Form)
Dim intLoop As Integer
   vForm.DrawStyle = vbInsideSolid
   vForm.DrawMode = vbCopyPen
   vForm.ScaleMode = vbPixels
   vForm.DrawWidth = 2
   vForm.ScaleHeight = 256
   For intLoop = 0 To 255
      vForm.Line (0, intLoop)-(Screen.Width, intLoop - 1), RGB(0, 0, 255 -intLoop), B
   Next intLoop
End Sub

Now, add to the Form_Activate event the line

Dither ME

This version creates a fading blue background by adjusting the blue value in the RGB function. (RGB stands for Red-Green-Blue.) You can create a fading red background by changing the RGB call to

RGB(255 - intLoop, 0, 0).

April 06, 1998
By: Barron Anderson, Micron Electronics, Inc.

Tricks and Tips Index                  Top of Page

 

Selecting all text when a TextBox gets focus

When you present the user with default text in a TextBox, you'll often want to select that text when the TextBox gets focus. That way, the user can easily type over your default text.

The function below will do the trick. The first click on the TextBox will select all the text; the second click will place the cursor.

Public Sub TextSelected()
Dim i As Integer
Dim oMyTextBox As Object

Set oMyTextBox = Screen.ActiveControl
     If TypeName(oMyTextBox) = "TextBox" Then
          i = Len(oMyTextBox.Text)
          oMyTextBox.SelStart = 0
          oMyTextBox.SelLength = i
     End If
End Sub

Just add the function to your project and call it from the TextBox's GotFocus event.

Private Sub Text1_GotFocus()
     TextSelected
End Sub

December 8, 1997
By Christopher Buteau.

Tricks and Tips Index                  Top of Page

 

Dealing with Null strings in Access database fields

By default Access string fields contain NULL values unless a string value (including a blank string like "") has been assigned. When you read these fields using recordsets into VB string variables, you get a runtime type-mismatch error. The best way to deal with this problem is to use the built-in & operator to concatenate a blank string to each field as you read it. For example,

Dim DB As Database
Dim RS As Recordset
Dim sYear As String

Set DB = OpenDatabase("Biblio.mdb")
Set RS = DB.OpenRecordset("Authors")
sYear = "" & RS![Year Born]

November 17, 1997
by Pradeep Arora.

Tricks and Tips Index                  Top of Page

 

 

If you have any queries email:
developer@may-computing.com