Solved On entry of two digits (no more, no less) in intField1, immediately set focus to txtField2 ? (1 Viewer)

alan2013

Registered User.
Local time
Today, 00:40
Joined
Mar 24, 2013
Messages
69
On a continuous Form, I'd like - if possible - to be able enter two digits in intField1 and - without having to hit the Tab key, or click on txtField2, etc - to have the focus set to txtField2. Is that achievable.......in VBA or otherwise (maybe via a Validation Rule) ? I've been trying the code below with After_Update, Before_Update, On_Dirty... but can't get anything to work. Can it be done ?

If Len(Me.intField1.Value) = 2 Then
Me.txtField2.SetFocus
End If
 

Josef P.

Well-known member
Local time
Today, 09:40
Joined
Feb 2, 2023
Messages
850
If you are using an input mask, you could set AutoTab to True.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:40
Joined
Feb 28, 2001
Messages
27,348
If that is a text box for input, you can look at the "OnChange" event as a way to note that two non-blank characters have been entered, because the OnChange event triggers for each keystroke. You can check the control.Text property each time a key is entered to see what is in the box. Then you can decide what to do after each keystroke, including a .SetFocus to something else. The down side of this is that your user could backspace to remove the first character, but the second character would not be correctable so easily.
 

alan2013

Registered User.
Local time
Today, 00:40
Joined
Mar 24, 2013
Messages
69
Thanks for your response, Josef. There's no input mask, as intField1 is Number data type. One can't set an input mask on a field of Number data type, right ?
 

Josef P.

Well-known member
Local time
Today, 09:40
Joined
Feb 2, 2023
Messages
850
e.g: textbox with input mask 00;0;_

/edit: see attachment
 

Attachments

  • NumberAndAutoTab.zip
    19.6 KB · Views: 65
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:40
Joined
Feb 19, 2002
Messages
43,534
The code needs to reference the .Text property and the code needs to go into the control's Change Event.
Code:
If Len(Me.intField1.Text) = 2 Then
    Me.txtField2.SetFocus
End If

Each text control has THREE buffers
.OldValue - whatever is in the saved version of the record. Always Null for a new record.
.Text - the typing buffer. As each character is typed, the character is placed into this buffer and the Change event is called.
.Value - after data entry is complete, and focus has moved, the .Text buffer is copied to the .Value buffer.

Between the form's BeforeUpdate event and its AfterUpdate event, data is copied from the .Value buffer to the .OldValue buffer

WHENEVER you use the Change event, you need to reference the .Text buffer to see the live typing BEFORE it gets moved to the .Value buffer. If you reference the .Value buffer from the Change event, you will see what was there previously, usually the same as the .OldValue unless the user has modified the data in the control more than once before saving. In older versions of Access, you would get an error if you referenced the .Text property from any event except the Change event. Newer versions seem to be more forgiving of that error. But they can't detect the mistake of using .Value in the Change event instead of .Text because you may actually want to refer to the .Value property.
 
Last edited:

alan2013

Registered User.
Local time
Today, 00:40
Joined
Mar 24, 2013
Messages
69
Thanks ever so much, Pat. That worked really well with my numeric field. Josef and The_Doc_Man : thanks to you too; your suggestions have resolved some other elements I'm working on.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:40
Joined
Feb 19, 2002
Messages
43,534
You're welcome. Most people don't know the difference between the three buffers or how to use them.
 

Users who are viewing this thread

Top Bottom