Solved How many digits have I typed? (1 Viewer)

Sun_Force

Active member
Local time
Today, 21:06
Joined
Aug 29, 2020
Messages
318
Is there any way to find out how many digits I have typed in a textbox? (before update)

I'm working on a search form.
I start inputting numbers in a text box and I'd like to move the focus (cursor) to another textbox as soon as the first textbox is filled with 5 digits.

I've tried On_KeyDown, On_KeyPress, On_Change events of the text box but I couldn't make it work mostly because I'm still in the textbox and it's not updated yet.
I also thought of updating the textbox with each key press and then move the cursor to the end of the typed number, but I think it will cause screen flickering.

Any idea is much appreciated.
 
Last edited:

June7

AWF VIP
Local time
Today, 04:06
Joined
Mar 9, 2014
Messages
3,657
Did you try:
Code:
Private Sub Text1_Change()
If Len(Me.Text1.Text) = 5 Then
    Me.Text2.SetFocus
End If
End Sub

Or

Declare a public variable in form header.

Code:
Option Compare Database
Option Explicit
Dim intX As Integer
________________________________

Private Sub Text1_Change()
intX = intX + 1
Debug.Print intX
If intX = 5 Then
    Me.Text2.SetFocus
    intX = 0
End If
End Sub
Does not prevent user clicking back into Text1 at end of previous input and typing more characters. If you want to remove data when textbox gets focus, use its GotFocus event: Me.Text1 = Null.
 
Last edited:

Sun_Force

Active member
Local time
Today, 21:06
Joined
Aug 29, 2020
Messages
318
Declare a public variable in form header.

Code:
Option Compare Database
Option Explicit
Dim intX As Integer
________________________________

Private Sub Text1_Change()
intX = intX + 1
Debug.Print intX
If intX = 5 Then
    Me.Text2.SetFocus
    intX = 0
End If
End Sub

Sorry I should have explained this.
I used this method before posting. But I faced a real problem.

When a user deletes a digit. I can trap delete or backspace buttons in On_Keypress or On_KeyDown and subtract from the variable.
But I couldn't find a way if the user drags and selects two or more digits and delete them.
In this case the value of the variable is not accurate.

If there's nothing to overcome this problem, I can live with it.
Thanks again for your time.

Edit : Sorry I didn't noticed your first solution.
I will try and see how it works.
Thank you
 

Sun_Force

Active member
Local time
Today, 21:06
Joined
Aug 29, 2020
Messages
318
Did you try:
Code:
Private Sub Text1_Change()
If Len(Me.Text1.Text) = 5 Then
    Me.Text2.SetFocus
End If
End Sub

As I had guessed , it always returns 0. Because the textbox is not updated yet.
 

Sun_Force

Active member
Local time
Today, 21:06
Joined
Aug 29, 2020
Messages
318
It worked for me.
I wonder if I'm doing something wrong.
I typed 123456 in the textbox and I receive 6 Null values:

2021-09-21_18-53-35.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:06
Joined
May 7, 2009
Messages
14,156
did you see the .Text property of the textbox?
 

isladogs

CID VIP
Local time
Today, 13:06
Joined
Jan 14, 2017
Messages
15,412
You omitted the .Text from the code. It won't work without that.

The default property is .Value but the value is only set after the field has been saved. So you are currently testing for a non existent value.
That means the length is null
 

Sun_Force

Active member
Local time
Today, 21:06
Joined
Aug 29, 2020
Messages
318
Million thanks to all.
I should have been blind.

Solved
 

June7

AWF VIP
Local time
Today, 04:06
Joined
Mar 9, 2014
Messages
3,657
Code would not prevent user going back into textbox and typing another character. Would have to expand code to prevent more than 5 characters or maybe ValidationRule property could handle.
 

Sun_Force

Active member
Local time
Today, 21:06
Joined
Aug 29, 2020
Messages
318
Code would not prevent user going back into textbox and typing another character. Would have to expand code to prevent more than 5 characters or maybe ValidationRule property could handle.
Thanks for additional warning. But I'm not using it as a data validation tool. It's a search form and I use it just as a replacement for pressing Tab key or moving the mouse and clicking the next textbox.

Again thanks and sorry for missing .text
I should have known when somebody with your reputation says "it works for me", s/he is right and I'm missing some parts of the code.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:06
Joined
Feb 28, 2001
Messages
20,872
For the problem you are addressing (which I understand), two points to consider...

First, until something has been saved/updated, its .Value is generally equal to its .OldValue - but .Text changes with each key (or select/cut) action. Access doesn't usually waste its time re-evaluating something that is still in focus since it could just change again.

Second, the mouse action, when it actually HIGHLIGHTS something in the text box, should give that box focus. Mouse moves that don't actually highlight anything don't affect focus. I would offer the idea that perhaps you don't really care about the contents of the box until the .LostFocus event occurs. Access won't evaluate the value until then, so why should you?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:06
Joined
Feb 19, 2002
Messages
33,281
Forget the other events. You want to use the on Change event of the control and BeforeUpdate of the FORM

Use Me. to qualify your control references and that will also give you intellisense.

In the Change event, this will move focus to whatever control you want to move it to. However as June7 mentioned, it doesn't help with validation or prevent the control from being empty if the control is required.
Code:
If Len(Me.srchRecIDFrom.Text) = 5 Then
    Me.SomeotherControl.Setfocus
End If

You need to use the BeforeUpdate event to do validation and if you want all the validation for a single field in one place, you can't use the control's BeforeUpdate event, you have to use the Form's BeforeUpdate event which can be cancelled to prevent saving an invalid record.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.srchRecIDFrom& "" = "" Then
        msgbox "This field is required."," vbOKOnly
        Me.srchRecIDFrom.SetFocus
        Cancel = True
        Exit Sub
    End If
    If Len(Me.srchRecIDFrom) <> 6 Then
        msgbox "This field must be 5 characters long", vbOKOnly
        Me.srchRecIDFrom.SetFocus
        Cancel = True
        Exit Sub
    End If
End Sub
 

Users who are viewing this thread

Top Bottom