PDA

View Full Version : [SOLVED] Restricting Input on a TextBox



leah
07-25-2006, 01:11 PM
Hi

I am trying to do a data validation of some sort in a textbox and wondering if it is possible. I was hoping to restrict the input to 4 digits, no more no less! If there is anyone who can help please let me know! Here is part of my code if that helps explain what is happening!


Private Sub btnSearchMachNum_Click()
Dim number As String
Prompt = "Please enter the number using 4 digits." & vbNewLine & "(i.e. 300 should be '0300')" & vbNewLine & Path
Title = "Input Number"
macnumber = InputBox(Prompt, Title, "Enter search term")
Call FindAll(macnumber, True)
End Sub

--Leah

Killian
07-25-2006, 02:57 PM
Hi Leah and welcome to VBAX :hi:

One easy solution is to loop the InputBox function until it's return value matches your criteria (length: 4 charaters and a number)


Do
macnumber = InputBox(Prompt, Title, "Enter search term")
Loop Until Len(macnumber) = 4 And IsNumeric(macnumber)


This doesn't provide any feedback to the user but maybe your prompt text already has that covered

Killian
07-25-2006, 02:59 PM
oops...
the user can't cancel the input box :doh:
Slight adjustment to deal with that



Do
macnumber = InputBox(Prompt, Title, "Enter search term")
Loop Until Len(macnumber) = 4 And IsNumeric(macnumber) Or macnumber = ""
If macnumber <> "" Then
Call FindAll(macnumber, True)
End If

Killian
07-25-2006, 03:04 PM
... and maybe it would be polite the put the user's invalid entry as the default so they can clearly see the error of their ways



Do
If macnumber = "" Then
DefaultText = "Enter search term"
Else
DefaultText = macnumber
End If
macnumber = InputBox(Prompt, Title, DefaultText)
Loop Until Len(macnumber) = 4 And IsNumeric(macnumber) Or macnumber = ""

ok, i'm done now :)

jindon
07-25-2006, 08:54 PM
Since IsNumeric function returns true for the data like 1,,,,, or 1d5


With CreateObject("VBScript.RegExp")
.Pattern = "^\d{4}$"
Do
num = InputBox("Enter 4 degits number only")
If Not Len(num) Then Exit Sub
Loop Until .test(num) = True
End With

leah
07-26-2006, 06:51 AM
Thanks for all the help! Problem was solved wonderfully! Look for my next post if you are interested, I have one last problem!

--leah

Killian
07-26-2006, 08:14 AM
Since IsNumeric function returns true for the data like 1,,,,, or 1d5It does? :think:

Ivan F Moala
07-26-2006, 08:37 PM
You can always use Application.Inputbox

and use Type:=1 >> Number