Solved

Access 2013: working query cannot be read through DAO recordset

Posted on 2015-02-18
3
336 Views
Last Modified: 2016-02-10
Hi

I'm experiencing a problem that I have never seen before:
- I have an Access query on a join of 3 tables in SQL Server. When I run that query manually, it works perfectly
- I need to use that query in a VBA routine, so I set a DAO recordset using  the usual

Set rs = CurrentDb.OpenRecordset("SELECT * FROM queryName", dbOpenDynaset)

- the latter gives me either an error "Too few parameters. Expected 1.", or no error, but the recordset is set to nothing !

I mean, I have done this in 1000 other cases without the smallest trouble. What can be the reason for such an error ??

The strange thing is that, in the Access app, I need to access some tables over ADODB, so I have a reference to the ADO library. When I looked at my references, I noticed the DAO ref was missing, so I tried to add it. This, however, gives an error "Name conflicts with existing module, project, or object libray", which I also have never seen before. But when I dimension the recordset mentioned above as a DAO recordset, Access happily accepts it and compiles.

So I'm pretty lost and totally stuck with that application.

Thanks for help
Bernard
0
Comment
Question by:bthouin
3 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40616337
This often means that your query references a form with [Forms]![YourFormName]![SomeFieldName]

This you will have to supply when running from code:

Set db = CurrentDb
Set qd = db.QueryDefs("queryName")
qd.Parameters(0).Value = [Forms]![YourFormName]![SomeFieldName]
Set rs = qd.OpenRecordset()

/gustav
0
 
LVL 1

Author Comment

by:bthouin
ID: 40616345
Oh, Gustav, you are spot on ! That is indeed the case.

You saved my day, I was getting desperate...
0
 
LVL 57
ID: 40616351
When you open a query in code, resolving any references is left up to you.

If all your references are form/control references, then you can do it like this:

Dim db As Database
Dim qdef As QueryDef
Dim prm as Parameter
Dim rs As Recordset


Set db = CurrentDb()
Set qdef = db.QueryDefs(source)
For Each prm in qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rs = qdef.OpenRecordset()  


You could also set the paramertes by name:

qdef.Parameters("<name>") = Eval(qdef.Parameters(0).Name)  

and if the name of the parameter is not a reference, then you must set it directly:

qdef.Parameters(0)  =  strMyCrieria  

For example.

Jim.
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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

932 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

13 Experts available now in Live!

Get 1:1 Help Now