473,651 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to set parameter on query when using DoCmd.OpenQuery

167 New Member
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 "ActionTake n", 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.Query Defs(qry_AssetActio nTaken_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 14965
NeoPa
32,568 Recognized Expert Moderator MVP
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 Recognized Expert Moderator Top Contributor
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
12065
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 to know this number after the first FETCH, isn't it? On a side note, why queries using LIMIT are SO terribly slow, compared to cursors and sometimes even ones without LIMIT? Shouldn't LIMIT be internally implemented using cursor mechanism then?...
3
17224
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 if for some reason a field is missing a value that's required, Access throws up an error. At that point I just want to end the process and notify them of the error so they can correct the data and try the process again. Here's my code:
0
1694
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 a parameter query somewhere on it (for me it was in a couple of combo boxes), and you _sometimes_ get a dialog box asking you for the parameter. If you've worked with parameter queries before you know the box I'm talking about. Here's the MS...
10
6721
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 contains only tables. I have linked the tables for the front end to the back end database. I am trying to set the recordsource of a form to a query established by the user to narrow the scope but I don't want to display the form if there are no...
4
2080
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 problem is that for the final user interface, I had planned to use a "frmDisplay" form which had a number of menu items and then depending on what the user selected, it would display the appropraite form in a subform beneath the menu items. I did this...
8
20761
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 missing something, however. Here's what I have: Dim db As Database Dim rs As Recordset Dim rs2 As Recordset
2
5256
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 this?". Outlook then waits for a response. Since I want the message to be sent out from an unattended applicaton when certain conditions are met, is there a way to supress or bypass this message?
10
7615
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 into a Front End/ Back End, which is very sensible. However, there are a number of ad hoc queries that are required to be available to all users, which can be edited, deleted or created "on the fly". The most sensible solution would seem to be...
2
5021
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 Work Finish Time. The query works perfectly. I have insert a Subform in my main form (assigned to this query) to retreive this query data and display. but I dont want to open the query in a seperate window as I have the subform in my Main Form to...
0
8352
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.