Results 1 to 4 of 4
  1. #1
    Hobbes29 is offline Novice
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Posts
    10

    Split a string for use as query parameters.

    I am looking for help with SQL / query.



    I have a form that list tables in my MS Access 2007 DB. The purpose of the form is to cycle through the tables in my db and display the tables in a multiselect list box. I am only interested in the table names, because the user is going to select the tables and then using a SQL query, append the tables into a single table. Each table has identical fields.

    Table names can change, but my sample DB tables are called Table1, Table2, Table3, TableN where N is some number of tables in the DB.

    In my function testMultiselect_Click(), I pass all the tables the user selects to a string 'sTemp' separated by a comma in a text box. (The textbox is used for testing to make sure I am selecting the right tables.) An example of sTemp is "Table1, Table2 Table3".

    I am looking for assistance in writing the SQL that passes the selected tables from sTemp to a query/sql statement that appends the tables the user selects into a single table where the single table is called "tblCombine".

    testMultiselect_Click() is called from a button click.

    The code I have so far is:

    Code:
    Private Sub testmultiselect_Click()
     
        Dim oItem As Variant
        Dim sTemp As String
        Dim iCount As Integer
        Dim SQLText As String
     
     
        iCount = 0
     
        If Me!NamesList.ItemsSelected.Count <> 0 Then
            For Each oItem In Me!NamesList.ItemsSelected
     
                If iCount = 0 Then
                     sTemp = sTemp & Me!NamesList.ItemData(oItem)
                     iCount = iCount + 1
                Else
                     sTemp = sTemp & "," & Me!NamesList.ItemData(oItem)
                     iCount = iCount + 1
     
                     'JUST TESTING THE COUNT OF TABLES
                     MsgBox "I only selected after else: " & iCount
     
                     'THIS IS WHERE I WANT TO SPLIT sTemp AND CREATE A
                     'SQL TO TAKE THE TABLES I SELECT AND APPEND THEM TO
                     'A SINGLE TABLE CALLED tblCombine.
     
                 End If
             Next oItem
        Else
            MsgBox "Nothing was selected from the list", vbInformation
            Exit Sub 'Nothing was selected
        End If
     
        Me!mySelections.Value = sTemp
    End Sub
    Thanks,
    Ken

  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
    I don't think you want to combine them together in a string, unless I'm missing something. Within the loop, you could append each one:

    strSQL = "INSERT INTO tblCombine (Field1, Field2) SELECT Field1, Field2 FROM " & Me!NamesList.ItemData(oItem)

    CurrentDb.Execute strSQL
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Hobbes29 is offline Novice
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Posts
    10
    Paul,

    Thank you!. I used this string in the code and it did exactly what I needed.

    In excel I was able to fill an array with a list a strings and split the string for use and thought I could do something similar with Access but quickly learned VBA operates a little differently between access and excel.

    Ken

  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,521
    Happy to help. You can certainly split a string in Access, but it didn't seem that you needed to here.
    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. Split string into Date and Time Columns?
    By Hobbes29 in forum Queries
    Replies: 2
    Last Post: 06-08-2010, 06:50 PM
  2. Query NOT taking the two parameters WHY?
    By iamraja5 in forum Forms
    Replies: 7
    Last Post: 03-16-2010, 04:29 AM
  3. Split Query Field into Sub Queries/Tables
    By maggioant in forum Queries
    Replies: 0
    Last Post: 10-15-2009, 05:23 PM
  4. Add query parameters in with vbCode?
    By ~SwAmPdOnKeY~ in forum Programming
    Replies: 0
    Last Post: 08-07-2008, 07:58 PM
  5. Replies: 0
    Last Post: 12-05-2005, 04:09 AM

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