Results 1 to 2 of 2

Thread: For Each Loop Help

  1. #1

    Thread Starter
    Registered User
    Join Date
    Aug 2019

    Angry For Each Loop Help

    So basically, I've got a "For Each" loop that reads in data from an XML File and if the inputted address matches an address in the XML File, it'll display the data. Now, believe it or not, everything works fine except for one thing. It searches every record, so even if the data matches and it outputs it, it still displays a "Missing Record" MessageBox which has been coded to appear if it doesn't match.

    A Simple Exit For will work, except when I input it into the code, it doesn't work. I'm not sure where to place it, so I've put the code here in hopes to get some assistance with using the Exit For.

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click                                   'When the Search Button is clicked
            lstAddressOutput.Items.Clear()                                                                                      'Current records cleared
            Dim SearchAddress As String = txtAddressSearch.Text                                                                 'Declaring SearchAddress variable as entered data
            For Each Apartment In Records                                                                                       'Reads in Apartment Records
                If SearchAddress = (Apartment.Element("Address").Value) Then                                                    'If Name entered matches a tenant name
                    lstAddressOutput.Items.Add("Address: " + (Apartment.Element("Address").Value))                              'Adds Apartment Records to Listbox
                    lstAddressOutput.Items.Add("Suburb: " + (Apartment.Element("Suburb").Value))                                'Adds Apartment Records to Listbox
                    lstAddressOutput.Items.Add("Postcode: " + (Apartment.Element("Postcode").Value))                            'Adds Apartment Records to Listbox
                    lstAddressOutput.Items.Add("CurrentTenant: " + (Apartment.Element("CurrentTenant").Value))                  'Adds Apartment Records to Listbox
                    lstAddressOutput.Items.Add("Bedrooms: " + (Apartment.Element("Bedrooms").Value))                            'Adds Apartment Records to Listbox
                    lstAddressOutput.Items.Add("Bathrooms: " + (Apartment.Element("Bathrooms").Value))                          'Adds Apartment Records to Listbox
                ElseIf SearchAddress IsNot (Apartment.Element("Address").Value) Then                                            'If The address is not a match
                    MsgBox("Apartment Not Found")                                                                               'If Address doesn't match, error 404
                End If                                                                                                          'End If Statement
            Next Apartment                                                                                                      'Close Loop
        End Sub                                                                                                                 'End Private Sub

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

    Re: For Each Loop Help

    Welcome to VBForums

    You are currently showing that message once for every record that doesn't match... but really you only want to show it once, and only if there were no matching records.

    Add a boolean variable (perhaps called Found), and set it to False before the loop. Inside the If block (when a match is found) set the variable to True. After the loop, if the variable is still False, display the message.

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