PDA

View Full Version : [SOLVED:] Type Mismatch Error regarding a do while loop



stixx
10-09-2015, 11:13 PM
getting a type mismatch error but i've defined all the variables. the idea is that the program shows an error message when A, B or C arent entered


Option Explicit


Function main()
Dim packageSelection As String
Dim adultTickets As Integer
Dim childTickets As Integer



packageSelection = InputBox("Welcome, please enter your preffered package.")
packageSelection = UCase(packageSelection)


Do While packageSelection <> "A" Or "B" Or "C"
MsgBox ("error, please enter package selection")
packageSelection = InputBox("Welcome, please enter your preffered package.")
packageSelection = UCase(packageSelection)
Loop

jonh
10-10-2015, 02:43 AM
Private Sub sub1()
Dim packageSelection As String
Do
packageSelection = InputBox("Please enter package selection. (A, B or C", , packageSelection)
packageSelection = UCase(packageSelection)
Loop Until packageSelection = "A" Or packageSelection = "B" Or packageSelection = "C"
End Sub

or



Private Sub sub2()
Dim packageSelection As String
Do
Select Case packageSelection
Case "A", "B", "C"
Exit Do
Case Else
packageSelection = UCase(InputBox("Please enter package selection. (A, B or C", , packageSelection))
End Select
Loop
End Sub

stixx
10-10-2015, 03:23 AM
both are amazing and solve the problem really well but I have to ask if there is a way to display the error message as it is required as part of an assessment piece. Thanks so much for your help so far!

jonh
10-10-2015, 05:29 AM
There is no error imo, it's just data validation.

Use a counter. Plus one it after the input. If it's >1 the user has had a failed input.

SamT
10-10-2015, 08:24 AM
Actually that is a bad syntax error.

Do While packageSelection <> "A" Or packageSelection <> "B" Or packageSelection <> "C"

However your code also has a logic error using Or. If you correct the syntax, Or will always loop because at least two of the condition will be true.


Do While packageSelection <> "A" And packageSelection <> "B" And packageSelection <> "C"