Solved

Access 2013: working query cannot be read through DAO recordset

Posted on 2015-02-18
3
347 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 50

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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 …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
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