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

    Listbox Issue

    I am trying to add information into a table based on all selected items in a listbox.

    tbl2Campaign_Trainers is the table
    has three fields.
    field1 = CampaignID
    field2 = TrainerID
    field3 = Active (yes/no field, this is used in a query subform later)

    frmAssignCampaign is the form.
    has a combobox and a listbox.


    combobox - (lets you select the Trainer and bound to TrainerID)
    listbox - (lets you select which campaigns are assigned to the trainer and bound to CampaignID)

    Code:
    Private Sub Command20_Click()
        Dim conceptValue As String
        Dim strInsert As String
        Dim i As Variant
    
    
        For Each i In Me.lstCampaigns.ItemsSelected
            conceptValue = conceptValue & Me.lstCampaigns.ItemData(i)
        Next i
    
    
        strInsert = "INSERT INTO tbl2Campaign_Trainers(CampaignID) " & "VALUES('" & conceptValue & "');"
        DoCmd.RunSQL strInsert
        
        DoCmd.Close acForm, "frmAssignCampaign", acSaveNo
    End Sub
    The code is listed above. All that the code successfully does right now is adds the TrainerID to the table but keeps the CampaignID blank.
    Having the information saved in multiple records is fine, actually preferred, as that will keep the option to only display active campaigns in the query subform later.
    What in my code needs to be changed to make this successful?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    If table and controls are bound, why are you using code to create record? Bound means the ControlSource property is set to a field of form RecordSource. Selected values in bound combo and list boxes are passed directly to table. If you want to use a multi-select listbox and code to create records, do not use bound controls.

    And the SQL action will have to populate both TrainerID and CampaignID fields. Do you want the Active field set to Yes? Is the field DefaultValue property set for Yes?
    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.

  3. #3
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    I attempted your approach. I removed the code and just used the controls as is.
    When clicking the btnSave, it only adds the TrainerID to the table and does not list the CampaignID that were selected in the listbox. The way that they table is currently structured is that it would require a new record for each TrainerID/CampaignID combination.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    If you want to automate creating multiple records, must use code. My point was cannot also have the controls bound to fields.

    If TrainerID and CampaignID are number type fields, do not use apostrophe delimiters.

    "INSERT INTO tbl2Campaign_Trainers(TrainerID, CampaignID) VALUES('" & Me.TrainerID & "', '" & conceptValue & "');"
    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
    Finally got it to work and here is the code:

    Code:
    Private Sub Command20_Click()    Dim i As Variant
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
        Dim qd As DAO.QueryDef
        
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset("tbl2Campaign_Trainers", dbOpenTable)
        
        For Each i In lstCampaign.ItemsSelected
            rst.AddNew
            rst!CampaignID = lstCampaign.ItemData(i)
            rst!TrainerID = Me.cboTrainer
            rst.Update
        Next i
        Set rst = Nothing
        Set qd = Nothing
        DoCmd.Close acForm, "frmAssignCampaign", acSaveNo
    End Sub

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    That's one way, however, your original code method should have worked as well.

    Glad you got a solution.
    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.

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

Similar Threads

  1. Multi Select Listbox Issue
    By msk7777 in forum Access
    Replies: 12
    Last Post: 05-11-2015, 09:25 AM
  2. Replies: 2
    Last Post: 03-23-2014, 06:50 AM
  3. Listbox - Column Widths issue
    By Ganesh7299 in forum Access
    Replies: 12
    Last Post: 01-09-2014, 09:38 AM
  4. Replies: 3
    Last Post: 12-13-2012, 04:40 AM
  5. Replies: 1
    Last Post: 09-10-2012, 11:21 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