Solved

VBA function with recordset error

Posted on 2013-11-20
6
625 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
Comment Utility
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

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:TOWELLR
Comment Utility
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
Comment Utility
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
Comment Utility
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)

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

6 Experts available now in Live!

Get 1:1 Help Now