Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick and Easy way to Create a Custom Auto Tab feature to speed data entry

How to

Quick and Easy way to Create a Custom Auto Tab feature to speed data entry

by  SBendBuckeye  Posted    (Edited  )
Problem to solve:

I want to make my user's data entry easier by simulating an auto tab feature with key strokes so they don't have to use the tab key or mouse when data entry is complete for long text strings such as addresses, cities, email addresses, etc.

Easy Solution:

Note: This is ugly but it will work. I used 2 spaces in the example below. One of the benefits of using spaces is that you don't have to strip them off because the system will do that for you automatically.

Note: For a more generalized (but more complex) treatment or if you want to use something other than 2 spaces, see faq702-2099.

Simplest Case - Delimit with 2 spaces

Step 1: Declare a global variable in the declarations section to hold the data entry string which we will build as the user keys in data. We build a global string because the key events to be trapped only return one character at a time.

******************** Begin Code ********************

Public strSaveUserData As String

********************* End Code *********************

Step 2: Initialize the global string on the Got Focus event of your text based control. We want to start over every time we enter the textbox control.

Note: The same global variable can be used for every text base control on your form since the user can only ever be entering data into one control at a time.

******************** Begin Code ********************

Private Sub ControlName_GotFocus()
strSaveUserData = ""
End Sub

********************* End Code *********************

Step 3: Build and test the global string in the Key Press event of your text based control. This will happen multiple times as the user keys in data. We append the current char to our global string and then check for 2 spaces on the end of the global string. If it finds them, we are done entering data for this field, so we send a tab key to go to the next control on the form.

******************** Begin Code ********************

Private Sub ControlName_KeyPress(KeyAscii As Integer)

strSaveUserData = strSaveUserData & Chr$(KeyAscii)

'Send tab key to go to next control if 2 spaces fnd
If Right(strSaveUserData, 2) = " " Then
SendKeys (Chr$(vbKeyTab))
End If

End Sub

********************* End Code *********************

If you are using this in several controls on your form, you would want to put the above keypress code in a separate function as below.

************** Alternate Begin Code ****************

Private Sub ControlName_KeyPress(KeyAscii As Integer)

'Send tab key to go to next control if 2 spaces fnd
If AllDone(KeyAscii) Then
SendKeys (Chr$(vbKeyTab))
End If

End Sub

Private Function AllDone(KeyAscii As Integer) As Boolean

strSaveUserData = strSaveUserData & Chr$(KeyAscii)

'If last 2 = spaces we are done with data entry
If Right(strSaveUserData, 2) = " " Then
AllDone = True
Exit Function
End If

AllDone = False

End Function

********************* End Code *********************

Have fun and enjoy!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top