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

a dynamic query needing a sum function

Hi!
I have a dynamic query that has a form attached. I want to add up a
field with the sum funtion, the field is netkg.

Dim rec As Recordset
Dim qdf As QueryDef
Dim strsql

Set db = CurrentDb
db.QueryDefs.Refresh
For Each qdf In db.QueryDefs
If qdf.Name = "qryWeightsTotals" Then
db.QueryDefs.Delete qdf.Name
End If
Next qdf
strsql = "SELECT [Date],[Time],[makemodel],pao, Street,
[town],[Postcode],[gross weight],tare,[NetKG] FROM tblVanDetails WHERE
[RegistrationNo] = '" & zz & "'"
Set qdf = db.CreateQueryDef("qryWeightsTotals", strsql)
db.QueryDefs.Refresh
Me.Refresh
DoCmd.OpenForm "frmweightstotals", acNormal
Set db = Nothing

I have tried replacing [NetKG] with SUM([NetKG]) and SUM([NetKG]) as
TotalNet, this is with TotalNet dimmed as a variant. Have also
attempted, somewhat in ignorance, using the GROUP BY clause after the
where, however this results in a syntax error i.e. does not compile.
The first 2 result in an erroneous error message referring to
aggregating a date (netkg is not a date field), this may mean
something to others more enlghtened than I?!

any pointers would be much appreciated.
Regards
Phil

Aug 29 '07 #1
4 3249
On Aug 29, 1:11 pm, Philip_coll...@lineone.net wrote:
Hi!
I have a dynamic query that has a form attached. I want to add up a
field with the sum funtion, the field is netkg.

Dim rec As Recordset
Dim qdf As QueryDef
Dim strsql

Set db = CurrentDb
db.QueryDefs.Refresh
For Each qdf In db.QueryDefs
If qdf.Name = "qryWeightsTotals" Then
db.QueryDefs.Delete qdf.Name
End If
Next qdf
strsql = "SELECT [Date],[Time],[makemodel],pao, Street,
[town],[Postcode],[gross weight],tare,[NetKG] FROM tblVanDetails WHERE
[RegistrationNo] = '" & zz & "'"
Set qdf = db.CreateQueryDef("qryWeightsTotals", strsql)
db.QueryDefs.Refresh
Me.Refresh
DoCmd.OpenForm "frmweightstotals", acNormal
Set db = Nothing

I have tried replacing [NetKG] with SUM([NetKG]) and SUM([NetKG]) as
TotalNet, this is with TotalNet dimmed as a variant. Have also
attempted, somewhat in ignorance, using the GROUP BY clause after the
where, however this results in a syntax error i.e. does not compile.
The first 2 result in an erroneous error message referring to
aggregating a date (netkg is not a date field), this may mean
something to others more enlghtened than I?!

any pointers would be much appreciated.
Regards
Phil
First of all, it is bad form to use Date or Time as a field name,
variable name or control name, as these are common functions and
keywords.
Second, DSUM( ) will give you a total based on a SQL query.
Third, SQL can return records or record totals, not both mixed. That
being said, it is possible to have an aggregate sum, where only the
last record would show the total. This is also problematic as Access
doesn't guarantee that any particular record will be the last one.
This will give you a one record total:
strsql = "SELECT SUM([NetKG]) AS NetKG_Total FROM tblVanDetails WHERE
[RegistrationNo] = '" & zz & "'"

Aug 29 '07 #2
On 29 Aug, 19:35, OldPro <rrossk...@sbcglobal.netwrote:
On Aug 29, 1:11 pm, Philip_coll...@lineone.net wrote:


Hi!
I have a dynamic query that has a form attached. I want to add up a
field with the sum funtion, the field is netkg.
Dim rec As Recordset
Dim qdf As QueryDef
Dim strsql
Set db = CurrentDb
db.QueryDefs.Refresh
For Each qdf In db.QueryDefs
If qdf.Name = "qryWeightsTotals" Then
db.QueryDefs.Delete qdf.Name
End If
Next qdf
strsql = "SELECT [Date],[Time],[makemodel],pao, Street,
[town],[Postcode],[gross weight],tare,[NetKG] FROM tblVanDetails WHERE
[RegistrationNo] = '" & zz & "'"
Set qdf = db.CreateQueryDef("qryWeightsTotals", strsql)
db.QueryDefs.Refresh
Me.Refresh
DoCmd.OpenForm "frmweightstotals", acNormal
Set db = Nothing
I have tried replacing [NetKG] with SUM([NetKG]) and SUM([NetKG]) as
TotalNet, this is with TotalNet dimmed as a variant. Have also
attempted, somewhat in ignorance, using the GROUP BY clause after the
where, however this results in a syntax error i.e. does not compile.
The first 2 result in an erroneous error message referring to
aggregating a date (netkg is not a date field), this may mean
something to others more enlghtened than I?!
any pointers would be much appreciated.
Regards
Phil

First of all, it is bad form to use Date or Time as a field name,
variable name or control name, as these are common functions and
keywords.
Second, DSUM( ) will give you a total based on a SQL query.
Third, SQL can return records or record totals, not both mixed. That
being said, it is possible to have an aggregate sum, where only the
last record would show the total. This is also problematic as Access
doesn't guarantee that any particular record will be the last one.
This will give you a one record total:
strsql = "SELECT SUM([NetKG]) AS NetKG_Total FROM tblVanDetails WHERE
[RegistrationNo] = '" & zz & "'"- Hide quoted text -

- Show quoted text -
Thanks for the response. how do I place that sql within the above code
to get the form to see the total? I'm quite happy to change the field
names!
regards
Phil

Aug 29 '07 #3
On Aug 29, 3:13 pm, Philip_coll...@lineone.net wrote:
On 29 Aug, 19:35, OldPro <rrossk...@sbcglobal.netwrote:


On Aug 29, 1:11 pm, Philip_coll...@lineone.net wrote:

First of all, it is bad form to use Date or Time as a field name,
variable name or control name, as these are common functions and
keywords.
Second, DSUM( ) will give you a total based on a SQL query.
Third, SQL can return records or record totals, not both mixed. That
being said, it is possible to have an aggregate sum, where only the
last record would show the total. This is also problematic as Access
doesn't guarantee that any particular record will be the last one.
This will give you a one record total:
strsql = "SELECT SUM([NetKG]) AS NetKG_Total FROM tblVanDetails WHERE
[RegistrationNo] = '" & zz & "'"- Hide quoted text -
- Show quoted text -

Thanks for the response. how do I place that sql within the above code
to get the form to see the total? I'm quite happy to change the field
names!
regards
Phil- Hide quoted text -

- Show quoted text -
It depends on when you want the total to show. If you want it to show
after zz is updated, then put the code in that controls' afterupdate
event. You will want the total control to be a label.
You can use either...

lblTotal.Caption=DSUM("NetKG","tblVanDetails", "[RegistrationNo] = "
& zz )

or ...

Dim cTotal as currency
Dim db as dao.database
Dim rs as dao.recordset
set db=currentdb()
strsql = "SELECT SUM([NetKG]) AS NetKG_Total FROM tblVanDetails
WHERE
[RegistrationNo] = " & zz
set rs=db.openrecordset(strsql,dbopensnapshot)
if rs.eof then
lblTotal.Caption=""
else
lblTotal.Caption=rs!NetKG_Total
endif
rs.close
set rs=nothing
db.close
set db=nothing

Aug 29 '07 #4
On 29 Aug, 22:01, OldPro <rrossk...@sbcglobal.netwrote:
On Aug 29, 3:13 pm, Philip_coll...@lineone.net wrote:


On 29 Aug, 19:35, OldPro <rrossk...@sbcglobal.netwrote:
On Aug 29, 1:11 pm, Philip_coll...@lineone.net wrote:
First of all, it is bad form to use Date or Time as a field name,
variable name or control name, as these are common functions and
keywords.
Second, DSUM( ) will give you a total based on a SQL query.
Third, SQL can return records or record totals, not both mixed. That
being said, it is possible to have an aggregate sum, where only the
last record would show the total. This is also problematic as Access
doesn't guarantee that any particular record will be the last one.
This will give you a one record total:
strsql = "SELECT SUM([NetKG]) AS NetKG_Total FROM tblVanDetails WHERE
[RegistrationNo] = '" & zz & "'"- Hide quoted text -
- Show quoted text -
Thanks for the response. how do I place that sql within the above code
to get the form to see the total? I'm quite happy to change the field
names!
regards
Phil- Hide quoted text -
- Show quoted text -

It depends on when you want the total to show. If you want it to show
after zz is updated, then put the code in that controls' afterupdate
event. You will want the total control to be a label.
You can use either...

lblTotal.Caption=DSUM("NetKG","tblVanDetails", "[RegistrationNo] = "
& zz )

or ...

Dim cTotal as currency
Dim db as dao.database
Dim rs as dao.recordset
set db=currentdb()
strsql = "SELECT SUM([NetKG]) AS NetKG_Total FROM tblVanDetails
WHERE
[RegistrationNo] = " & zz
set rs=db.openrecordset(strsql,dbopensnapshot)
if rs.eof then
lblTotal.Caption=""
else
lblTotal.Caption=rs!NetKG_Total
endif
rs.close
set rs=nothing
db.close
set db=nothing- Hide quoted text -

- Show quoted text -
Thanks for the help! I dont seem to be able to get my head round it!
so a little more info might help. I build the query to show the how
many times the vehicle comes onto the site and how much refuse it has
left, I then open another form frmWeightsTotals using the qry as the
record source. What I want to do is put a control on the forms footer
showing the number of visits and the total weight. it's probably me,
but I tried both your solutions thinking that i might refer to the
first form, but i kept getting syntax errors
Thanks again for the help.
regards
Phil

Aug 30 '07 #5

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

Similar topics

1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
6
by: Andy | last post by:
Hello, I am having many problems with setting up a parameter query that searches by the criteria entered or returns all records if nothing is entered. I have designed an unbound form with 3...
3
by: Peter Bailey | last post by:
Could someone please tell me how to pass criteria as if it were a parameter. I have a routine now that creates the sql string (well almost). at present the parameter is so I can pass one item ie...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
2
by: xinsir | last post by:
dynamic array as a byref parameter by used in function and have a Marshal error ,what is the matter?thanks source like as this . ------------------------------------------------------ Declare...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
5
by: jjyconsulting | last post by:
Newbie needing some help. I have a tblParticipants. The fields include gender, education_level, income, occupation etc., I'm trying to create a form where a user can run a query from the form and...
3
jenkinsloveschicken
by: jenkinsloveschicken | last post by:
I am somewhat new to Access and have been tasked with creating a reporting database for my operation. The problem I am having is that I am needing to use the Count function to calculate total...
0
by: vinbelgian | last post by:
I have some trouble with making a buffer in vb.net. I use a C dll that requires me to give him a pointer to a buffer of bytes where he is going to write bytes to, depending on the command i...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
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...
0
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
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...

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.