Solved

VBA function with recordset error

Posted on 2013-11-20
6
634 Views
Last Modified: 2013-11-20
I am trying to create a function that returns a concatenated string value from a recordset for a variable that gets passed in and searched for.

Function Code:

Public Function GetSessionRecs(p_sessionname As String)

strSQL = "Select Description from [Session Detail Table] where [SessionName] = p_sessionname "

Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)

   If rs.RecordCount = 0 Then
      'MsgBox "no records"
   Exit Function
   End If
   
   GetSessionRecs = ""
   
Do Until rs.EOF

  GetSessionRecs = GetSessionRecs & rs!Description

  rs.MoveNext
  Loop

rs.Close

End Function

When I call it from another block of code like this

p_sessionvalues = GetSessionRecs(p_sessionname)

I get:

Run-time error '3016':
Too few parameters. Expected 2.

Pointing to this line:
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
0
Comment
Question by:TOWELLR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
Anthony Berenguel earned 500 total points
ID: 39663979
Try changing this line
strSQL = "Select Description from [Session Detail Table] where [SessionName] = p_sessionname "

Open in new window

to
strSQL = "Select Description from [Session Detail Table] where [SessionName] ='" & p_sessionname & "'"

Open in new window

0
 
LVL 58
ID: 39663981
This:
strSQL = "Select Description from [Session Detail Table] where [SessionName] = p_sessionname "


needs to be:

strSQL = "Select Description from [Session Detail Table] where [SessionName] = " & chr$(34) & p_sessionname & chr$(34)

assuming session name is a string.  Also check spelling and spacing.   Anything that is not understood will become a parameter, which when executing in code, Access won't deal with.

Jim.
0
 
LVL 58
ID: 39663984
and by the way, so you don't re-invent the wheel:

"Domain Aggregate" for Concatenating Values by Group in Microsoft Access
http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_2380-Domain-Aggregate-for-Concatenating-Values-by-Group-in-Microsoft-Access.html

Jim.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:TOWELLR
ID: 39664026
OK It looks like this with a simpler numeric variable:

strSQL = "Select Description from [Session Detail Table] where [Item Code] ='" & p_itemcode & "'"
MsgBox "Func Select: " & strSQL

Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)

The MsgBox for the insert string looks clean:

Func Select: Select Description from [Session Detail Table] where [Item Code] ='1'

Now it is saying : too few parameters. Expected 1

I will read through the links provided.
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 39664051
ok. So  since it's a numeric field now you don't need the apostrophes. Change this line
strSQL = "Select Description from [Session Detail Table] where [Item Code] ='" & p_itemcode & "'"

Open in new window

to
strSQL = "Select Description from [Session Detail Table] where [Item Code] =" & p_itemcode 

Open in new window

0
 

Author Comment

by:TOWELLR
ID: 39664102
Solved:

My original syntax for the select was wrong.  Fixed it with the suggestion from aebea.  Thanks

When the error persisted and a read some other articles I knew it must be referring to a bad column somewhere which was my second issue.

The [Description] column was not in the table as Description.  It had another name.

Thanks to all for the help.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question