Results 1 to 3 of 3
  1. #1
    Paranap is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    30

    Selections from a list clearing before they can be updated when using DoCmd

    Hi

    I have a form linked to one table. It is a form for adding a new record of a lesson plan. This I've managed to do fine using the
    Code:
    DoCmd.RunCommand acCmdSaveRecord
    I have then tried to add a list box which adds training needs for a given lesson plan. As there are normally many training needs for one lesson plan it is in a seperate table linking by the lessonID. Again I can get this to work using the code
    Code:
    Set ctl = Me.lstTraining
      For Each varItem In ctl.ItemsSelected
        rs.AddNew
        rs!TrainingID = ctl.ItemData(varItem)
        rs!LessonID = Me.Text44
        MsgBox "now"
        rs.Update
    Problem is when I try to put them together in one function like command button the first part of the code seems to clear the selected items in the list. If you reselect the items and click it again it seems to work fine.

    Any help would be appreciated

    Neil

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    A For --Next construct has a For statement with some iterator/counter, some processing statement, and a Next statement.
    Please post all the code involved in the routine. Thanks for using code tags.

  3. #3
    Paranap is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    30
    Code:
      Dim db            As DAO.Database
      Dim rs            As DAO.Recordset
      Dim ctl           As Control
      Dim varItem       As Variant
       Set db = CurrentDb()
       DoCmd.RunCommand acCmdSaveRecord
     
     Set rs = db.OpenRecordset("tblLessonTraining", dbOpenDynaset, dbAppendOnly)
      Set ctl = Me.lstTraining
      For Each varItem In ctl.ItemsSelected
        rs.AddNew
        rs!TrainingID = ctl.ItemData(varItem)
        rs!LessonID = Me.Text44
        MsgBox "now"
        rs.Update
      Next varItem
    
      Set rs = Nothing
      Set db = Nothing
    Sorry must have missed the bottom bit off when copying

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

Similar Threads

  1. Copy list box selections to another list box
    By Gotham_Knight in forum Forms
    Replies: 1
    Last Post: 03-07-2013, 10:40 AM
  2. List Box Selections
    By MJeanBap in forum Forms
    Replies: 10
    Last Post: 04-05-2012, 06:26 AM
  3. Replies: 1
    Last Post: 03-05-2012, 04:21 PM
  4. Multiple Selections from a List Box
    By oleBucky in forum Forms
    Replies: 4
    Last Post: 05-06-2011, 08:24 AM
  5. Clear selections from a List Box?
    By Dreams2Fly in forum Forms
    Replies: 3
    Last Post: 03-28-2011, 02: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