Only allowing numbers in textbox

TheMatrixReloaded

New Member
Joined
May 6, 2002
Messages
29
How would I format a textbox to only allow phone #'s? And if the customer enters text the form won't allow them to get to the next form? Here is the code:

intcurrentrow = Sheets("Phone Table").[a65536].End(xlUp).Offset(1).Row
On Error GoTo 1:
Set mycl2 = Sheet3.Columns("A").Find(What:=txtPhoneNumber.Value)
1:
If mycl2 Is Nothing Then
Sheets("Phone Table").Range("A" & intcurrentrow).Value = _
txtPhoneNumber.Value
Sheets("Phone Table").Range("B" & intcurrentrow).Value = _
TxtAddress.Value
End If
Me.Hide
frmPizzaOptions.Show
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
On 2002-05-10 05:18, TheMatrixReloaded wrote:
How would I format a textbox to only allow phone #'s? And if the customer enters text the form won't allow them to get to the next form? Here is the code:

intcurrentrow = Sheets("Phone Table").[a65536].End(xlUp).Offset(1).Row
On Error GoTo 1:
Set mycl2 = Sheet3.Columns("A").Find(What:=txtPhoneNumber.Value)
1:
If mycl2 Is Nothing Then
Sheets("Phone Table").Range("A" & intcurrentrow).Value = _
txtPhoneNumber.Value
Sheets("Phone Table").Range("B" & intcurrentrow).Value = _
TxtAddress.Value
End If
Me.Hide
frmPizzaOptions.Show
End Sub


Try something like this for textbox code;


<pre/>
Private Sub Textbox1_Change()
'// Allow ONLY Numbers
'// TextBox number mask
Dim Curpos As Double
Curpos = TextBox1.SelStart
If Not ValidateNumeric(Right(TextBox1.Text, 1)) Then
TextBox1.Text = Left(TextBox1.Text, Curpos - 1)
End If
End Sub

Private Function ValidateNumeric(strText As String) As Boolean
ValidateNumeric = CBool(strText = "" Or IsNumeric(strText))
End Function

</pre>
 
Upvote 0
On 2002-05-10 05:47, TheMatrixReloaded wrote:
nevermind, I fixed it. Thanks man

Thats great
How did you fix it?

Here is what I came up with.
Note this handles the event where the user
may enter -- in a row....

<pre/>
Private Sub Textbox1_Change()
'// Allow ONLY Numbers
'// TextBox number mask
Dim Curpos As Double
Curpos = TextBox1.SelStart
If Curpos = 1 Then
'// need to handle -- in a row
If Not ValidateNumeric(Right(TextBox1.Text, 1)) Then
TextBox1.Text = Left(TextBox1.Text, Curpos - 1)
End If
Else
If Not ValidateNumeric(Right(TextBox1.Text, 1), Mid(TextBox1.Text, Curpos - 1, 1)) Then
TextBox1.Text = Left(TextBox1.Text, Curpos - 1)
End If
End If
End Sub

Private Function ValidateNumeric(strText As String, Optional strPrev As String) As Boolean
ValidateNumeric = CBool(strText = "" Or IsNumeric(strText) _
Or strText = "-" And strPrev <> "-")
End Function

</pre>
 
Upvote 0

Forum statistics

Threads
1,213,513
Messages
6,114,072
Members
448,546
Latest member
KH Consulting

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