Results 1 to 4 of 4
  1. #1
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130

    Question Adding Records from MultiSelect ListBox

    Code:
    Private Sub cmdAssignClasses_Click()
    
    Dim MyDB As dao.Database
    Dim varItem As Variant
    Dim lst As ListBox
     
    Dim rst As dao.Recordset
    
    
    If lstTrainingProviders.ItemsSelected.Count = 0 Then
        MsgBox "Please select at least 1 (one) training provider.", vbOKOnly, "Error"
        Exit Sub
    End If
    
    
    DoCmd.RunCommand acCmdSaveRecord
     
    Set MyDB = CurrentDb
    Set rst = MyDB.OpenRecordset("tbl2GrantTrainers", dbOpenDynaset, dbAppendOnly)
     
    With rst
      For Each varItem In lstTrainingProviders.ItemsSelected
        .AddNew
           ![GrantID] = frmAddNewGrant![ID]
           ![TrainerID] = lstTrainingProviders.ItemData(varItem)
        .Update
      Next varItem
    End With
     
    rst.Close
    Set rst = Nothing
     
    MsgBox "The training providers have been successfully added to the new grant.", vInformation, "Information"
    
    
    DoCmd.Maximize
    End Sub
    For some reason this code is not executing the following code:



    Code:
    With rst  For Each varItem In lstTrainingProviders.ItemsSelected
        .AddNew
           ![GrantID] = frmAddNewGrant![ID]
           ![TrainerID] = lstTrainingProviders.ItemData(varItem)
        .Update
      Next varItem
    End With
    It steps through With rst
    Highlights For Each varItem line to show it will execute next
    Then it jumps down and highlights End With without stepping through the other lines of code. Any idea where I went wrong here?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I wonder if the SaveRecord line is clearing the selections? Try commenting that out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    Wow thanks for that. It is running through the code now. Now just to changed it to where the record gets added to the Grant table as well. Thanks a bunch!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help! If the form is bound, it will save without code, either when you move to a different record or close the form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Multiselect Listbox
    By wwhit in forum Forms
    Replies: 19
    Last Post: 03-09-2015, 02:58 PM
  2. VBA Select All Rows in a MultiSelect ListBox
    By ItsMe in forum Code Repository
    Replies: 0
    Last Post: 04-09-2014, 01:52 PM
  3. Replies: 4
    Last Post: 06-24-2013, 07:34 AM
  4. Listbox multiselect status
    By Sam23 in forum Programming
    Replies: 5
    Last Post: 03-06-2012, 01:13 PM
  5. Append Records using MultiSelect Listbox
    By Ted C in forum Programming
    Replies: 14
    Last Post: 03-15-2011, 01:25 PM

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