PC Review


Reply
Thread Tools Rate Thread

Concatenate Function

 
 
Steve
Guest
Posts: n/a
 
      15th Sep 2009
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
--

 
Reply With Quote
 
 
 
 
Ken Snell MVP
Guest
Posts: n/a
 
      15th Sep 2009
What field(s) does qryMapMethod return? You must give an SQL statement to
the function that identifies the field that you want to concatenate into one
string. For example:

Concatenate("SELECT Field1 FROM qryMapMethod")

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


"Steve" <(E-Mail Removed)> wrote in message
news:580244BA-0987-450A-B9A7-(E-Mail Removed)...
> 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
> --
>



 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      15th Sep 2009
Thanks Ken,

If I pass the SQL statement to the function as below, it works fine. Thanks.

Concatenate("SELECT tblMapMethod.chrMapMethod FROM tblMapMethod;").

Is there a way to write the function so that you can pass the name of a
query instead of having to pass the SQL statement itself? Thanks again.
--
Steve


"Ken Snell MVP" wrote:

> What field(s) does qryMapMethod return? You must give an SQL statement to
> the function that identifies the field that you want to concatenate into one
> string. For example:
>
> Concatenate("SELECT Field1 FROM qryMapMethod")
>
> --
>
> Ken Snell
> <MS ACCESS MVP>
> http://www.accessmvp.com/KDSnell/
>
>
> "Steve" <(E-Mail Removed)> wrote in message
> news:580244BA-0987-450A-B9A7-(E-Mail Removed)...
> > 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
> > --
> >

>
>
>

 
Reply With Quote
 
Ken Snell MVP
Guest
Posts: n/a
 
      15th Sep 2009
Well, yes, but how will the function be told what field to use?

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


"Steve" <(E-Mail Removed)> wrote in message
news:1977D4E3-5E71-4E31-B30C-(E-Mail Removed)...
> Thanks Ken,
>
> If I pass the SQL statement to the function as below, it works fine.
> Thanks.
>
> Concatenate("SELECT tblMapMethod.chrMapMethod FROM tblMapMethod;").
>
> Is there a way to write the function so that you can pass the name of a
> query instead of having to pass the SQL statement itself? Thanks again.
> --
> Steve
>
>
> "Ken Snell MVP" wrote:
>
>> What field(s) does qryMapMethod return? You must give an SQL statement to
>> the function that identifies the field that you want to concatenate into
>> one
>> string. For example:
>>
>> Concatenate("SELECT Field1 FROM qryMapMethod")
>>
>> --
>>
>> Ken Snell
>> <MS ACCESS MVP>
>> http://www.accessmvp.com/KDSnell/
>>
>>
>> "Steve" <(E-Mail Removed)> wrote in message
>> news:580244BA-0987-450A-B9A7-(E-Mail Removed)...
>> > 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
>> > --
>> >

>>
>>
>>



 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      15th Sep 2009
Try using:

Concatenate(CurrentDb.QueryDefs("qryMapMethod").SQL)


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Steve" <(E-Mail Removed)> wrote in message
news:1977D4E3-5E71-4E31-B30C-(E-Mail Removed)...
> Thanks Ken,
>
> If I pass the SQL statement to the function as below, it works fine.
> Thanks.
>
> Concatenate("SELECT tblMapMethod.chrMapMethod FROM tblMapMethod;").
>
> Is there a way to write the function so that you can pass the name of a
> query instead of having to pass the SQL statement itself? Thanks again.
> --
> Steve
>
>
> "Ken Snell MVP" wrote:
>
>> What field(s) does qryMapMethod return? You must give an SQL statement to
>> the function that identifies the field that you want to concatenate into
>> one
>> string. For example:
>>
>> Concatenate("SELECT Field1 FROM qryMapMethod")
>>
>> --
>>
>> Ken Snell
>> <MS ACCESS MVP>
>> http://www.accessmvp.com/KDSnell/
>>
>>
>> "Steve" <(E-Mail Removed)> wrote in message
>> news:580244BA-0987-450A-B9A7-(E-Mail Removed)...
>> > 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
>> > --
>> >

>>
>>
>>



 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      15th Sep 2009
Sure there is. You might try

Set db = CurrentDb

'Add this line that assumes that the query String will always start with
'SELECT plus a space. If it doesn't then it assumes the pstrSQL is the
'name of a query. I would probably institute something that checked
'for the existence of such a query and that the query was a SELECT query.
'Also, they query could not have any parameters in it, so I would suspect
'doing this would be close to useless.

IF Not(pStrSQL Like "SELECT *") THEN
pstrSQL = db.QueryDefs(pStrSQL).SQL
End if

Set rs = db.OpenRecordset(pstrSQL, dbOpenDynaset)

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

Steve wrote:
> Thanks Ken,
>
> If I pass the SQL statement to the function as below, it works fine. Thanks.
>
> Concatenate("SELECT tblMapMethod.chrMapMethod FROM tblMapMethod;").
>
> Is there a way to write the function so that you can pass the name of a
> query instead of having to pass the SQL statement itself? Thanks again.

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      16th Sep 2009
"Steve" <(E-Mail Removed)> wrote in message
news:580244BA-0987-450A-B9A7-(E-Mail Removed)...
> 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with Concatenate function Snowy Microsoft Excel Programming 7 10th Jan 2011 07:13 AM
Concatenate: in an IF Function Tracey Microsoft Excel Worksheet Functions 6 16th Mar 2009 06:13 PM
Using the concatenate function BIAKathy Microsoft Excel Misc 3 9th Jan 2009 12:46 AM
Concatenate Function =?Utf-8?B?TG91?= Microsoft Excel Worksheet Functions 3 18th Oct 2004 03:49 AM
Concatenate Function anne Microsoft Excel Worksheet Functions 1 18th Jul 2003 01:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:43 PM.