Consulting

Results 1 to 8 of 8

Thread: Restricting Input on a TextBox

  1. #1

    Restricting Input on a TextBox

    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

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi Leah and welcome to VBAX

    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
    K :-)

  3. #3
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    oops...
    the user can't cancel the input box
    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
    K :-)

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    ... 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
    K :-)

  5. #5
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    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

  6. #6
    Thanks for all the help! Problem was solved wonderfully! Look for my next post if you are interested, I have one last problem!

    --leah

  7. #7
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Since IsNumeric function returns true for the data like 1,,,,, or 1d5
    It does?
    K :-)

  8. #8
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    You can always use Application.Inputbox

    and use Type:=1 >> Number
    Kind Regards,
    Ivan F Moala From the City of Sails

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •