We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

MS Access module to convert table rows into comma delimeted values

csouthards
csouthards asked
on
Medium Priority
655 Views
Last Modified: 2012-05-06
I have been working with a module code created in Access 2000 to generate comma delimited values from multiples rows of a table.

The module has two routines:

1) Create a table called CustPurchItems, if it does not exist, with two columns CustomerID and ItemID.
2) Table "SalesRegWhsleOnly-Items" has the same 2 columns CustomerID and ItemID.  The code is supposed to group all similar records from CustomerID and concatenate their ItemID column values into a comma delimited text string.  The new concatenated data is then copied into the previously created table "CustPurchItems".

I have a simple form to execute the module code, but in Access 2007 it stalls and will not complete.  The database is in Access 2002/2003 format, but is being used in Access 2007.  Please let me know if there is something wrong with my coding.  
Option Compare Database
Option Explicit
Break -------
Public Function FixTable() As Boolean
On Error Resume Next
 
Dim db As DAO.Database, rst As DAO.Recordset, sSQL As String
Dim strCustomerID As String, strItemID As String
 
Set db = CurrentDb()
Call RecreateTables(db)
 
'Stop
sSQL = "SELECT CustomerID, ItemID FROM SalesRegWhsleOnly-Items " _
       & "ORDER BY CustomerID, ItemID ASC"
Set rst = db.OpenRecordset(sSQL, dbOpenSnapshot)
 
If Not rst.BOF And Not rst.EOF Then
  rst.MoveFirst
  strCustomerID = rst!CustomerID
  strItemID = rst!ItemID
  
  rst.MoveNext
  Do Until rst.EOF
    If strCustomerID = rst!CustomerID Then
      strItemID = strItemID & ", " & rst!ItemID
    Else
      sSQL = "INSERT INTO CustPurchItems (CustomerID, ItemID)  " _
           & "VALUES('" & strCustomerID & "','" & strItemID & "')"
      db.Execute sSQL
      strCustomerID = rst!CustomerID
      strItemID = rst!ItemID
    End If
    rst.MoveNext
  Loop
  
  ' Insert Last Record
    sSQL = "INSERT INTO CustPurchItems (CustomerID, ItemID)  " _
         & "VALUES('" & strCustomerID & "','" & strItemID & "')"
    db.Execute sSQL
End If
 
Set rst = Nothing
Set db = Nothing
 
'DoCmd.OpenForm "frmOutput"
 
End Function
Break ---------------
Public Function RecreateTables(ByRef dbs As DAO.Database)
On Error Resume Next
Dim sSQL As String
 
' Delete Table, if exists
If DCount("*", "MsysObjects", "[Name]='CustPurchItems'") = 1 Then
    DoCmd.DeleteObject acTable, "CustPurchItems"
End If
 
' Create Temp Table
sSQL = "SELECT CustomerID, ItemID INTO CustPurchItems " _
       & "FROM SalesRegWhsleOnly-Items WHERE 1 = 0;"
dbs.Execute sSQL
 
End Function

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
Not much info. What is the error?

Code looks OK, but it may not like (ByRef dbs As DAO.Database).
In that function I would just use:

Set dbs = CurrentDb()

/gustav

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Apparently, the problem was the name of my table.  I simply changed the name to "tblSales" rather than SalesRegWhsleOnly-Items and it worked.  Thanks for the assitance.
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Maybe you could have used [SalesRegWhsleOnly-Items] in the SQL.

/gustav
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.