PDA

View Full Version : Solved: Input Box Validation please help



buddy2000
02-05-2011, 05:38 PM
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

Paul_Hossler
02-05-2011, 07:33 PM
1. Use the little VBA icon will insert the formatting tags

2. Two syntax errors


If LocationCode <> 1 Or 2 -----------

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




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


Paul

pike
02-05-2011, 07:45 PM
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

buddy2000
02-05-2011, 08:49 PM
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

buddy2000
02-05-2011, 08:51 PM
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.