473,503 Members | 7,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass Parameter Value in Query via VBA

124 New Member
I have a saved query (q_StdDegReqAll) that has one parameter (WHERE StdDegReq.StdDegReqId = lngStdDegReqId). I want to pass a value into this parameter in vba. I have the following code:

Expand|Select|Wrap|Line Numbers
  1. Dim lngStdDegReqId As Long
  2. Dim qd1 As DAO.QueryDef
  3.  
  4. Set qd1 = CurrentDb.QueryDefs("q_StdDegReqAll")
  5. qd1.Parameters(lngStdDegReqId) = lngStdDegReqId
  6.  
  7. DoCmd.OpenQuery "q_StdDegReqAll", , "acReadOnly"
  8.  
When I execute the code I get the error: "Item not found in this collection". It's referring to my 4th line. I do not know what I am doing wrong.
May 6 '10 #1
13 62284
Jim Doherty
897 Recognized Expert Contributor
@bullfrog83
Yes.

qd1.Parameters(lngStdDegReqId) = lngStdDegReqId

Passing lngStdDegReqId unto itself? where is lngStdDegReqId defined value
May 6 '10 #2
bullfrog83
124 New Member
Sorry. It's defined at the beginning of the routine where a value is passed into it:

Public Sub procEvaluateDegReq(lngStdDegReqId As Long)
May 6 '10 #3
NeoPa
32,557 Recognized Expert Moderator MVP
This is a tricky area. Parameters in a Recordset are transitory. They do not get saved with any QueryDef you may be accessing in your Recordset.

Your line #5 (I assume to be the one that fails) would only set the parameter for use by qd1. The subsequent call to open the QueryDef would not have any reference to the parameter in qd1. Does that make sense?
May 6 '10 #4
NeoPa
32,557 Recognized Expert Moderator MVP
Sorry. It's defined at the beginning of the routine where a value is passed into it:

Public Sub procEvaluateDegReq(lngStdDegReqId As Long)
I believe Jim was referring to the name of the QueryDef's parameter.

Do you have the SQL for that to view?
May 6 '10 #5
bullfrog83
124 New Member
@NeoPa
I'm assuming you mean the SQL to the query? If so, here it is:

Expand|Select|Wrap|Line Numbers
  1. SELECT StdDegReq.StdDegReqId, Cl.StdDegReqDiscId AS DiscId, D.SortOrder AS DiscSortOrder, D.CreditMin AS DiscCreditMin, D.CourseMin AS DiscCourseMin, Cr.StdDegReqClassId AS ClassId, Cl.SortOrder AS ClassSortOrder, Cl.CreditMin AS ClassCreditMin, Cl.CourseMin AS ClassCourseMin, Cr.StdDegReqCourseId AS CourseId, Cr.Course, Cr.SortOrder AS CourseSortOrder, T.Event_Id AS TakenCourse, T.Credit_Grade
  2. FROM (((StdDegReq INNER JOIN StdDegReqDisc AS D ON StdDegReq.StdDegReqId=D.StdDegReqId) INNER JOIN StdDegReqClass AS Cl ON D.StdDegReqDiscId=Cl.StdDegReqDiscId) INNER JOIN StdDegReqCourse AS Cr ON Cl.StdDegReqClassId=Cr.StdDegReqClassId) LEFT JOIN t_TranscriptDetail AS T ON Cr.TakenCourseId=T.TranscriptDetailId
  3. WHERE StdDegReq.StdDegReqId=lngStdDegReqId
  4. ORDER BY D.SortOrder, Cl.SortOrder, Cr.SortOrder;
May 6 '10 #6
bullfrog83
124 New Member
@NeoPa
I think so. So, is there not a way to pass a parameter value into a query via vba?
May 6 '10 #7
NeoPa
32,557 Recognized Expert Moderator MVP
I see no parameters in your query at all. This would certainly explain the error message.
May 6 '10 #8
NeoPa
32,557 Recognized Expert Moderator MVP
I think so. So, is there not a way to pass a parameter value into a query via vba?
If I read you aright (Query here referring to a QueryDef opened in data view on the screen as opposed to into a recordset in your code), then no. i don't believe there is. Something I always thought was a strange omission if I'm honest.
May 6 '10 #9
bullfrog83
124 New Member
@NeoPa
The parameter is lngStdDegReqId in the WHERE clause. When I execute the query it prompts me to enter a value for it.
May 6 '10 #10
NeoPa
32,557 Recognized Expert Moderator MVP
You need to use the PARAMETERS declaration. You can find details in the help system under Microsoft Jet SQL Reference.
Expand|Select|Wrap|Line Numbers
  1. PARAMETERS name datatype [, name datatype [, ...]]
May 7 '10 #11
Jim Doherty
897 Recognized Expert Contributor
@bullfrog83
Sorry....... have been absent and very busy last couple of days.....

OK you are using the DoCmd.OpenQuery statement. The arguments for that action do not include a capability ie an 'argument' in itself to pass parameters (as would appear logical, given parameters do tend to go hand in hand with queries) I agree with Neopa in that, it is somewhat lacking in that area. It is not like the OpenForm method for instance where you can pass an sql string as a WHERE clause or a filter or openargs for a form.

The DoCmd.OpenQuery command is designed to open a saved query but as you know many queries have parameters attached to them. The general rule of thumb if you want to open a query using the parameters AS IS is to open a recordset in code by reference to the QUERYDEFS collection. You then manipulate any dataset using recordset code. An example of this would be as follows:

Expand|Select|Wrap|Line Numbers
  1. Dim MyDb as DAO.Database
  2. Dim Myqdf as DAO.QueryDef
  3. Dim rst as DAO.Recordset
  4. Dim MyQueryName As String
  5. Dim strSurname as String
  6.  
  7. MyQueryName = "qryPeople"
  8. strSurname="Lennon"
  9.  
  10. set MyDb =CurrentDb()
  11. Set Myqdf = MyDb.QueryDefs(MyQueryName)
  12. qdf.parameters("Surname")=strSurname
  13. 'Open a recordset on this query
  14. set rst=Myqdf.OpenRecordset()
  15. Do While Not rst.EOF
  16.  
  17. 'your code sequence goes here
  18. Loop
  19.  
  20. rst.Close
  21. Myqdf.Close
  22. MyDB.Close
  23. rst=Nothing
  24. Myqdf=Nothing
  25. Mydb=Nothing
The other method is to build your SQL on the fly in your form code assigning elements for any WHERE clause and then use this to set the recordsource of a form.

If you merely want to use and open the the query itself you might want to consider using a function to pass back values to your parameter query. Your form code could set the criteria and pass this to a global variable. A Function could pick up on that global variable and pass the value to the query. The below function can be pasted into a new module


Expand|Select|Wrap|Line Numbers
  1. Global MY_INTEGER As Integer
  2. Global MY_LONG As Long
  3. Global MY_STRING As String
  4. Global MY_BOOLEAN As Boolean
  5.  
  6. Function GetMyInteger() As Integer
  7. GetMyInteger = MY_INTEGER
  8. End Function
  9. Function GetMyString() As Integer
  10. GetMyString = MY_STRING
  11. End Function
  12. Function GetMyBoolean() As Integer
  13. GetMyBoolean = MY_BOOLEAN
  14. End Function
To implement the above functions you would merely replace your 'square bracketed' parameters in your query in the query grid and instead use a function call ie:

GetMyLongInteger()

(Remember you are explicitly setting the value of your global variable from your form code) so in your particular specific example case, your current DoCmd.Openquery command program flow could still be used


Expand|Select|Wrap|Line Numbers
  1. MY_LONG=7
  2. DoCmd.OpenQuery "q_StdDegReqAll", , "acReadOnly"

So your current query amended SQL would look like this...

Expand|Select|Wrap|Line Numbers
  1. SELECT StdDegReq.StdDegReqId, Cl.StdDegReqDiscId AS DiscId, D.SortOrder AS DiscSortOrder, D.CreditMin AS DiscCreditMin, D.CourseMin AS DiscCourseMin, Cr.StdDegReqClassId AS ClassId, Cl.SortOrder AS ClassSortOrder, Cl.CreditMin AS ClassCreditMin, Cl.CourseMin AS ClassCourseMin, Cr.StdDegReqCourseId AS CourseId, Cr.Course, Cr.SortOrder AS CourseSortOrder, T.Event_Id AS TakenCourse, T.Credit_Grade 
  2. FROM (((StdDegReq INNER JOIN StdDegReqDisc AS D ON StdDegReq.StdDegReqId=D.StdDegReqId) INNER JOIN StdDegReqClass AS Cl ON D.StdDegReqDiscId=Cl.StdDegReqDiscId) INNER JOIN StdDegReqCourse AS Cr ON Cl.StdDegReqClassId=Cr.StdDegReqClassId) LEFT JOIN t_TranscriptDetail AS T ON Cr.TakenCourseId=T.TranscriptDetailId 
  3. WHERE StdDegReq.StdDegReqId=GetMyLongInteger()
  4. ORDER BY D.SortOrder, Cl.SortOrder, Cr.SortOrder; 
Hope this helps you a little and yes I know this is workaround material :)
May 7 '10 #12
DataAnalyzer
15 New Member
When assigning parameters, you need to refer to the parameter name

I think your your line:
Expand|Select|Wrap|Line Numbers
  1. qd1.Parameters(lngStdDegReqId) = lngStdDegReqId
should be
Expand|Select|Wrap|Line Numbers
  1. qd1.Parameters("StdDegReqId") = lngStdDegReqId
Here's a good resource for queries including VBA code for parameters:
http://www.fmsinc.com/tpapers/querie...ningParameters
May 9 '10 #13
bullfrog83
124 New Member
@Jim Doherty
Thanks for the workarounds! I decided to just type my sql in code. Hopefully, Microsoft will improve on the .OpenQuery command in a future release and allow an argument to pass parameters. Definitely a serious omission.
May 10 '10 #14

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

Similar topics

0
3706
by: Reggie | last post by:
Hi and TIA, I Have a query that uses a function for setting the Criteria. When I view the return value of the finction it appears to be exactly what I want for my criteria. The problem is if I...
7
19669
by: Annelies | last post by:
I need to pass a parameter to the source query of a subform via vba. Any ideas on how to do that? (Actually I wanted to set the subform source to a query but I guess that isn't possible.. ) ...
3
3100
by: thomas goodwin | last post by:
I have a query which asks for a parameter value to execute it. To see the results I have to: a) click on the query -- the "Enter Parameter Value" window pops up. b) enter the parameter value c)...
2
30293
by: rg | last post by:
Hi all, I have query about passing a parameter from form to a query. My situation is as follows: I have a query that pulls up some data from a table, however there is a condition that must...
1
7730
by: | last post by:
Hi, I've defined an ObjectDataSource and a parameterized DataSet. I would like it if I could pass the parameter value that describes the query that creates the DataSet as part of a user control...
3
2627
by: programmer2004 | last post by:
Hi, I am trying to pass parameter value to ODBCCommand object. but it is not assigning values , the null value is gets stored. i m using following code, Private Sub cmdsave_Click(ByVal sender As...
2
6361
by: Roger | last post by:
Anyone know how to pass a parameter to a query via a report in Access 2003?
2
2019
by: de_ja | last post by:
Hi, If, for example an access database that concerns insurance claim investigations. The database comprises only one table containing data - investigation reference number, referral date,...
2
14922
by: ncsthbell | last post by:
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...
0
7184
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
7253
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6966
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7438
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
5555
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,...
0
4663
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...
0
3145
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1486
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
365
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...

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.