Consulting

Results 1 to 5 of 5

Thread: Solved: Input Box Validation please help

  1. #1

    Solved: Input Box Validation please help

    I am new to Vb and need help. I need a user to input 1 or 2 only. I need the loop to validate this an not crash. Here is what I have and it does not work.


    Dim LocationCode As Integer
    Dim SalesAmount As Single
    Dim Bonus As Single
    Dim Commision As Single
    Dim IsValid As Boolean
    Dim i As Integer
    ' set isValid = to false
    IsValid = False
    Do Until IsValid = True
    'Ask the user to input Location code into inout box
    LocationCode = InputBox("Enter a Location Code 1 or 2", "Location Code", 1)
    ' Cofirms that the user entered a 1 or 2
    If LocationCode <> 1 Or 2 Then IsValid = False

    'If not a 1 or 2 the user sees an error message
    MsgBox "Sorry you did not enter a 1 or 2. Please try again."
    End


    For i = 1 To 9
    If LocationCode = 1 Or 2 Then
    IsValid = True
    Exit For
    End If
    Next

    Loop




    SalesAmount = InputBox("enter amount")




    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. Use the little VBA icon will insert the formatting tags

    2. Two syntax errors

    [vba]
    If LocationCode <> 1 Or 2 -----------

    MsgBox "Sorry you did not enter a 1 or 2. Please try again."
    End --------------- End If

    [/vba]

    [vba]
    Option Explicit
    Sub test()
    Dim LocationCode As Integer
    Dim SalesAmount As Single
    Dim Bonus As Single
    Dim Commision As Single
    Dim IsValid As Boolean
    Dim i As Integer
    ' set isValid = to false
    IsValid = False

    Do Until IsValid = True

    'Ask the user to input Location code into inout box
    LocationCode = InputBox("Enter a Location Code 1 or 2", "Location Code", 1)

    'Confirms that the user entered a 1 or 2
    If (LocationCode = 1) Or (LocationCode = 2) Then 'you need an AND or OR ----------
    IsValid = True
    Else
    'If not a 1 or 2 the user sees an error message
    MsgBox "Sorry you did not enter a 1 or 2. Please try again."
    End If ' -------------- you need the IF

    Loop
    SalesAmount = InputBox("enter amount")
    End Sub
    [/vba]

    Paul

  3. #3
    VBAX Regular pike's Avatar
    Joined
    Dec 2007
    Location
    Alstonville, Australia
    Posts
    97
    Location
    or another way to capture the error if canel button is clicked

    Option Explicit
    Sub Input_Value()
        Dim LocationCode
        Dim IsValid As Boolean
        Do Until IsValid
        'Ask the user to input Location code into inout box
            LocationCode = InputBox("Enter a Location Code 1 or 2", "Location Code", 1)
        ' Cofirms that the user entered a 1 or 2
            Select Case LocationCode
            Case ""
            MsgBox "You have cancelled"
                Exit Sub
            Case 1, 2
                MsgBox "You did enter a 1 or 2"
                IsValid = True
            Case Else
                MsgBox "Sorry you did not enter a 1 or 2. Please try again."
            End Select
        Loop
        MsgBox " You Have exited the Loop"
        ' Your next  code
    End Sub

  4. #4

    Post

    [VBA]Dim LocationCode As Integer
    Dim SalesAmount As Single
    Dim Bonus As Single
    Dim Commision As Single
    Dim IsValid As Boolean
    Dim i As Integer
    ' set isValid = to false
    'IsValid = False
    'Do Until IsValid = True
    'Ask the user to input Location code into inout box
    LocationCode = InputBox("Enter a Location Code 1 ", "Location Code", 1)
    'Computer Cofirms that the user entered a 1
    'IsValid = False
    IsValid = False
    [/VBA]

  5. #5
    Thanks Paul,

    That works great! Sorry this is my first post did not know to use the VBA icon.

    I was pretty sure the End was wrong.

    The other syntax error I could not find an example of that thak you so much.

Posting Permissions

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