setfocus

DharmeshKP

New Member
Joined
Feb 15, 2020
Messages
34
Office Version
  1. 2007
Platform
  1. Windows
I have user form for patient data entry
I want to validate phone to to 10 digit numeric form
I havel following dode
I run fine
No error is there
Only problem is if wrong entry done, it removes it and curser moves to next textbox
I need curser in same text box so that user is forced to fill proper data
VBA Code:
Private Sub TextBox8_afterupdate()
If Not IsNumeric(TextBox8.Value) Or Len(TextBox8.Text) <> 10 Then
TextBox8.Text = ""
TextBox8.SetFocus
End If


End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Your code is correct but in the incorrect event procedure
Delete TextBox8_afterupdate

If TextBox9 is the next textbox
VBA Code:
Private Sub TextBox9_Enter()
    If Not IsNumeric(TextBox8.Value) Or Len(TextBox8.Text) <> 10 Then
        TextBox8.Text = ""
        TextBox8.SetFocus
    End If
End Sub
 
Upvote 0
Thanks for reply
after changing code curser sets in next textbox ie textbox10
even more troublesome as textbox 9 is missed altogather
Pl suggest some solution
 
Upvote 0
It works for me, but my form is very simple
- something must be different in your form

What are your AutoTab property settings for TextBox8 and TextBox9 ?
 
Upvote 0
Autotab property in false for both of them. I tried after turning true. It do not work
 
Upvote 0
How about
VBA Code:
Private Sub TextBox8_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   With Me.TextBox8
      If Not IsNumeric(.Value) Or Len(.Value) <> 10 Then
         .Value = ""
         Cancel = True
      End If
   End With
End Sub
 
Upvote 0
VBA Code:
If Not IsNumeric(TextBox8.Value) Or Len(TextBox8.Text) <> 10 Then
I know you have an answer to your question but it uses the above line of code from your original post. I would like to suggest that IsNumeric is a terrible function to use to decide if an an entry into a TextBox is a number or not. See my mini-blog article here for the reason why...

Thinking About Using VBA's IsNumeric Function? Read this first.

My suggestion would be to use this in place of the above code line...
VBA Code:
If Not TextBox8.Text Like "##########" Then
 
Upvote 0
Thanks for your suggestion
Would you like to suggest simillar thing for email validation
How to write for email validation
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top