Solved

VBA function with recordset error

Posted on 2013-11-20
6
626 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
  • 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 57
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 57
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now