Consulting

Results 1 to 7 of 7

Thread: Solved: Input check

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location

    Question Solved: Input check

    guys, I need some help.

    I have two txtBoxes on a form for users to add the data. One field is a Description and the other one is Amount.

    Before submission I would like to make sure that the first field doesn't contain the "/" symbol and the second field is actually the numeric value.

    Can you help with the code, please?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This checks input as entered

    [vba]

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    If KeyAscii < 48 Or KeyAscii > 57 Then
    KeyAscii = 0
    End If

    End Sub
    [/vba]
    You can test for / with

    [vba]

    If InStr("TextBox1.Text"."/") > 0 Then
    MsgBox "Invalid input"
    End If
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Feb 2007
    Posts
    68
    Location
    Hi
    Can we use this to make sure that textbox has an integer value :?

    [vba]Private Sub CommandButton1_Click()
    On Error GoTo err:
    Dim a As Long
    a = textbox1
    If a = textbox1 Then
    MsgBox "ya, that's good"
    End If

    err:
    If Err.Number <> 0 Then
    MsgBox "Input Integer only", vbCritical, "Invalid data"
    End If
    End Sub[/vba]
    Regards,
    Dan

  4. #4
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Quote Originally Posted by dansam
    Hi
    Can we use this to make sure that textbox has an integer value :?

    [vba]Private Sub CommandButton1_Click()
    On Error GoTo err:
    Dim a As Long
    a = textbox1
    If a = textbox1 Then
    MsgBox "ya, that's good"
    End If

    err:
    If Err.Number <> 0 Then
    MsgBox "Input Integer only", vbCritical, "Invalid data"
    End If
    End Sub[/vba]
    Regards,
    Dan
    I thought of using the code that looks like this:
    [VBA]Private Sub txtAddAmnt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not IsNumeric(txtAddAmnt) Then KeyAscii = 0
    End Sub[/VBA]
    For some reason it's not working. You, guys, know why?

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That is because Keypress is a before event, it is run before the value is accepted, so on first run the textbox is always blank, so the test always fails, so the input always gets cancelled.

    You would use the technique I showed earlier

    [vba]

    Private Sub txtAddAmnt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case 48 To 57 'Nos 0 - 9
    Exit Sub
    Case 32 ' space
    Case Else
    KeyAscii = 0
    Beep
    End Select

    End Sub
    [/vba]

  6. #6
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Great, it works. Thaks a lot, XLD

  7. #7
    VBAX Regular
    Joined
    Feb 2007
    Posts
    68
    Location
    Hi,
    xld's code works fine.
    thanks
    dan

Posting Permissions

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