limit textbox to blank or exactly 4 numberic digits

G

Guest

This works

Private Sub txtbxPhonePost_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txtbxPhonePost.Value = "" Then
usfDemo.cmbxDOBMonth.SetFocus
Else
If IsNumeric(usfDemo.txtbxPhonePost.Text) = False Then
MsgBox "This box must be blank or contain exactly 4 numbers!",
vbOKOnly
Else
If Len(usfDemo.txtbxPhonePost.Text) <> 4 Then
MsgBox "This box must be blank or contain exactly 4
numbers!", vbOKOnly
End If
End If
End If
End Sub

Is there a better way?
 
G

Guest

I added this below the message box:

MsgBox "This box must be blank or contain exactly 4 numbers!", vbOKOnly
If VBAOk Then
usfDemo.txtbxPhonePost.Text = ""
usfDemo.txtbxPhonePost.SetFocus
End If

However, the textbx doesn't clear or select?
 
D

Dave Peterson

maybe

If VBOk Then
I added this below the message box:

MsgBox "This box must be blank or contain exactly 4 numbers!", vbOKOnly
If VBAOk Then
usfDemo.txtbxPhonePost.Text = ""
usfDemo.txtbxPhonePost.SetFocus
End If

However, the textbx doesn't clear or select?
 
D

Dave Peterson

But since you're not giving the user a choice, why bother checking the response?

msgbox "This box must be blank or contain exactly 4 numbers!", vbOKOnly
usfDemo.txtbxPhonePost.Text = ""
usfDemo.txtbxPhonePost.SetFocus
 
T

tissot.emmanuel

Hi,

Private Sub txtbxPhonePost_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 'Numeric keys only
End Sub

Private Sub txtbxPhonePost_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With txtbxPhonePost
If Len(.Text) > 0 And Len(.Text) <> 4 Then
MsgBox "This box must be blank or contain exactly 4 numbers!",
vbOKOnly
.Text = vbNullString'Clear the textbox
Cancel = True'Leave the focus on the textbox
End If
End With
End Sub

Is it ok?

Best regards from France,

Manu/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top