[Resolved]Exit loop when first user input error is found
I have the following code and I need to exit the checking if an error is found otherwise the user could have numerous msgboxs poping up one after another.
I am not too sure how to do this.
I have tried Do While, but either I got it wrong or this is not the correct way :(
VB Code:
'check all txtboxes are filled in correctly
If Len(txtName.Text) < 3 Then
If MsgBox("Please enter a valid Drivers Name", vbOK, "Invalid driver name") = vbOK Then
txtName.Text = ""
txtName.SetFocus
ErrorResult = "Yes"
End If
End If
'check agency name is correct format etc
If Len(cmbAgencyName.Value) = 0 Then
If MsgBox("Please enter a valid Agency Name", vbOK, "Invalid Agency Name") = vbOK Then
cmbAgencyName.Value = "Pertemps"
cmbAgencyName.SetFocus
ErrorResult = "Yes"
End If
End If
'check there are the correct number of characters in date boxes
If Len(txtAssessment.Text) <> 8 Then
If MsgBox("Please enter a valid Assessment Date", vbOK, "Invalid Assessment Date") = vbOK Then
txtAssessment.Text = Format(Date, "DD/MM/YY")
txtAssessment.SetFocus
ErrorResult = "Yes"
End If
End If
If Len(txtLicense.Text) <> 8 Then
If MsgBox("Please enter a valid License Checked Date", vbOK, "Invalid License Checked Date") = vbOK Then
txtLicense.Text = Format(Date, "DD/MM/YY")
txtLicense.SetFocus
ErrorResult = "Yes"
End If
End If
If Len(txtInduction.Text) <> 8 Then
If MsgBox("Please enter a valid Induction Date", vbOK, "Invalid Induction Date") = vbOK Then
txtInduction.Text = Format(Date, "DD/MM/YY")
txtInduction.SetFocus
ErrorResult = "Yes"
End If
End If
'if no errors go to next form
If ErrorResult = "No" Then
frmAgency.Hide
frmTrainingClerical.Show
End If
Re: Exit loop when first user input error is found
You can either extend your If statements to check ErrorResult, eg:
VB Code:
If (ErrorResult = "No") And (Len(txtInduction.Text) <> 8) Then
..or you can exit the routine entirely by using Exit Sub (or Exit Function).
Re: Exit loop when first user input error is found
Thanks Si.
the Exit Sub was the one I was trying to think of.
Re: Exit loop when first user input error is found
If you use the Exit Sub, make sure that any other code you may have run in the sub prior to checking input, if any, doesn't cause errors in whatever code runs next. If no other prior code, no problem.
Re: Exit loop when first user input error is found
Actually I've just noticed something else.. for the MsgBox's you have If statements when there is no need (as there is only one possible return value), you could have them like this:
VB Code:
If Len(txtName.Text) < 3 Then
MsgBox "Please enter a valid Drivers Name", vbOK, "Invalid driver name"
txtName.Text = ""
txtName.SetFocus
ErrorResult = "Yes"
End If
..which makes an alternative option spring to mind - you could use ElseIf, eg:
VB Code:
If Len(txtName.Text) < 3 Then
MsgBox "Please enter a valid Drivers Name", vbOK, "Invalid driver name"
...
ErrorResult = "Yes"
'check agency name is correct format etc
ElseIf Len(cmbAgencyName.Value) = 0 Then
MsgBox ...
...
...
...
'if no errors go to next form
Else
frmAgency.Hide
frmTrainingClerical.Show
End If
This would be tidier, avoid any issues like salvelinus was thinking of, and would probably eliminate the need for your ErrorResult variable.
Re: Exit loop when first user input error is found
Thanks to both salvelinus and si_the_geek. :)
I actually have changes this to the ElseIf already, but thanks for the help.
I did do a year or two of programming a couple of years ago, self taught at home, but I am only just getting back into it again, and thats VBA.
Its a shame how much I seem to have forgotten. :(