Text Field Entry Question

H

HG

If you have a text field in a database that is 6 characters, is there a way
when entering this field on a form and the 6th position is filled to
automatically tab to the next field on the form?
 
K

Ken Sheridan

Put the following code in the first control's Change event procedure,
substituting the name of your next control for SomeOtherControl:

Dim ctrl As Control

Set ctrl = Me.ActiveControl

If Len(ctrl.Text) = 6 Then
Me.SomeOtherControl.SetFocus
End If

This also allows a user to enter character(s) in the middle of a string of
less than 6 characters and automatically move focus to the next control, but
does not prevent a user entering a character in the middle of an existing
string of 6 characters, however, so to cater for that put the following code
in the first control's KeyPress event procedure:

Const conBACKSPACE = 8
Dim ctrl As Control

Set ctrl = Me.ActiveControl

If Len(ctrl.Text) = 6 Then
If KeyAscii <> conBACKSPACE Then
KeyAscii = 0
End If
End If

This also allows for the use of the backspace or delete key to delete
characters. You will find, however, that if a user wants to substitute a
string for an existing value in the control or a substring in part of a
control of 6 characters then they won't be able to select the string to be
replaced and overtype it. Instead they'd have to delete the selection and
then enter the replacement string.

Ken Sheridan
Stafford, England
 
H

HG

Thank you for the help - very much appreciated.
I am just now getting into using VBA in Access.
 
H

HG

In the statement: If Len(ctrl.Text) = 6 - Is Text suppose to be the Control
Name?
Is Me. stand for the names in the current form?
Thank you for the assistance.
 
K

Ken Sheridan

No, it’s the Text property of the control. You don't need to change any of
the code, just paste it in as is.

Ken Sheridan
Stafford, England

HG said:
In the statement: If Len(ctrl.Text) = 6 - Is Text suppose to be the Control
Name?
Is Me. stand for the names in the current form?
Thank you for the assistance.
 
H

HG

Thank you - worked perfect.

--
HG


Ken Sheridan said:
No, it’s the Text property of the control. You don't need to change any of
the code, just paste it in as is.

Ken Sheridan
Stafford, England
 
Top