?
Solved

Access 2013: working query cannot be read through DAO recordset

Posted on 2015-02-18
3
Medium Priority
?
363 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
[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
3 Comments
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 2000 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 58
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Suggested Courses

770 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