Results 1 to 9 of 9
  1. #1
    HansBades is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    85

    Inserting multiple records at once with SQL

    I have a vba script that inserts a record chosen from a list box into a table. But I have now made the list box multi-select. So when the user chooses multiple records from the table, I want to be able to insert them all at once. How is this usually done?

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,142
    Two ways, you'll need to create a loop of the selected items or create an In() clause for your sql statement that contains the ID's of the selected records.

    The second is more efficient than the loop as it does them all at once, although if it's only a few records you won't notice the difference.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    HansBades is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    85
    The In() clause worked great for me in another respect. I was able to use it for another issue I was having and it worked fantastic. But how do I use it to extract values from a listbox? I am trying to write a vba statement that inserts all of the values chosen in the listbox into the appropriate table.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    Still have to loop the listbox SelectedItems to build the IN() array. Then build the SQL statement which executes only once instead of for each iteration of loop.

    Common topic. Review http://allenbrowne.com/ser-50.html
    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
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,747
    Hi

    The following is Code that I have used to insert multiple selections from an Unbound Listbox:-

    Code:
    Private Sub cmdSet_Click()
    10    On Error GoTo myerror
     
          Dim rnSQL As String
          Dim varCustOrderID As Long
          Dim varItem As Variant
     
    20    On Error GoTo myerror
    30    For Each varItem In Me.List10.ItemsSelected
    40      varCustOrderID = Me.txtCustOrderID
           
    50      varItem = Me.List10.Column(0, varItem)
     
    60      rnSQL = "INSERT INTO tblOrderItemsSets  (CustOrderID,[SET]) Values ( " & varCustOrderID & " , '" & varItem & "')"
     
    70    Debug.Print rnSQL
    80    CurrentDb.Execute rnSQL
    90    Next varItem
    100   Forms!frmCustomers![frmOrderItemsSetssubform].Requery
     
    ExitHere:
     
    110   Exit Sub
     
    myerror:
    120   MsgBox Err.Description
    130   Resume ExitHere
     
    End Sub
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,858
    This is a public function I use with multiselect list boxes.
    It returns a string like 1,3,6,8 Which you can use in an In clause.
    It has options to select column, seperator characters, and delimiters.

    You call it by passing the listbox object
    Code:
    ...Where SomeID in(" & getLBX(Me.SomeListBox) & ")"
    Code:
    Public Function getLBX(lbx As ListBox, Optional intColumn As Variant = 0, Optional Seperator As String = ",", _
                           Optional delim As Variant = Null) As String
    
    'Iterates thru the multiselect listbox and constructs an array of the selected items
    'Arguments:
    'Lbx is Listbox Object ie.Me.MyListbox
    'intColumn is the column # to be returned
    'Seperator is the character seperating items in array returned
    'Delim is optional delimiter to be return in array ie. #1/1/2001#,#12/25/2015#
    
        Dim strlist As String
        Dim varSelected As Variant
    
        'On Error GoTo getLBX_Error
    
        If lbx.ItemsSelected.Count = 0 Then
            'MsgBox "Nothing selected"
        Else
    
            For Each varSelected In lbx.ItemsSelected
    
                If Nz(lbx.Column(intColumn, (varSelected)), "") <> "" Then
    
                    strlist = strlist & delim & lbx.Column(intColumn, (varSelected)) & delim & Seperator
    
                Else
    
                    strlist = strlist
    
    
                End If
    
            Next varSelected
    
            If Nz(strlist, "") <> "" Then
    
                strlist = Left$(strlist, Len(strlist) - 1)  'remove trailing comma
    
            End If
        End If
    
        getLBX = strlist
    
        On Error GoTo 0
        Exit Function
    
    getLBX_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure getLBX of Module modLBX"
    
    End Function
    Here's an example to show how it works
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,236
    Quote Originally Posted by June7 View Post
    Still have to loop the listbox SelectedItems to build the IN() array. Then build the SQL statement which executes only once instead of for each iteration of loop.

    Common topic. Review http://allenbrowne.com/ser-50.html
    Looping for Selected and one query, should be more efficient than looping for Selected and a query for each selected though, surely?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,173
    Quite right. Where I work developers that use RBAR (Row By Agonizing Row) programming get shot at sunrise by the DBA's.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    Quote Originally Posted by Welshgasman View Post
    Looping for Selected and one query, should be more efficient than looping for Selected and a query for each selected though, surely?
    That's what I suggested - build array string and execute SQL only once afterward.
    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. Replies: 1
    Last Post: 08-17-2020, 06:46 AM
  2. Replies: 4
    Last Post: 02-24-2017, 08:02 PM
  3. VBA Inserting two new records
    By lzook88 in forum Programming
    Replies: 2
    Last Post: 10-11-2015, 10:52 AM
  4. Inserting records in multiple tables
    By Nikos in forum Database Design
    Replies: 8
    Last Post: 02-17-2012, 02:35 PM
  5. Inserting multiple records into a table
    By New2Access in forum Programming
    Replies: 1
    Last Post: 07-07-2011, 09:18 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