473,626 Members | 3,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set parameters for nested query

Well, I've been searching around the groups for an answer to this one,
and none of the suggestions seems to be working for me. I'm almost
positive that this is a simple fix, but for the life of me I can't
figure out what I'm doing wrong.

I have a form that uses a query as its recordsource; I'll call the
query qryParent.

qryParent is based on 2 nested queries; which is to say qryParent
selects from qryChild2 and qryChild2 selects from qryChild1.

qryChild1 has two parameters: Company_Locatio n and Menu_Price_Date

I am trying to set the parameter for qryChild1 using DAO, then opening
the form which uses qryParent as its recordsource.

The problem is that my code doesn't do anything, Access still prompts
me for the parameters.

Now, before it is suggested, I want to preempt the following:
1. I want to keep the queries generic so that I can use them in future
forms; therefore I am not willing to set the parameters to field
values on the various forms (which could be done)
2. I could write the whole query in SQL (I think) and set the
parameters in the SQL string; however, I would prefer to use the
'parameter setting process' b/c it limits the lines of code in an
already complex procedure

Ok, so here is the code I'm using:
Private Sub Form_Load()
On Error GoTo Error_Handler

Dim db As DAO.Database
Dim qdf As DAO.QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("q ryChild1")
qdf.Parameters( "Menu_Price_Dat e") = Date
qdf.Parameters( "Company_Locati on") = Me.txtCompanyLo cation

Me.Recordsource = _
qryParent

' ....Code Continues....

Any thoughts or suggestions would be extremely well received.

Thanks,

Kelii
Jul 19 '08 #1
7 5504
One other thing to mention, and I think this may be important,
qryParent is not an action query. This is fairly self evident from the
description above, but I wanted to be explicit about that.

Kelii
Jul 19 '08 #2
Hmmm, after doing some research, it may be useful for everyone to see
the actual SQL of qryChild1 ... since my query may be improperly
written (i.e., for passing parameters).

Please note that the query works as I expect ... which is to say if I
open qryParent, I get two prompts for Company_Locatio n and
Menu_Price_Date .

However, as I've continued looking for a solution, perhaps this is not
'enough.'

qryChild1 in SQL:
PARAMETERS [Company_Locatio n] Text ( 255 ), [Menu_Price_Date]
DateTime;
SELECT tblMenuPrice.Pr ice_ID, tblMenuPrice.Co mpany_Location,
tblMenuPrice.Me nu_Price_Date, tblMenuPrice.Me nu_Description_ ID,
tblMenuPrice.Me nu_Item_Price
FROM tblMenuPrice
WHERE (((tblMenuPrice .Company_Locati on)=[Company_Locatio n]) AND
((tblMenuPrice. Menu_Price_Date )<=[Menu_Price_Date]));
Jul 19 '08 #3
On Fri, 18 Jul 2008 20:49:00 -0700 (PDT), Kelii <ke****@yahoo.c om>
wrote:

Try this:
Set qdf = db.QueryDefs("q ryParent")

-Tom.
Microsoft Access MVP

>Well, I've been searching around the groups for an answer to this one,
and none of the suggestions seems to be working for me. I'm almost
positive that this is a simple fix, but for the life of me I can't
figure out what I'm doing wrong.

I have a form that uses a query as its recordsource; I'll call the
query qryParent.

qryParent is based on 2 nested queries; which is to say qryParent
selects from qryChild2 and qryChild2 selects from qryChild1.

qryChild1 has two parameters: Company_Locatio n and Menu_Price_Date

I am trying to set the parameter for qryChild1 using DAO, then opening
the form which uses qryParent as its recordsource.

The problem is that my code doesn't do anything, Access still prompts
me for the parameters.

Now, before it is suggested, I want to preempt the following:
1. I want to keep the queries generic so that I can use them in future
forms; therefore I am not willing to set the parameters to field
values on the various forms (which could be done)
2. I could write the whole query in SQL (I think) and set the
parameters in the SQL string; however, I would prefer to use the
'parameter setting process' b/c it limits the lines of code in an
already complex procedure

Ok, so here is the code I'm using:
Private Sub Form_Load()
On Error GoTo Error_Handler

Dim db As DAO.Database
Dim qdf As DAO.QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("q ryChild1")
qdf.Parameters( "Menu_Price_Dat e") = Date
qdf.Parameters( "Company_Locati on") = Me.txtCompanyLo cation

Me.Recordsource = _
qryParent

' ....Code Continues....

Any thoughts or suggestions would be extremely well received.

Thanks,

Kelii
Jul 19 '08 #4
Tom,

Thanks for the reply. So I tried to get your suggestion to work last
night and this morning with no success.

A few things I did discover as I was working your suggestion:
1. I've located the query parameter setting code in the Form_Load
procedure of the form that used the query (i.e., qryParent) as its
recordsource

2. Relocating the code to Form_Open does not resolve the issue

3. Relocating the code to the event just prior to the Form_Open event
(e.g., cmdOpenForm) does not resolve the issue

4. The parameter names are properly set, or at least I'm pretty sure
they are, and the code will recognize that the parameters exist; for
example:

For each prm in qdf.Parameters
Debug.Print prm.Name
Next prm

Above code works and generates the following:
[Company_Name]
[Menu_Price_Date]

I would be grateful for any additional suggestions.

Thanks,

Kelii
Jul 20 '08 #5
Kelii <ke****@yahoo.c omwrote in news:5c8560b2-88ad-475a-b330-
83**********@2g 2000hsn.googleg roups.com:
Tom,

Thanks for the reply. So I tried to get your suggestion to work last
night and this morning with no success.

A few things I did discover as I was working your suggestion:
1. I've located the query parameter setting code in the Form_Load
procedure of the form that used the query (i.e., qryParent) as its
recordsource

2. Relocating the code to Form_Open does not resolve the issue

3. Relocating the code to the event just prior to the Form_Open event
(e.g., cmdOpenForm) does not resolve the issue

4. The parameter names are properly set, or at least I'm pretty sure
they are, and the code will recognize that the parameters exist; for
example:

For each prm in qdf.Parameters
Debug.Print prm.Name
Next prm

Above code works and generates the following:
[Company_Name]
[Menu_Price_Date]

I would be grateful for any additional suggestions.

Thanks,

Kelii
I don't know of any way to do what you want to do which is (I think ...
maybe):

Establish the parameter values of a query (Query1) and then call that
query by name from another query (Query2) which will "know" what those
parameter values are.

One can establish the parameter values of a query object and open a
recordset with that object's OpenRecordset method. But those parameter
values are not persistent. TTBOMK Query2 cannot easily use that query
object, which is not the same thing as the saved query.

My practice might be (if I used Saved Queries in JET or ACE, which I
seldom do) to load the query's SQL to a string, replace the parameter
names with the values I want and to use that string as a subquery.

eg

Public Sub Whim()
Dim SQL$
With DBEngine(0)(0). QueryDefs
..Refresh
SQL = .Item("Query1") .SQL
SQL = Replace(SQL, "[GetMinEmployeeI D]", 3)
SQL = Replace(SQL, ";", "")
Debug.Print DBEngine(0)(0). OpenRecordset(R eplace(.Item("Q uery2").SQL,
"Query1", "(" & SQL & ")"))(0)
End With
End Sub

Query1:SELECT *
FROM Employees
WHERE ID>[GetMinEmployeeI D];

Query2:SELECT *
FROM Query1;

The "Whim" sub prints 4 in the Immediate Window in Northwinds.

Newsclients/servers are likely to add line breaks to the code, which must
be removed.
Jul 20 '08 #6
Lyle,

Fair enough ... there are easy work arounds by saving the query or
writing SQL into the code. However, I am somewhat dissapointed as this
means more query tedium.

Question for you on follow up, why do you rarely use saved queries?

And, is your normal practice to simply write the SQL out in your code
and, for example, set the RecordSource property on Load or Open?

Thanks,

Kelii
Jul 21 '08 #7
In case future viewers are interested, I wanted to post my work
around.

I think this solution is almost as good, if not possibly better.

I created a module called kleQueryOutput.

Then I put a function in the module that compiles the SQL string based
on the two parameters (passed).

Then I set the recordsource equal to the function and passed the
appropriate parameters.

In the future, I can use the same function to produce an equivlaent
SQL string with different parameters (which makes me soooooo happy).

The SQL is tedious, so I avoid it at all possible costs. However, now
that its done, I'm off and rolling.

Here is the actual code should anyone be interested:
Private Sub Form_Load()
On Error GoTo Error_Handler

Me.RecordSource = _
fMenuItemWithLa stPrice(Me.txtC ompanyLocation, Date)

....code continues...
End Sub

Function fMenuItemWithLa stPrice _
(strCompanyName As String, datLastDate As Date)
On Error GoTo Error_Handler
'This function returns the SQL string for the query showing
'all the various details of a menu item as well as the last
'menu price given a specified Company and Date
'Inputs: company location, last maximum date (i.e., <= Date)
'Example: fMenuItemWithLa stPrice (Me.Company_Loc ation, _
'Me.Sale_Date)
'Note: this SQL string build on fLastMenuPrice

Dim strSQL As String

'This compiles the query string
strSQL = _
"SELECT " & _
"tblItemDetails .Item_Descripti on_Number,
tblItemDetails. Class, " & _
"tblItemDetails .Item_Descripti on_ID,
tblItemDetails. Item_Unit_of_Me asure, " & _
"tblItemDetails .Item_Location, tblItemDetails. Item_Type, "
& _
"tblItemDetails .Item_Category,
tblItemDetails. Active_Status, " & _
"tblItemDetails .Item_Par, tblItemDetails. Item_EOQ, " & _
"tblItemDetails .Sub_Assembly_T otal_Yield,
tblItemDetails. Memo, " & _
"tblMenuPrice.C ompany_Location , rsPrice3.Menu_P rice_Date,
" & _
"tblMenuPrice.M enu_Item_Price " & _
"FROM " & _
"(tblItemDetail s INNER JOIN tblMenuPrice ON
tblItemDetails. Item_Descriptio n_ID = " & _
"tblMenuPrice.M enu_Description _ID) INNER JOIN " & _
"(" & fLastMenuPrice( strCompanyName, datLastDate) & ")
rsPrice3 " & _
"ON tblMenuPrice.Pr ice_ID = rsPrice3.Price_ ID " & _
"WHERE " & _
"(((tblMenuPric e.Company_Locat ion)='" & _
FindFirstFixup( strCompanyName) & "'))"

fMenuItemWithLa stPrice = strSQL

Exit_Procedure:
On Error Resume Next
Exit Function
Error_Handler:
Select Case Err
Case Else
MsgBox "Error: " & Err.Number & vbCr & Err.Description
Resume Exit_Procedure
End Select
End Function

Function fLastMenuPrice _
(strCompanyName As String, datLastDate As Date)
On Error GoTo Error_Handler
'This function returns the SQL string for the query showing
'each menu item along with the last
'menu price given a specified Company and Date
'Inputs: company location, max date of price (i.e., <= date)
'Example: fMenuItemWithLa stPrice (Me.Company_Loc ation, _
'Me.Sale_Date)

Dim strSQL As String

'This compiles the query string
strSQL = _
"SELECT " & _
"tblMenuPrice.P rice_ID, tblMenuPrice.Co mpany_Location, " &
_
"tblMenuPrice.M enu_Price_Date,
tblMenuPrice.Me nu_Description_ ID, " & _
"tblMenuPrice.M enu_Item_Price " & _
"FROM " & _
"(SELECT " & _
"Max(rsPrice1.M enu_Price_Date) AS
MaxOfMenu_Price _Date, " & _
"rsPrice1.Menu_ Description_ID " & _
"FROM " & _
"(SELECT " & _
"tblMenuPrice.P rice_ID,
tblMenuPrice.Co mpany_Location, " & _
"tblMenuPrice.M enu_Price_Date,
tblMenuPrice.Me nu_Description_ ID, " & _
"tblMenuPrice.M enu_Item_Price " & _
"FROM " & _
"tblMenuPri ce " & _
"WHERE " & _
"(((tblMenuPric e.Company_Locat ion)='" & _
FindFirstFixup( strCompanyName) & "') AND " & _
"((tblMenuPrice .Menu_Price_Dat e)<=# "
strSQL = strSQL & _
datLastDate & "#))) rsPrice1 " & _
"GROUP BY rsPrice1.Menu_D escription_ID) rsPrice2 " & _
"INNER JOIN " & _
"tblMenuPri ce ON (rsPrice2.MaxOf Menu_Price_Date = " & _
"tblMenuPrice.M enu_Price_Date) AND " & _
"(rsPrice2.Menu _Description_ID =
tblMenuPrice.Me nu_Description_ ID)"

fLastMenuPrice = strSQL

Exit_Procedure:
On Error Resume Next
Exit Function
Error_Handler:
Select Case Err
Case Else
MsgBox "Error: " & Err.Number & vbCr & Err.Description
Resume Exit_Procedure
End Select
End Function

Thanks to Lyle and Tom for their help.

Kelii
Jul 21 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2196
by: Trey Guy | last post by:
I have an ASP page I use as a front end to an Access db. I am running a query from that page that is returning a "Too few parameters..." error. The query works fine in Access. I have checked all field names and they are correct. The SQL is below. Any ideas as to the cause of this error? Thanks, Trey SELECT qryParticipantOutcomesAggregate.IndID, Indicators.IndName, Sum(qryParticipantOutcomesAggregate.NEligible) AS SumOfNEligible,
3
16939
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can create a parameter query in a stored procedure, but how do I use the result set of a parameter query in a select query (in the same or another sp)? In short, if a select query contains a result table that is generated as a parameter query, how do I...
14
3419
by: | last post by:
Hi, I was performing SQL UPDATE queries and I notice that they SUCCEED on the ExecuteNonQuery() call with NO exceptions raised BUT they fail at the Database. They say they succeed in the code but they fail at the database. To fix this they Parameters.Add must be called in the ORDER they are in the SQL STATEMENT. This is confusing and bad.
3
23173
by: Ryan Hubbard | last post by:
I would like to run a query from VBA. The query will be one with parameters. Is there a way to run the query and have Access prompt for the values like it does if I where to execute it through the database window?
6
8532
by: Jonathan LaRosa | last post by:
I am trying to open a recordset and I am getting an error and I can't figure out why. See code below. sqlString2 does not work. sqlString does. Clearly the problem is with the nested SELECT statement. But when I copy sqlString2 into a new query it runs just fine. Futhermore, I have used a nested SELECT statement with the OpenRecordset() method before and it works just fine. Any ideas? thanks much,
7
21612
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the server ? Is it possible to use parameters in pass-through queries ? An additional question: Is it possible to connect to a database on MySQL or PostgreSQL using ADO ? Is it possible to execute pass-through queries with parameters, using ADO...
21
4172
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if possible. The thing I am trying to do is to convert a double value to a string with 8 elements. 8 is fixed because of the files I work with. I will change this 8 character string with the one(8 character string) already in the file and so on. But I...
10
3920
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication eventname string source string ID string
0
147
by: Steve Holden | last post by:
Vlastimil Brom wrote: Ultimately that depends where the table and column names come from. If they are user inputs then you are still vulnerable to SQL injection, but usually that's not the case when a query is being parameterized - usually it's values. As long as you consider the source of your data carefully you'll probably be OK. regards
0
8202
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
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
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
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6125
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
5575
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
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
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 we have to send another system
2
1512
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.