How do I get the VBA code for my combo box to repeat for the next record in a form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TPezz
    New Member
    • Aug 2015
    • 4

    How do I get the VBA code for my combo box to repeat for the next record in a form?

    I am new to Access and I'm using a combo box to append fields in a report. The VBA code (see below) that I have written works for the first record, but I have to manually go to the next record and select the value from the combo box again to append that record. How do I get this to automatically go to the next record and repeat the process based on my initial selection?

    Code:
    Private Sub cboInvoiceNumber_Change()
        
            Me.txtInitialInvoiceDate.Value = Me.cboInvoiceNumber.Column(2)
            Me.txtInitialInvoiceNumber.Value = Me.cboInvoiceNumber.Column(1)
    
    End Sub
    Last edited by Rabbit; Aug 12 '15, 08:00 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    You could create a loop to go through all your records in your form.
    Code:
    Me.Recordset.MoveFirst
    
    Do While Not Me.Recordset.EOF
        Me.txtInitialInvoiceDate.Value = Me.cboInvoiceNumber.Column(2)
        Me.txtInitialInvoiceNumber.Value = Me.cboInvoiceNumber.Column(1)
    
        Me.Recordset.MoveNext
    Loop

    Comment

    • TPezz
      New Member
      • Aug 2015
      • 4

      #3
      Do I need to add something to end the loop? I tried this and it seems to run in a continuous loop.

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        What ends the loop is the
        Code:
        Do While Not Me.Recordset.EOF
        It will only loop to the end of the recordset (EOF stands for End Of File). Make sure that you have the
        Code:
        Me.Recordset.MoveNext
        in there, other wise it will just sit on the one record and it will loop endlessly.

        Comment

        • TPezz
          New Member
          • Aug 2015
          • 4

          #5
          Do I need anything before the Me.Recordset.Mo veFirst and/or after the Loop

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            Not for anything that you have specified in your question. Are the correct values getting appended, or are they all the same values?

            Comment

            • TPezz
              New Member
              • Aug 2015
              • 4

              #7
              All set, thank you for your help with this!
              Last edited by TPezz; Aug 13 '15, 03:00 PM. Reason: I just figured out what I was doing wrong.

              Comment

              Working...