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

Running Totals On QueryDef In Code

I have the following code:

Dim strSQL As String
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Dim intNumOfPaid, intNumOfHypoed, intNumOfNotTaken, intNumOfDeclined,
intNumOfWasted, intNumOfApproved As String
Dim QDF As QueryDef
Dim PARAM As Parameter
Set DB = CurrentDb()
Set QDF = DB.QueryDefs("qryHealthReceivedAutomated")
For Each PARAM In QDF.Parameters 'Loop through & eval
Parameters
PARAM.Value = Eval(PARAM.name)
Next PARAM
Set RS = QDF.OpenRecordset(dbOpenDynaset)
RS.MoveFirst

RS.Close
Set RS = Nothing
DB.Close
Set DB = Nothing

-------------------------------------------------------------------------------------------
QryHealthReceivedRevised is not a totals query.

Is there a way to return the totals of QryHealthReceivedRevised
without making it a Totals query? (I would just make a copy of
QryHealthReceivedRevised and make it a totals query, but they want as
few objects as possible)

I was thinking something like : Set QDF = DB.QueryDefs("SELECT
COUNT(PolicyNumber), Sum(Premium) FROM qryHealthReceivedAutomated") but
that doesnt work because I need to evaluate the parameters first.

Should I just step through the recordset and total it that way? Or is
there a better way to do this?

Mar 23 '06 #1
2 2684
You could have the parameters refer to controls on a form (hidden controls,
if desired) and fill in the value of those controls in code. This would set
the parameters but allow you to open another copy of the query and still
have the parameters' values available. You could then use a DSum() function
with the query as a source.

You could also use a totals query as you mentioned, but do it all in code.
Don't create the Query object. You can do this by leaving the name of the
query as an empty string when you create a QueryDef object variable. Once
you've evaluated the parameters, you could use those values to make your SQL
statement.

Another possibility is to change the SQL of the current query in code then
change it back again (qdf.SQL = "SELECT ..."). If you do this, you could
evaluate the parameters first, but assign those values to variables then
hard code them into the SQL by concatenating the values in instead of
leaving them as parameters.

And, as you pointed out, you could total it yourself by stepping through the
recordset. However, if the recordset gets very large you will find this to
be very slow compared to letting Access handle this with its built-in
abilities.

--
Wayne Morgan
MS Access MVP
"BerkshireGuy" <bd*****@yahoo.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I have the following code:

Dim strSQL As String
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Dim intNumOfPaid, intNumOfHypoed, intNumOfNotTaken, intNumOfDeclined,
intNumOfWasted, intNumOfApproved As String
Dim QDF As QueryDef
Dim PARAM As Parameter
Set DB = CurrentDb()
Set QDF = DB.QueryDefs("qryHealthReceivedAutomated")
For Each PARAM In QDF.Parameters 'Loop through & eval
Parameters
PARAM.Value = Eval(PARAM.name)
Next PARAM
Set RS = QDF.OpenRecordset(dbOpenDynaset)
RS.MoveFirst

RS.Close
Set RS = Nothing
DB.Close
Set DB = Nothing

-------------------------------------------------------------------------------------------
QryHealthReceivedRevised is not a totals query.

Is there a way to return the totals of QryHealthReceivedRevised
without making it a Totals query? (I would just make a copy of
QryHealthReceivedRevised and make it a totals query, but they want as
few objects as possible)

I was thinking something like : Set QDF = DB.QueryDefs("SELECT
COUNT(PolicyNumber), Sum(Premium) FROM qryHealthReceivedAutomated") but
that doesnt work because I need to evaluate the parameters first.

Should I just step through the recordset and total it that way? Or is
there a better way to do this?

Mar 24 '06 #2
Thanks Wayne.

I was wondering if the recorset method was a bad route to take. I will
go with one of your other suggestions and post what I did in case
others could use the information.

Thanks again,
Brian

Mar 27 '06 #3

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

Similar topics

4
by: Bill Dika | last post by:
Hi I am trying to calculate a running total of a calculated textbox (tbAtStandard) in GroupFooter1 for placement in a textbox (tbTotalAtStandard) on my report in Groupfooter0. The problem...
6
by: KashMarsh | last post by:
Trying to show running totals on a report, except it needs to show one total amount and values being subtracted from it. For example, the report shows a Total Inventory amount (TotInvAmt). And...
0
by: Beacher | last post by:
Hi all, I'm having troubles with a report I've created. You make some selections on a form which then sets a reports filter property and opens the report, this works fine. One bigger problem...
2
by: Jana | last post by:
Using Access 97. Background: I have a main report called rptTrustHeader with a subreport rptTrustDetails in the Details section of the main report. The main report is grouped by MasterClientID. ...
2
by: BerkshireGuy | last post by:
I have a form that acts like a dashboard to show summarized data. Currently, this form gets its summarized values from a total's query. When the user selects to run the dashboard, they should be...
3
by: =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?= | last post by:
Hi, Fairly easy to create one running total for a gridview but what if you have dozens of them? I have a gridview that allows bulk editing (all rows at once) and have it set up so that, on data...
3
by: mochatrpl | last post by:
I am looking for a way to make a query / report display the running average for total dollars. I have already set up a query to provide totals dollars per day from which a report graphly shows...
9
by: JJM0926 | last post by:
I'm trying to create a running totals query in access 97. I have followed the directions on how to do it from Microsofts website article id 138911. I took their code they had and replaced it with...
6
by: Stuart Shay | last post by:
Hello All: I have a array which contains the totals for each month and from this array I want to get a running total for each month decimal month = new decimal; month = 254; (Jan) month =...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.