Results 1 to 17 of 17

Thread: Exit Sub not working properly?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Question Exit Sub not working properly?

    This procedure is used to search through a database of customers via first and last names. If the user leaves the txtSearchFirst or txtSearchLast fields empty, a msgbox is displayed and the search is cancelled. Why is the sub continuing to the next lines of code if it should be exiting the sub?

    VB Code:
    1. Private Sub cmdSearch_Click()
    2.  
    3.     If txtSearchFirst = "" Or txtSearchLast = "" Then
    4.         MsgBox "Missing information for a valid search", vbCritical, "Warning!"
    5.         Exit Sub
    6.     End If
    7.  
    8.     adoSearch.Recordset.MoveFirst
    9.        
    10.     Do While Not adoSearch.Recordset.EOF
    11.         If adoSearch.Recordset.Fields("First Name").Value = txtSearchFirst And _
    12.             adoSearch.Recordset.Fields("Last Name").Value = txtSearchLast Then
    13.             MsgBox "Match Found!", vbInformation, ""
    14.             fraResults.Visible = True
    15.             imgResultsGirl.Visible = True
    16.             imgWelcomeBack.Visible = True
    17.             lblWelcomeName.Caption = txtResultFirst & " " & txtResultLast
    18.             lblWelcomeName.Visible = True
    19.             txtResultFirst = adoSearch.Recordset.Fields("First Name")
    20.             txtResultLast = adoSearch.Recordset.Fields("Last Name")
    21.             txtResultStreet = adoSearch.Recordset.Fields("Street")
    22.             txtResultCity = adoSearch.Recordset.Fields("City")
    23.             txtResultState = adoSearch.Recordset.Fields("State")
    24.             txtResultZip = adoSearch.Recordset.Fields("ZipCode")
    25.             txtResultPhone = adoSearch.Recordset.Fields("Phone")
    26.             txtResultFax = adoSearch.Recordset.Fields("Fax")
    27.             txtResultEmail = adoSearch.Recordset.Fields("Email")
    28.             adoSearch.Recordset.MoveNext
    29.     Loop
    30.             Else
    31.             MsgBox "No Match Found", vbInformation, ""
    32.             Exit Sub
    33.         End If
    34.            
    35. End Sub

  2. #2
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Exit Sub not working properly?

    If trim(txtSearchFirst.text) = "" Or trim(txtSearchLast.text) = "" Then

  3. #3
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Exit Sub not working properly?

    thats your code? it looks like you should be getting an error cause the loop loops through only 1 part of the IF

    Put the whole if statement in the loop

  4. #4
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Exit Sub not working properly?

    Also, your 'IF' starts inside the loop, and the 'ENDIF' is outside the loop.
    VB Code:
    1. Do While Not adoSearch.Recordset.EOF
    2.         If adoSearch.Recordset.Fields("First Name").Value = txtSearchFirst And _
    3.             adoSearch.Recordset.Fields("Last Name").Value = txtSearchLast Then
    4.             MsgBox "Match Found!", vbInformation, ""
    5.             fraResults.Visible = True
    6.             imgResultsGirl.Visible = True
    7.             imgWelcomeBack.Visible = True
    8.             lblWelcomeName.Caption = txtResultFirst & " " & txtResultLast
    9.             lblWelcomeName.Visible = True
    10.             txtResultFirst = adoSearch.Recordset.Fields("First Name")
    11.             txtResultLast = adoSearch.Recordset.Fields("Last Name")
    12.             txtResultStreet = adoSearch.Recordset.Fields("Street")
    13.             txtResultCity = adoSearch.Recordset.Fields("City")
    14.             txtResultState = adoSearch.Recordset.Fields("State")
    15.             txtResultZip = adoSearch.Recordset.Fields("ZipCode")
    16.             txtResultPhone = adoSearch.Recordset.Fields("Phone")
    17.             txtResultFax = adoSearch.Recordset.Fields("Fax")
    18.             txtResultEmail = adoSearch.Recordset.Fields("Email")
    19.             adoSearch.Recordset.MoveNext
    20.     Loop
    21.             Else
    22.             MsgBox "No Match Found", vbInformation, ""
    23.             Exit Sub
    24.         End If


    Edit: Added [vbcode][/vbcode] tags for clairty. - Hack
    Last edited by Hack; Aug 10th, 2005 at 02:01 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Exit Sub not working properly?

    VB Code:
    1. Private Sub cmdSearch_Click()
    2.  
    3.     If Trim(txtSearchFirst.Text) = "" Or Trim(txtSearchLast.Text) = "" Then
    4.         MsgBox "Missing information for a valid search", vbCritical, "Warning!"
    5.         Exit Sub
    6.     End If
    7.  
    8.     adoSearch.Recordset.MoveFirst
    9.        
    10.     Do While Not adoSearch.Recordset.EOF
    11.         If adoSearch.Recordset.Fields("First Name").Value = txtSearchFirst And _
    12.             adoSearch.Recordset.Fields("Last Name").Value = txtSearchLast Then
    13.             MsgBox "Match Found!", vbInformation, ""
    14.             fraResults.Visible = True
    15.             imgResultsGirl.Visible = True
    16.             imgWelcomeBack.Visible = True
    17.             lblWelcomeName.Caption = txtResultFirst & " " & txtResultLast
    18.             lblWelcomeName.Visible = True
    19.             txtResultFirst = adoSearch.Recordset.Fields("First Name")
    20.             txtResultLast = adoSearch.Recordset.Fields("Last Name")
    21.             txtResultStreet = adoSearch.Recordset.Fields("Street")
    22.             txtResultCity = adoSearch.Recordset.Fields("City")
    23.             txtResultState = adoSearch.Recordset.Fields("State")
    24.             txtResultZip = adoSearch.Recordset.Fields("ZipCode")
    25.             txtResultPhone = adoSearch.Recordset.Fields("Phone")
    26.             txtResultFax = adoSearch.Recordset.Fields("Fax")
    27.             txtResultEmail = adoSearch.Recordset.Fields("Email")
    28.             adoSearch.Recordset.MoveNext
    29.             Else
    30.             MsgBox "No Match Found", vbInformation, ""
    31.             Exit Sub
    32.         End If
    33. Loop
    34.    
    35. End Sub

    I think my "No Match Found" MsgBox is in the wrong place because it shold be finding the info that Im typing in. I verified by looking at the database.
    Last edited by phoeneous; Aug 10th, 2005 at 02:08 PM.

  6. #6
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Exit Sub not working properly?

    you would have had that error long before the Trim was added

    Switch ur above code to this

    VB Code:
    1. Do While Not adoSearch.Recordset.EOF
    2.         If adoSearch.Recordset.Fields("First Name").Value = txtSearchFirst And _
    3.             adoSearch.Recordset.Fields("Last Name").Value = txtSearchLast Then
    4.             MsgBox "Match Found!", vbInformation, ""
    5.             fraResults.Visible = True
    6.             imgResultsGirl.Visible = True
    7.             imgWelcomeBack.Visible = True
    8.             lblWelcomeName.Caption = txtResultFirst & " " & txtResultLast
    9.             lblWelcomeName.Visible = True
    10.             txtResultFirst = adoSearch.Recordset.Fields("First Name")
    11.             txtResultLast = adoSearch.Recordset.Fields("Last Name")
    12.             txtResultStreet = adoSearch.Recordset.Fields("Street")
    13.             txtResultCity = adoSearch.Recordset.Fields("City")
    14.             txtResultState = adoSearch.Recordset.Fields("State")
    15.             txtResultZip = adoSearch.Recordset.Fields("ZipCode")
    16.             txtResultPhone = adoSearch.Recordset.Fields("Phone")
    17.             txtResultFax = adoSearch.Recordset.Fields("Fax")
    18.             txtResultEmail = adoSearch.Recordset.Fields("Email")
    19.             adoSearch.Recordset.MoveNext
    20.             Else
    21.             MsgBox "No Match Found", vbInformation, ""
    22.             Exit Sub
    23.         End If
    24. Loop

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Exit Sub not working properly?

    Yeah, I moved the loop to the outside but I think my "No Match Found" msgbox is in the wrong place because it isnt finding the info that Im typing in. I verified by looking at the records in the dbase and searched for that same exact name yet I still get "No Match Found".

  8. #8
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Exit Sub not working properly?

    try

    VB Code:
    1. Do While Not adoSearch.Recordset.EOF
    2.         If Trim(adoSearch.Recordset.Fields("First Name")) = txtSearchFirst.Text And _
    3.             Trim(adoSearch.Recordset.Fields("Last Name")) = txtSearchLast.Text Then
    4.             MsgBox "Match Found!", vbInformation, ""
    5.             fraResults.Visible = True
    6.             imgResultsGirl.Visible = True
    7.             imgWelcomeBack.Visible = True
    8.             lblWelcomeName.Caption = txtResultFirst & " " & txtResultLast
    9.             lblWelcomeName.Visible = True
    10.             txtResultFirst = adoSearch.Recordset.Fields("First Name")
    11.             txtResultLast = adoSearch.Recordset.Fields("Last Name")
    12.             txtResultStreet = adoSearch.Recordset.Fields("Street")
    13.             txtResultCity = adoSearch.Recordset.Fields("City")
    14.             txtResultState = adoSearch.Recordset.Fields("State")
    15.             txtResultZip = adoSearch.Recordset.Fields("ZipCode")
    16.             txtResultPhone = adoSearch.Recordset.Fields("Phone")
    17.             txtResultFax = adoSearch.Recordset.Fields("Fax")
    18.             txtResultEmail = adoSearch.Recordset.Fields("Email")
    19.             adoSearch.Recordset.MoveNext
    20.             Else
    21.             MsgBox "No Match Found", vbInformation, ""
    22.             Exit Sub
    23.         End If
    24. Loop

    all i can really think of

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Exit Sub not working properly?

    I dont think its even reading from the dbase... I tried the following procedure and my msgbox came up blank!

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     adoSearch.Recordset.MoveFirst
    4.     MsgBox (adoSearch.Recordset.Fields("First Name"))
    5.    
    6. End Sub

    Ggggrrrrr.....

  10. #10
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Exit Sub not working properly?

    you know what? if you dont find a record you make it move on

    try putting "adoSearch.Recordset.MoveNext" outside the loop


    can you post all your code? the connection string and everything please?
    Last edited by kfcSmitty; Aug 10th, 2005 at 02:33 PM.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Exit Sub not working properly?

    The problem is that the dbase is overwriting the first record when I click on save. I need to modify some of my procedures, be back later.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Exit Sub not working properly?

    Well, I still cant figure it out. I thought the dbase was overwriting the first record but it doesnt.

    Is my syntax correct for comparing the txtsearchfirst and txtsearchlast fields to the fileds of the dbase?

    Do these two statements do the same things?

    VB Code:
    1. If Trim(adoSearch.Recordset.Fields("First Name")) = txtSearchFirst.Text And _
    2.             Trim(adoSearch.Recordset.Fields("Last Name")) = txtSearchLast.Text Then

    VB Code:
    1. If Trim(adoSearch.Recordset.Fields(0)) = txtSearchFirst.Text And _
    2.             Trim(adoSearch.Recordset.Fields(1)) = txtSearchLast.Text Then

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Exit Sub not working properly?

    Those statements are the same. Try it like this. If it doesn't find a match, it will loop until the end and print an error message. If it does find a match, it will exit the sub without reading any more records.

    VB Code:
    1. Private Sub cmdSearch_Click()
    2.  
    3.     If Trim(txtSearchFirst.Text) = "" Or Trim(txtSearchLast.Text) = "" Then
    4.         MsgBox "Missing information for a valid search", vbCritical, "Warning!"
    5.         Exit Sub
    6.     End If
    7.  
    8.     adoSearch.Recordset.MoveFirst
    9.        
    10.     Do While Not adoSearch.Recordset.EOF
    11.         If adoSearch.Recordset.Fields("First Name").Value = txtSearchFirst And _
    12.             adoSearch.Recordset.Fields("Last Name").Value = txtSearchLast Then
    13.             MsgBox "Match Found!", vbInformation, ""
    14.             fraResults.Visible = True
    15.             imgResultsGirl.Visible = True
    16.             imgWelcomeBack.Visible = True
    17.             lblWelcomeName.Caption = txtResultFirst & " " & txtResultLast
    18.             lblWelcomeName.Visible = True
    19.             txtResultFirst = adoSearch.Recordset.Fields("First Name")
    20.             txtResultLast = adoSearch.Recordset.Fields("Last Name")
    21.             txtResultStreet = adoSearch.Recordset.Fields("Street")
    22.             txtResultCity = adoSearch.Recordset.Fields("City")
    23.             txtResultState = adoSearch.Recordset.Fields("State")
    24.             txtResultZip = adoSearch.Recordset.Fields("ZipCode")
    25.             txtResultPhone = adoSearch.Recordset.Fields("Phone")
    26.             txtResultFax = adoSearch.Recordset.Fields("Fax")
    27.             txtResultEmail = adoSearch.Recordset.Fields("Email")
    28.             exit sub
    29.         End If
    30.         adoSearch.Recordset.MoveNext
    31. Loop
    32.         MsgBox "No Match Found", vbInformation, ""
    33.    End Sub

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Exit Sub not working properly?

    This is going to sound stupid... but. Why don't you use a WHERE clause (FirstName & LastName), returning ONLY the matched Record?

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Exit Sub not working properly?

    Okay, that finds the match but something keeps overwriting the first record in the database and it only does it when I run the search.

    It also "no match found" for every record if it doesnt match the search. So, out of 12 records, 11 dont match I will get a msgbox 11 times saying "no match found".

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Exit Sub not working properly?

    1) The msgbox is outside of the loop, so it gets called once, every time you call the search routine. 2) Nothing in the sub does anything TO the recordset.
    You must have something else looping thru the records, and changing them.
    Not that it would matter, unless you are displaying or saving them somewhere else.

    You should place a breakpoint in your code, and step (F8) thru it to see what it's doing.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Exit Sub not working properly?

    I found it, but after 5 hours of coding and debugging yesterday I forgot what it was

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