Re: Concatenate Function



"Steve" <Steve@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:580244BA-0987-450A-B9A7-710CF6678808@xxxxxxxxxxxxxxxx
Greetings:

I am attempting to use Duane Hookom's concatenate function (reproduced
below) to concatenate the results of a query named "qryMapMethod". When I
type:

Concatenate("qryMapMethod") into the immediate window I get the following
error:

"Runtime Error 3061. Too few parameters, expected 1" when it attempts to
execute "Set rs = db.OpenRecordset(pstrSQL, dbOpenDynaset)" line of code.

Any help in what I am doing wrong would be greatly appreciated. Thanks.

Steve

Function Concatenate(pstrSQL As String, Optional pstrDelim As String = "; ") _
As String
'Created by Duane Hookom, 2003
'this code may be included in any application/mdb providing
' this statement is left intact

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset(pstrSQL, dbOpenDynaset)

Dim strConcat As String 'build return string
With rs
If Not .EOF Then
.MoveFirst
Do While Not .EOF
strConcat = strConcat & _
.Fields(0) & pstrDelim
.MoveNext
Loop
End If
.Close
End With
Set rs = Nothing
'====== uncomment next line for DAO ========
Set db = Nothing
If Len(strConcat) > 0 Then
strConcat = Left(strConcat, _
Len(strConcat) - Len(pstrDelim))
End If
Concatenate = strConcat
End Function
--


Pardon me, but I think there may be some confusion here. Duane's Concatenate function works just fine if you pass it the name of a stored query, so long as that query has no parameters. My guess is that qryMapMethod has a parameter, such as (maybe) a reference to a control on a form. Unlike what Access does when you open a query using DoCmd.OpenQuery, DAO doesn't automatically resolve parameters.

If this is the reason for the error you're getting, you could modify Duane's function to get Access to resolve the parameters for you, like this:

'------ start of modified code ------
Function Concatenate( _
pstrSQL As String, _
Optional pstrDelim As String = "; ") _
As String

'Created by Duane Hookom, 2003
'this code may be included in any application/mdb providing
' this statement is left intact
'
' Modified by Dirk Goldgar, 15 September 2009, to resolve
' parameter references in stored queries.

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter

Set db = CurrentDb

If pstrSQL Like "SELECT *" Then
Set rs = db.OpenRecordset(pstrSQL, dbOpenDynaset)
Else
Set qdf = db.QueryDefs(pstrSQL)
With qdf
For Each prm In .Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rs = .OpenRecordset(dbOpenDynaset)
End With
End If

Dim strConcat As String 'build return string
With rs
If Not .EOF Then
.MoveFirst
Do While Not .EOF
strConcat = strConcat & _
.Fields(0) & pstrDelim
.MoveNext
Loop
End If
.Close
End With

Set rs = Nothing
'====== uncomment next line for DAO ========
Set db = Nothing
Set qdf = Nothing

If Len(strConcat) > 0 Then
strConcat = Left(strConcat, _
Len(strConcat) - Len(pstrDelim))
End If

Concatenate = strConcat

End Function
'------ end of modified code ------

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

.



Relevant Pages

  • Re: combined text fields using a GROUP BY statement
    ... Function Concatenate(pstrSQL As String, _ ... 'tblFamMem with FamID, FirstName, DOB,... ... 'Dim rs As DAO.Recordset ... Dim strConcat As String 'build return string ...
    (microsoft.public.access.queries)
  • Re: append memo fields together sequentially
    ... Did you go to the site and download the database and then extract the function and paste it into a module in your database. ... did you save the module with a name OTHER than Concatenate. ... Optional pstrDelim As String = ", ... Dim rs As DAO.Recordset ...
    (microsoft.public.access.queries)
  • Re: concatenate function
    ... delimit the specific string values of MU and Slave. ... 'tblFamMem with FamID, FirstName, DOB,... ... 'Dim rs As DAO.Recordset ... Dim strConcat As String 'build return string ...
    (microsoft.public.access.queries)
  • VBA Function error and display result in sheet
    ... 10 or eleven spaces) will not concatenate to the other ... Dim NoOfSpace As Variant ... Sub CreatePrn() ... Dim EmpNo, PayGp, Code, Prn, EffDate, Sal As String ...
    (microsoft.public.excel.programming)
  • Re: VBA Function error and display result in sheet
    ... > I have the following vba code and the FuncSpaces variable will not concatenate to the other> strings. ... > Dim NoOfSpace As Variant ... > Sub CreatePrn() ... > Dim EmpNo, PayGp, Code, Prn, EffDate, Sal As String> ...
    (microsoft.public.excel.programming)