Results 1 to 6 of 6

Thread: [Resolved]Exit loop when first user input error is found

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    [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:
    1. 'check all txtboxes are filled in correctly
    2.         If Len(txtName.Text) < 3 Then
    3.            If MsgBox("Please enter a valid Drivers Name", vbOK, "Invalid driver name") = vbOK Then
    4.                 txtName.Text = ""
    5.                 txtName.SetFocus
    6.                 ErrorResult = "Yes"
    7.             End If
    8.         End If
    9.        
    10.         'check agency name is correct format etc
    11.         If Len(cmbAgencyName.Value) = 0 Then
    12.             If MsgBox("Please enter a valid Agency Name", vbOK, "Invalid Agency Name") = vbOK Then
    13.                 cmbAgencyName.Value = "Pertemps"
    14.                 cmbAgencyName.SetFocus
    15.                 ErrorResult = "Yes"
    16.            End If
    17.         End If
    18.        
    19.         'check there are the correct number of characters in date boxes
    20.         If Len(txtAssessment.Text) <> 8 Then
    21.             If MsgBox("Please enter a valid Assessment Date", vbOK, "Invalid Assessment Date") = vbOK Then
    22.                 txtAssessment.Text = Format(Date, "DD/MM/YY")
    23.                 txtAssessment.SetFocus
    24.                 ErrorResult = "Yes"
    25.            End If
    26.         End If
    27.        
    28.         If Len(txtLicense.Text) <> 8 Then
    29.             If MsgBox("Please enter a valid License Checked Date", vbOK, "Invalid License Checked Date") = vbOK Then
    30.                 txtLicense.Text = Format(Date, "DD/MM/YY")
    31.                 txtLicense.SetFocus
    32.                 ErrorResult = "Yes"
    33.            End If
    34.         End If
    35.        
    36.         If Len(txtInduction.Text) <> 8 Then
    37.             If MsgBox("Please enter a valid Induction Date", vbOK, "Invalid Induction Date") = vbOK Then
    38.                 txtInduction.Text = Format(Date, "DD/MM/YY")
    39.                 txtInduction.SetFocus
    40.                 ErrorResult = "Yes"
    41.            End If
    42.         End If
    43.    
    44.         'if no errors go to next form
    45.         If ErrorResult = "No" Then
    46.             frmAgency.Hide
    47.             frmTrainingClerical.Show
    48.         End If
    Last edited by aikidokid; Jan 28th, 2007 at 02:06 AM. Reason: Resolved
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Exit loop when first user input error is found

    You can either extend your If statements to check ErrorResult, eg:
    VB Code:
    1. If (ErrorResult = "No") And (Len(txtInduction.Text) <> 8) Then
    ..or you can exit the routine entirely by using Exit Sub (or Exit Function).

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Exit loop when first user input error is found

    Thanks Si.

    the Exit Sub was the one I was trying to think of.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    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.
    Tengo mas preguntas que contestas

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. If Len(txtName.Text) < 3 Then
    2.            MsgBox "Please enter a valid Drivers Name", vbOK, "Invalid driver name"
    3.            txtName.Text = ""
    4.            txtName.SetFocus
    5.            ErrorResult = "Yes"
    6.         End If
    ..which makes an alternative option spring to mind - you could use ElseIf, eg:
    VB Code:
    1. If Len(txtName.Text) < 3 Then
    2.            MsgBox "Please enter a valid Drivers Name", vbOK, "Invalid driver name"
    3. ...
    4.            ErrorResult = "Yes"
    5.  
    6.         'check agency name is correct format etc
    7.         ElseIf Len(cmbAgencyName.Value) = 0 Then
    8.            MsgBox ...
    9. ...
    10. ...
    11. ...    
    12.         'if no errors go to next form
    13.         Else
    14.             frmAgency.Hide
    15.             frmTrainingClerical.Show
    16.         End If
    This would be tidier, avoid any issues like salvelinus was thinking of, and would probably eliminate the need for your ErrorResult variable.

  6. #6

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    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.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

Posting Permissions

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



Click Here to Expand Forum to Full Width