Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Access 2013: working query cannot be read through DAO recordset

Posted on 2015-02-18
3
Medium Priority
?
373 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 52

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

610 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