Results 1 to 2 of 2
  1. #1
    Matt18 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    1

    Concatenate function problem

    Hi



    I have a database with customer ID and their answer as shown below. What I would like to do is create a query that will group by ID and show Answer separated by comma.


    Table

    ID Answer
    1 a
    1 b
    2 a
    2 b
    5 a
    5 b

    Query
    1 a,b
    2 a,b
    5 a,b

    Access does not support GROUP_CONCAT I think so I looked for a function that would do that for me.

    I found a sample database (old one) in .mdb format that does that perfectly. Function is pasted bellow. But as I copy the module to my database (.mdb or .accdb), I get error:

    Compile error:
    User-defined type not defined

    I have "Microso... 14.0 Access database engine Object Library" checked under tools and references

    Code from the module is:

    Code:
    Option Compare Database
    Option Explicit
    
    Public Function Conc(Fieldx, Identity, Value, Source) As Variant
      Dim cnn As ADODB.Connection
      Dim rs As ADODB.Recordset
      Dim SQL As String
      Dim vFld As Variant
      
      Set cnn = CurrentProject.Connection
      Set rs = New ADODB.Recordset
      vFld = Null
      
      SQL = "SELECT [" & Fieldx & "] as Fld" & _
    		" FROM [" & Source & "]" & _
    		" WHERE [" & Identity & "]=" & Value
      
      ' open recordset.
      rs.Open SQL, cnn, adOpenForwardOnly, adLockReadOnly
      
      ' concatenate the field.
      Do While Not rs.EOF
    	If Not IsNull(rs!Fld) Then
    	  vFld = vFld & ", " & rs!Fld
    	End If
    	rs.MoveNext
      Loop
      ' remove leading comma and space.
      vFld = Mid(vFld, 3)
      
      Set cnn = Nothing
      Set rs = Nothing
      
      ' return concatenated string.
      Conc = vFld
    End Function
    Thank you very much for your help in advance!

  2. #2
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    Creation of the connection and recordset may have changed.

    I found several references of this sort:

    dim myconnection, myrecordset, name
    set myconnection = server.createobject("ADODB.Connection")
    set myrecordset = server.createobject("ADODB.Recordset")

    I have a legacy DB migrated to Access 2007 and I didn't have any
    problems with working with recordsets.

    Good luck.
    Last edited by hertfordkc; 11-19-2011 at 05:39 AM. Reason: Corrected line wrap

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

Similar Threads

  1. Problem with IIF function
    By Hulk in forum Forms
    Replies: 3
    Last Post: 03-20-2011, 12:59 PM
  2. DCount function problem
    By 10 Gauge in forum Forms
    Replies: 5
    Last Post: 02-28-2011, 02:08 PM
  3. Replies: 1
    Last Post: 08-18-2010, 02:05 PM
  4. VBA Function problem
    By smikkelsen in forum Programming
    Replies: 5
    Last Post: 07-16-2010, 07:46 AM
  5. dlookup function problem
    By bdaniel in forum Programming
    Replies: 3
    Last Post: 04-26-2010, 05:55 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