why won't "Dim rs As DAO.Recordset" compile

TyeReece

Board Regular
Joined
Aug 3, 2007
Messages
136
I am a novice so I apologize if this is a dumb question. Long story short. I am trying to combine multiple records in a query. I have an ID field and a field of names of diagnoses. My goal is to have one row per ID with the diagnoses combined in one field. Everything I read on line says Allen Browne's concatrelated() function is the way to go. So I paste the function into a module and when I try to compile it stops at "Dim rs As DAO.Recordset". Here is the link to his site if you need it. http://allenbrowne.com/func-concat.html I have also pasted the code below. Any help is appreciated.
Code:
Public Function ConcatRelated(strField As String, _
    strTable As String, _
    Optional strWhere As String, _
    Optional strOrderBy As String, _
    Optional strSeparator = ", ") As Variant
On Error GoTo Err_Handler
    'Purpose:   Generate a concatenated string of related records.
    'Return:    String variant, or Null if no matches.
    'Arguments: strField = name of field to get results from and concatenate.
    '           strTable = name of a table or query.
    '           strWhere = WHERE clause to choose the right values.
    '           strOrderBy = ORDER BY clause, for sorting the values.
    '           strSeparator = characters to use between the concatenated values.
    'Notes:     1. Use square brackets around field/table names with spaces or odd characters.
    '           2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
    '           3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
    '           4. Returning more than 255 characters to a recordset triggers this Access bug:
    '               [url]http://allenbrowne.com/bug-16.html[/url]
    Dim rs As DAO.Recordset         'Related records
    Dim rsMV As DAO.Recordset       'Multi-valued field recordset
    Dim strSql As String            'SQL statement
    Dim strOut As String            'Output string to concatenate to.
    Dim lngLen As Long              'Length of string.
    Dim bIsMultiValue As Boolean    'Flag if strField is a multi-valued field.
    
    'Initialize to Null
    ConcatRelated = Null
    
    'Build SQL string, and get the records.
    strSql = "SELECT " & strField & " FROM " & strTable
    If strWhere <> vbNullString Then
        strSql = strSql & " WHERE " & strWhere
    End If
    If strOrderBy <> vbNullString Then
        strSql = strSql & " ORDER BY " & strOrderBy
    End If
    Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
    'Determine if the requested field is multi-valued (Type is above 100.)
    bIsMultiValue = (rs(0).Type > 100)
    
    'Loop through the matching records
    Do While Not rs.EOF
        If bIsMultiValue Then
            'For multi-valued field, loop through the values
            Set rsMV = rs(0).Value
            Do While Not rsMV.EOF
                If Not IsNull(rsMV(0)) Then
                    strOut = strOut & rsMV(0) & strSeparator
                End If
                rsMV.MoveNext
            Loop
            Set rsMV = Nothing
        ElseIf Not IsNull(rs(0)) Then
            strOut = strOut & rs(0) & strSeparator
        End If
        rs.MoveNext
    Loop
    rs.Close
    
    'Return the string without the trailing separator.
    lngLen = Len(strOut) - Len(strSeparator)
    If lngLen > 0 Then
        ConcatRelated = Left(strOut, lngLen)
    End If

Exit_Handler:
    'Clean up
    Set rsMV = Nothing
    Set rs = Nothing
    Exit Function

Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
    Resume Exit_Handler
End Function
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

mgd_Excel

New Member
Joined
May 18, 2014
Messages
10
You did not mention if you had the reference checked. Go to tools reference and check Microsoft DAO 3.6 Library or some thing of the effect.
If you also lookup how excel deals with early binding and late binding this may clear up some of the mystery.
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,832
Office Version
  1. 2019
Platform
  1. Windows
What version of Access are you using?
 

TyeReece

Board Regular
Joined
Aug 3, 2007
Messages
136
Unfortunately, I do not understand what you mean by having the reference checked. I looked in Access Tools and don't see anything about Microsoft Library.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,034
Office Version
  1. 365
Platform
  1. Windows
From the VB Editor, select Tools then References. It will list all the libraries and show which ones are selected.
A "Microsoft DAO..." library needs to be selected. If one is not, then select it from the list.
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,832
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

Note that on my access the library I have for Access is just Microsoft Office 15.0 Access database engine Object Library.

Edit: In your case it would probably be 16.0 that applies.



If you are having trouble list here what IS checked.
 
Last edited:

TyeReece

Board Regular
Joined
Aug 3, 2007
Messages
136
THANK YOU! It compiled without an issue. This will save me so much time. Thanks!
 

TyeReece

Board Regular
Joined
Aug 3, 2007
Messages
136
OK. my query runs but doesn't concatenate the field I am trying to combine. I get the ID and every diagnosis attached to them in separate rows. Below is the code I have that runs. I think the issue I am having has to do with third and/or forth arguments of the concatrelated function (bolded below). I have tried to use the RevNum and ReviewNum in place of the bolded code and it doesn't work. Any ideas on what I am doing wrong?

Code:
SELECT 
	[Data Request Dr Lohr TBL].ID, 
	[Data Request Dr Lohr TBL].RevNum, 
	ConcatRelated("DSM_IV","DSM_IV_List","DSMIV_ID = " & [DSM_IV_List]![DSMIV_ID]) AS Diagnoses
FROM 
	([Data Request Dr Lohr TBL] 
	INNER JOIN DSM_IV_Diagnosis 
	ON [Data Request Dr Lohr TBL].[RevNum] = DSM_IV_Diagnosis.ReviewNum) 
	INNER JOIN DSM_IV_List 
	ON DSM_IV_Diagnosis.DSMIV_ID = DSM_IV_List.DSMIV_ID;
 
Last edited by a moderator:

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,832
Office Version
  1. 2019
Platform
  1. Windows
You probably have to supply sample data from the tables involved. Mostly likely the function is doing what it is supposed to and you have either set it up wrong or are just using it incorrectly or in a situation where it doesn't apply.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,291
Messages
5,641,373
Members
417,206
Latest member
sam89665

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top