ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   limit textbox to blank or exactly 4 numberic digits (https://www.excelbanter.com/excel-programming/390557-limit-textbox-blank-exactly-4-numberic-digits.html)

Shawn

limit textbox to blank or exactly 4 numberic digits
 
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?
--
Thanks
Shawn

Shawn

limit textbox to blank or exactly 4 numberic digits
 
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?

--
Thanks
Shawn


"Shawn" wrote:

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?
--
Thanks
Shawn


Dave Peterson

limit textbox to blank or exactly 4 numberic digits
 
maybe

If VBOk Then

Shawn wrote:

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?

--
Thanks
Shawn

"Shawn" wrote:

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?
--
Thanks
Shawn


--

Dave Peterson

Dave Peterson

limit textbox to blank or exactly 4 numberic digits
 
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



Shawn wrote:

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?

--
Thanks
Shawn

"Shawn" wrote:

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?
--
Thanks
Shawn


--

Dave Peterson

tissot.emmanuel

limit textbox to blank or exactly 4 numberic digits
 
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/

"Shawn" a écrit dans le message de news:
...
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?

--
Thanks
Shawn


"Shawn" wrote:

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?
--
Thanks
Shawn





All times are GMT +1. The time now is 03:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com