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

    Question Adding all items in ListBox



    Code:
    Private Sub cmdSaveClass_Click()
    Dim MyDB As DAO.Database
    Dim varItem As Variant
    Dim lst As ListBox
    
    
    Dim rst As DAO.Recordset
    
    
    'If lstApprovedTrainers.ItemsSelected.Count = 0 Then
    'MsgBox "Please select at least 1 (one) trainer to add to this grant.", vbOKOnly, "Error"
    'Exit Sub
    'End If
    
    
    Set MyDB = CurrentDb
    Set rst = MyDB.OpenRecordset("tbl2ClassTaken", dbOpenDynaset, dbAppendOnly)
    
    
    With rst
    For Each varItem In lstSelectedStudents
    .AddNew
    ![StudentID] = lstSelectedStudents.ItemData(varItem)
    ![ClassID] = cboClassSelection
    ![ClassDate] = txtClassDate
    ![HoursTaken] = txtHoursTaken
    .Update
    Next varItem
    End With
    
    
    DoCmd.Close acForm, "frmAddClassRoster"
    
    
    rst.Close
    Set rst = Nothing
    
    
    DoCmd.Maximize
    End Sub
    I am getting the following error on the "For Each varItem In lstSelectedStudents" line

    Run-Time Error '438'
    Object doesn't support this property or method


    Now I know this procedure works as I am using it on a different form. I changed all the information to reflect the new table I am trying to save to as well as referring to what fields I am trying to gather that information from. The other time it is used it just for selections in a listbox. This time I have created 2 listboxes. The first listbox contains all students that have ever been entered into the database. The second listbox contains only those that have been moved to the listbox from list1, removing it from list1 in the process. Any ideas on how to have the listbox cycled through similar to when .ItemsSelected is being used, but this time to have all that are listed.

  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,521
    A For/Next loop:

    For x = 0 To Me.ListboxName.ListCount - 1
    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
    Thanks. That got me past that error and it appears to be stepping through the code properly now. Only issue is after the code is run and the form is closed. I go to check the table for verification and no data is added.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Maybe need to programmatically select each item in the listbox. Otherwise, you aren't looking at each item in the listbox, just running the loop however many times as there are items in the list. But those items are not selected and the code is not pulling values. However, I would have expected that many records with the StudentID field blank - unless it is a required field, in which case the update fails.

    Me.lstSelectedStudents.Selected(x) = True
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    That makes complete sense.

    I have switched it up and added your line just below the "for x = 0" line
    It appears to accept the code line, but still errors after highlighting the ".update"
    It is giving an error to please enter number only.
    The only field that isnt an exact number would be the ClassDate field, which is set up for Date/Time.

  6. #6
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    It seemed to be a validation rule I had entered a while back, that I have removed and it now works...partially...

    It now adds new data and appears to be working. But it does not seem to change the StudentID. All new records that are added match the first entry in the listbox.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You haven't shown the new code, but you'd need to use the For/Next counter variable.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    Here is the new code:

    Code:
    Private Sub cmdSaveClass_Click()
    Dim MyDB As DAO.Database
    Dim varItem As Variant
    Dim lst As ListBox
    
    
    Dim rst As DAO.Recordset
    
    
    'If lstApprovedTrainers.ItemsSelected.Count = 0 Then
    'MsgBox "Please select at least 1 (one) trainer to add to this grant.", vbOKOnly, "Error"
    'Exit Sub
    'End If
    
    
    Set MyDB = CurrentDb
    Set rst = MyDB.OpenRecordset("tbl2ClassTaken", dbOpenDynaset, dbAppendOnly)
    
    
    With rst
    For x = 0 To Me.lstSelectedStudents.ListCount - 1
    Me.lstSelectedStudents.Selected(x) = True
    .AddNew
    ![StudentID] = lstSelectedStudents.ItemData(varItem)
    ![ClassID] = cboClassSelection
    ![ClassDate] = txtClassDate
    ![HoursTaken] = txtHoursTaken
    .Update
    Next
    End With
    
    
    DoCmd.Close acForm, "frmAddClassRoster"
    
    
    rst.Close
    Set rst = Nothing
    
    
    DoCmd.Maximize
    End Sub
    I am unsure of how to have it switch to the new variable during the process.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Is the second listbox set for multi-select, try single?

    Code options:

    Me.lstSelectedStudents.ListIndex = x

    Or set value of listbox and just reference the listbox value.

    Me.lstSelectedStudents = Me.lstSelectedStudents.ItemData(x)
    ...
    ![StudentID] = Me.lstSelectedStudents
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    Multi-select is set to none.

    Should it be set to simple or extended?

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You're using varItem instead of x.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    Switching it to x instead of varItem seems to have fixed it. Thanks for the help again guys!

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    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. Replies: 2
    Last Post: 03-27-2015, 08:48 AM
  2. how to de-select items in a listbox
    By CharbelKahi in forum Programming
    Replies: 1
    Last Post: 06-23-2014, 07:05 AM
  3. Replies: 7
    Last Post: 02-01-2013, 02:58 PM
  4. Replies: 8
    Last Post: 05-24-2011, 03:41 PM
  5. Selected items in listbox
    By tomodachi in forum Access
    Replies: 1
    Last Post: 09-09-2010, 01:14 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