WHy does it go to the next field when I've asked it to set focus to a specific box

IshBuild

New member
Local time
Today, 15:21
Joined
Sep 20, 2017
Messages
6
Hi all,

I've set the code below on a text box so it only allows the user to enter numbers. However when I enter in letters and hit Tab it brings up the message and instead of setting focus back in the current box it goes to the next field.

How do I stop that from happening?

Code:
Private Sub 
Txt_NewVolume_Exit(Cancel As Integer)
If Me.Txt_NewVolume Like "*[A-Z]*" Or Me.Txt_NewVolume Like "*[@#$%*^&?()<>/\'""!]*" Then
    MsgBox ("Please enter numbers only")
    Me.Txt_NewVolume.Value = vbNullString
    Me.Txt_NewVolume.SetFocus
    
Else

    End If
End Sub

Thanks
 
your are in the Txt_NewVolume_EXIT event.
the final action is to move to the next field.
Dont put the setFocus here.
 
your are in the Txt_NewVolume_EXIT event.
the final action is to move to the next field.
Dont put the setFocus here.

Which Event would be best for this scenario then?

Thanks
 
After the Msgbox, set Cancel to True:

...
...

Msgbox ....
Cancel = True

...
...
 
you can also use BeforeUpdate Event:

you also set Cancel to True, if
you don't want to leave the Textbox.
 
if you use the before update event you need to change your code slightly

If Me.Txt_NewVolume.Text Like "*[A-Z]*" ......

otherwise the code will be working on the original Txt_NewVolume value

Otherwise if you know the maximum numbers to enter use

#### (for 4 numbers)

in the control input mask property then users can only enter numbers - so you don't need a warning message
 

Users who are viewing this thread

Back
Top Bottom