473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to set parameter on query when using DoCmd.OpenQuery

167 100+
Using MS Access 2007. I built a form that has a combo box of lists of 'actions' (cmboActions). This form also has a button to click that I want to run a query and display the results in datasheet view.
I am trying to pass the value from the cmboActions box to the query via the following method:

Expand|Select|Wrap|Line Numbers
  1.   Dim qdf As QueryDef
  2.   Dim rst As Recordset
  3.  
  4.   Set qdf = CurrentDb.QueryDefs(qry_ActionTaken_parm)
  5.   qdf.Parameters(0) = cmboActions.value
  6.   Set rst = qdf.OpenRecordset
  7.  
  8.   DoCmd.OpenQuery "qry_ActionTaken_parm"  rst.Close
  9.   qdf.Close
  10.   Set rst = Nothing
  11.   Set qdf = Nothing
I am not sure how to set the parm up on the query. In the column on the query for "ActionTaken", how do I define the parameter (I currently have: [Forms]![frm_Action]![cmboActions]? I am assuming that 'qdf.Parameters(0)' relates to the first parameter found defined on the query. When I run the code I am getting the following error:
Set qdf = CurrentDb.QueryDefs(qry_AssetActionTaken_parm)"Variable not defined" on the query name

OR.... is there a better way to accomplish what I am trying to do? Trying to avoid having to build a separate query for each different value of 'ActionTaken' Any help would be appreciated.
Mar 22 '11 #1
2 14902
NeoPa
32,556 Expert Mod 16PB
I struggled with this concept manfully for many weeks, until I realised they really didn't support passing parameters to the query when opened from the interface (which is essentially the same as using DoCmd.OpenQuery()). Maybe there's a good reason for that, but it always seemed an omission of something obviously beneficial to me.

Another question recently had the same problem (though they weren't looking to use parameters). What you can do most easily (There are a number of ways around this. Some more clumsy than others, but in different ways) is to reference the form's ComboBox control directly from within your query :
Expand|Select|Wrap|Line Numbers
  1. ...
  2. WHERE [YourField] = Forms("YourForm").cmboActions
Mar 23 '11 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Have you tried using quotes around the query name? They seem to be missing in the code you have posted.
Mar 23 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
3
by: jj | last post by:
I've got a form button that fires off 3 queries but if the first query returns an error, I don't want the other two queries to happen. Example: first query runs an insert from a linked table but...
0
by: Jon LaRosa | last post by:
I just spent about 2 hours pulling my hair out over this issue, and I don't want it to happen to anyone else ever again. Ever. Basically you try to close a form using "DoCmd.Close", the form has...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
4
by: Regnab | last post by:
I've got a form - "frmLookup" (with a subform) that works very happily on its own. The form has a list box, which when updated requeries the subform to display the appropriate results. The...
8
by: hbean | last post by:
Hi - I'm trying to run a query as part of a VBA procedure, and what I want it to do is to grab the value for a parameter (the current month) from another part of the procedure. I clearly am...
2
by: Mac Campbell | last post by:
When automating e-mail messages from Access 2003 using DoCmd.SendObject, I get an Outlook warning message "A program is trying to automatically send e-mail on your behalf. Do you want to allow...
10
by: teddysnips | last post by:
My clients have asked me to maintain a database that was developed in- house. It's pretty good, considering the developer isn't a "programmer". The first thing they want me to do is to split it...
2
by: Ceylon | last post by:
Hi, 1. I am doing a simple timesheet model in Access. When i click on Review button with given dates, this will run a query to find the Username, Start Date, Break Start time, Break End Time and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.