Results 1 to 4 of 4
  1. #1
    roarcrm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2014
    Posts
    27

    Loop to add multiple records

    Hello,

    I currently have a form that adds data to a table, it currently only lets us select one client so if 10 clients attend a workshop then we have to fill this form in 10 times. I wondered if it was possible to have a multi-select list box to select as many clients as required and then loop through the rest of the fields that would all be the same. The code to add currently is:

    Code:
    Private Sub Command25_Click()
    
    Dim rst As Recordset
    Set rst = CurrentDb.OpenRecordset("Interaction")
    With rst
         .AddNew
         .Fields("Member") = ClientCombo.Value
         .Fields("Date of Interaction") = DateofInteraction.Value
         .Fields("Time Spent") = TimeSpent.Value
         .Fields("Interaction") = Interaction.Value
         .Fields("Service") = Service.Value
         .Fields("Staff") = Staff.Value
         .Fields("Comment") = Comment.Value
         .Update
     End With
     DoCmd.Close acForm, "Log Interaction", acSaveYes
    End Sub
    Any help with this? Thanks.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can iterate a multiselect listbox using a for each statement. Creating and testing a loop can be difficult if your code fails to ever exit the loop. It is good to understand how to use breaking points and the debugger tools when creating loops that iterate.

    So your code would look something like this.
    Code:
        
    
    'There is no need to declare and initialize the variant varItem
    'Dim varItem as Variant
    'varItem = ""
    
    Dim rst As Recordset
    Set rst = CurrentDb.OpenRecordset("Interaction")
    
    For Each varItem In Me.lstContol.ItemsSelected
    
    With rst
         .AddNew
         .Fields("Member") = Me.lstControl.Column(0, varItem)
         .Fields("Date of Interaction") = DateofInteraction.Value
         .Fields("Time Spent") = TimeSpent.Value
         .Fields("Interaction") = Interaction.Value
         .Fields("Service") = Service.Value
         .Fields("Staff") = Staff.Value
         .Fields("Comment") = Comment.Value
         .Update
     End With
    
    Next varItem
    
    rst.close
    set rst = Nothing
     DoCmd.Close acForm, "Log Interaction", acSaveYes

  3. #3
    roarcrm is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jun 2014
    Posts
    27
    Excellent thank you for that!

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You bet. It may be helpful to add code that will check if the user actually selected an item within the ListBox. You can see what properties are available by typing the control name and then the dot operator. Intellisence will display the Objects members. (Me.lstControl.) Checking the control's properties before executing code is a good idea.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 17
    Last Post: 04-07-2014, 07:48 PM
  2. Replies: 5
    Last Post: 10-08-2012, 12:05 PM
  3. VBA Loop to Combine Records
    By admessing in forum Queries
    Replies: 23
    Last Post: 03-06-2012, 11:37 AM
  4. Loop through records
    By sam10 in forum Programming
    Replies: 12
    Last Post: 07-07-2011, 02:30 PM
  5. Loop through Records and Make ID
    By rob4465 in forum Programming
    Replies: 3
    Last Post: 01-14-2010, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums