473,800 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Re fresh
For Each qdf In db.QueryDefs
If qdf.Name = "qryWeightsTota ls" Then
db.QueryDefs.De lete 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.CreateQueryD ef("qryWeightsT otals", strsql)
db.QueryDefs.Re fresh
Me.Refresh
DoCmd.OpenForm "frmweightstota ls", 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 3274
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.Re fresh
For Each qdf In db.QueryDefs
If qdf.Name = "qryWeightsTota ls" Then
db.QueryDefs.De lete 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.CreateQueryD ef("qryWeightsT otals", strsql)
db.QueryDefs.Re fresh
Me.Refresh
DoCmd.OpenForm "frmweightstota ls", 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...@sbcg lobal.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.Re fresh
For Each qdf In db.QueryDefs
If qdf.Name = "qryWeightsTota ls" Then
db.QueryDefs.De lete 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.CreateQueryD ef("qryWeightsT otals", strsql)
db.QueryDefs.Re fresh
Me.Refresh
DoCmd.OpenForm "frmweightstota ls", 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...@sbcg lobal.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.Captio n=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.openrecor dset(strsql,dbo pensnapshot)
if rs.eof then
lblTotal.Captio n=""
else
lblTotal.Captio n=rs!NetKG_Tota l
endif
rs.close
set rs=nothing
db.close
set db=nothing

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


On 29 Aug, 19:35, OldPro <rrossk...@sbcg lobal.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.Captio n=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.openrecor dset(strsql,dbo pensnapshot)
if rs.eof then
lblTotal.Captio n=""
else
lblTotal.Captio n=rs!NetKG_Tota l
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 frmWeightsTotal s 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
17682
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 Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
6
7138
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 fields on it: Date (DateSpan1 and DateSpan2), Originator, and GroupName. I have added a button that triggers a query and uses those fields as its parameter criteria to populate a form. The user must be allowed to either enter all of the
3
4431
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 Module M10S. Want I want to do is send 1 or more parameters ie M10S OR M10SA OR ...... The query works with one parameter can I send the dynamic sql from vba as a complete parameter string once the form calls the query? also I notice the...
0
3518
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 boxes do not necessarily need to have a selection made to be used in the dynamic query. In essence the form can have selections made in all or none of its list boxes to form the dynamic query I am looking to get some feedback in reference to...
2
2465
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 Function finit Lib "DllCap.dll" _ (ByRef rdAcnt As ACNTINF2) As Integer <StructLayout(LayoutKind.Sequential)> Structure ACNTINF <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=16)> Dim
2
2947
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 sub-report will capture what grades the student has achieved in a list of different subjects and the reason I need it to be dynamic is that students take different subjects. Basically I've been trying to doctor the KB article on dynamic
5
3703
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 just choose the appropriate criterias from the combo boxes to get the results. I also want the query to run even if there is not a value in all the combo boxes ie., i want just all males with income level of over $100,000...Any insights or help...
3
13692
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 instances for each phone agent within the report, then use these counts to calculate a percentage for a report. Sample recordset: Specialist ID, Specialist Name, Brand Satisifaction, Specialist Satisfaction 111111 John Doe 4 ...
0
1423
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 give. the import of the dll: with the C procedures in comments ' declarations for integration test.dll ' int __stdcall test_Open(char *device, long to); <DllImport("test.dll")Private Shared Function test_Open(ByVal
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10504
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
10274
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
10251
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
7576
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
6811
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
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
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
3
2945
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.