473,394 Members | 1,828 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,394 software developers and data experts.

query question

I have a query that uses a value from a form as a criteria. For
example looking for all customers belonging to a particular party.
the criteria on the QBE looks like [Forms]![frmParty]![PartyID]

I am trying to open the query in VBA using DAO. If I simply use
OpenRecordset (with the form open that contains the criteria value) it
fails. If I hard code a value in the query instead e.g. customerID 33
its fine. Alternatively if I change the criterion to a parameter then
use a querydef passing a value in for the parameter its fine.

This is the SQL of the query I'm trying to open.

SELECT DISTINCT qryHirePrice.HirePriceID, qryHirePrice.HirePrice,
Sum(qryPayments.PaymentAmount) AS SumOfPaymentAmount, [HirePrice]-
[SumOfPaymentAmount] AS Balance, qryHirePrice.PartyID
FROM qryHirePrice INNER JOIN qryPayments ON qryHirePrice.HirePriceID =
qryPayments.HirePriceID
GROUP BY qryHirePrice.HirePriceID, qryHirePrice.HirePrice,
qryHirePrice.PartyID
HAVING (((qryHirePrice.PartyID)=[Forms]![frmParty]![PartyID]));

this is the VBA code

Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
set rst = db.OpenRecordset("qryHirePricePayment")

thanks
Allan

Mar 19 '07 #1
4 2923
yes, that's correct; you have to hard-code the value or pass the value to a
querydef. a third alternative is to use a string variable to hold the SQL
statement and pass to the recordset, concatenating the value from the form
control into the string, as

Dim strSQL As String
strSQL = "SELECT DISTINCT qryHirePrice.HirePriceID..." _
& "...HAVING qryHirePrice.PartID = " _
& [Forms]![frmParty]![PartyID]

Set rst = db.OpenRecordset(strSQL)

hth
"allanx38" <al******@yahoo.co.ukwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
I have a query that uses a value from a form as a criteria. For
example looking for all customers belonging to a particular party.
the criteria on the QBE looks like [Forms]![frmParty]![PartyID]

I am trying to open the query in VBA using DAO. If I simply use
OpenRecordset (with the form open that contains the criteria value) it
fails. If I hard code a value in the query instead e.g. customerID 33
its fine. Alternatively if I change the criterion to a parameter then
use a querydef passing a value in for the parameter its fine.

This is the SQL of the query I'm trying to open.

SELECT DISTINCT qryHirePrice.HirePriceID, qryHirePrice.HirePrice,
Sum(qryPayments.PaymentAmount) AS SumOfPaymentAmount, [HirePrice]-
[SumOfPaymentAmount] AS Balance, qryHirePrice.PartyID
FROM qryHirePrice INNER JOIN qryPayments ON qryHirePrice.HirePriceID =
qryPayments.HirePriceID
GROUP BY qryHirePrice.HirePriceID, qryHirePrice.HirePrice,
qryHirePrice.PartyID
HAVING (((qryHirePrice.PartyID)=[Forms]![frmParty]![PartyID]));

this is the VBA code

Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
set rst = db.OpenRecordset("qryHirePricePayment")

thanks
Allan

Mar 19 '07 #2

How does it fail? What error are you getting if any?

Mar 19 '07 #3
On Mar 19, 1:13 am, "storrboy" <storr...@sympatico.cawrote:
How does it fail? What error are you getting if any?
Thanks guys. I had fallen back on the SQL string idea but it strikes
me as a bit long winded, call me lazy ...

The original error I get is 3061, Too few parameters. Expected 1.
Maybe I can pass the parameter in using a position as opposed to via a
name as I usually do.

Allan

Mar 19 '07 #4
Does the query run as just as query???
bobh.
On Mar 18, 8:48 pm, "allanx38" <allan...@yahoo.co.ukwrote:
I have a query that uses a value from a form as a criteria. For
example looking for all customers belonging to a particular party.
the criteria on the QBE looks like [Forms]![frmParty]![PartyID]

I am trying to open the query in VBA using DAO. If I simply use
OpenRecordset (with the form open that contains the criteria value) it
fails. If I hard code a value in the query instead e.g. customerID 33
its fine. Alternatively if I change the criterion to a parameter then
use a querydef passing a value in for the parameter its fine.

This is the SQL of the query I'm trying to open.

SELECT DISTINCT qryHirePrice.HirePriceID, qryHirePrice.HirePrice,
Sum(qryPayments.PaymentAmount) AS SumOfPaymentAmount, [HirePrice]-
[SumOfPaymentAmount] AS Balance, qryHirePrice.PartyID
FROM qryHirePrice INNER JOIN qryPayments ON qryHirePrice.HirePriceID =
qryPayments.HirePriceID
GROUP BY qryHirePrice.HirePriceID, qryHirePrice.HirePrice,
qryHirePrice.PartyID
HAVING (((qryHirePrice.PartyID)=[Forms]![frmParty]![PartyID]));

this is the VBA code

Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
set rst = db.OpenRecordset("qryHirePricePayment")

thanks
Allan

Mar 19 '07 #5

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

Similar topics

9
by: majsen | last post by:
Hi, I have problem running this query. It will time out for me... My database are small just about 200 members. I have a site for swaping appartments (rental). my query should look for match in...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
2
by: mmitchell_houston | last post by:
I'm working on a .NET project and I need a single query to return a result set from three related tables in Access 2003, and I'm having trouble getting the results I want. The details: ...
22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
3
by: Richard Hollenbeck | last post by:
I am very sorry about the (almost) re-post, but you will see that my first question wasn't very clear; I have another question I posted this morning called, "in DAO: Run time error 3061 Too few...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...
0
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
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...

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.