Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inputbox: Exit nested For Each-Next without leaving the execution

Status
Not open for further replies.

OscarAlberto

Programmer
Joined
Dec 4, 2002
Messages
15
Location
CR
How do I go about providing the user with a MsgBox containing information about an error made while making data entries via an InputBox, and then returning to the execution point where it was left when the error appeared? If I provide a UserForm to obtain input it is relatively easy to trap an error using TextBox_Exit but I cannot figure how to do it with an InputBox. The main reason not to use UserForms is size of the app.

Following the code:

'Assigns values to variables to keep track of # of customers and Doctors categories.
Counter = 0
Counter2 = 0
Counter3 = 0
For Each r In Doctors
For Each t In Customers
Msg = InputBox("Enter the BASE TARIFF for" & _
Space(3) & r & _
vbCrLf & vbCrLf & "and Doctor = " & Space(3) & t, _
"SOFTWARE – Medical Services Application")
'Enters the name of each doctor in the database.
If Not IsNumeric(Msg) Or Msg <= 0 Then
Msg = MsgBox(vbTab & _
"TARIFF DATA" & _
vbCrLf & vbCrLf & vbCrLf & _
"The figure you entered for BASE TARIFFS is NOT VALID." & _
vbCrLf & vbCrLf & vbCrLf & _
"To continue, PRESS OK and launch the MEDICAL Data Menu again." & _
vbCrLf & vbCrLf & vbCrLf, _
vbOKOnly + vbCritical, _
"SOFTWARE – Medical Services Application")
'Clears data from collections.
For Counter = 1 To Tariffs.Count
Tariffs.Remove 1
Next
For Counter = 1 To Doctors.Count
Doctors.Remove 1
Next
For Counter = 1 To Customers.Count
Customers.Remove 1
Next
'Stops execution of code.
Exit Sub


Thanks in advance.

OscarAlberto.
 
If I understand your question correctly, you can use a "Do. . . Loop While" statement. Here's an example:
Code:
IsValid = False
Do
  Msg = InputBox("Enter a number")
  If Not IsNumeric(Msg) Then ' bad entry
    MsgBox("You must enter a number. Try again.")
    IsValid = False
  Else
    IsValid = True
  End If
Loop While Not IsValid
It might be a good idea to make the error message box a vbYesNo box so the user would have the option of escaping the loop (just set IsValid to True if they click "No").


VBAjedi [swords]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top