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

sum two or more felds in a query

22
I need help in two things:

1. How can I sum the results of two or more fields in a query. I tried creating a variable like this: total: [field1] + [field2] + [field3] but i just get the concatenation of the three fields values.

2.In a report, include the page number by group, so it resets when a new group starts.
May 22 '08 #1
6 2247
EORTIZ
22
What happened is that there were null values in the fields. I created a variable that replace the Null for a zero. However, doing that, made the variable as text.

To correct it, I first created a table from the query, then run an UPDATE query to put zero where the value was NULL, and then create a query based on the table to create the variable that sums the fields as total:[field1]+[field2]+[field3]
May 22 '08 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi. Your calculated total field is correct - it is the field values which are the problem. These may look like numbers, but they are undoubtedly strings. When you add strings you get the concatenated result you have found. Another clue to look for is that strings are left-aligned by default, whereas numbers are always right-aligned.
I do not know why what these fields are actually strings - you may be using the format function (which returns a string value) or Nz (which also returns a string value). Anyway, to add them together you need to convert them back to numbers using the Val function:
Expand|Select|Wrap|Line Numbers
  1. total: val([field1]) + val([field2]) + val([field3])
There is a thread elsewhere in this forum about resetting page numbers in a report group. I don't have the thread to hand at present. However, here is how I do one of mine for a report on our academic areas (called Schools). This uses a global variable called PrevSchl (defined in the report's code module) to store the last school name as a comparator, and an unbound text box in the footer of the report for the page number (SchoolPages in the example below). The code is in the footers Format event.
Expand|Select|Wrap|Line Numbers
  1. Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
  2.     If PrevSchl <> Me.School_Name Then
  3.         Me.SchoolPages = 1
  4.         PrevSchl = Me.School_Name
  5.     Else
  6.         Me.SchoolPages = Me.SchoolPages + 1
  7.     End If
  8. End Sub
  9.  
Note that because the code is in the page footer I don't have to consider the use of Cancel if FormatCount > 1, as the footer is the last item to be placed on the page by Access. In general, Format events for Access reports may be called repeatedly as individual sections are moved around to fit, but the footer seems not to be moved in this way.

-Stewart

I need help in two things:

1. How can I sum the results of two or more fields in a query. I tried creating a variable like this: total: [field1] + [field2] + [field3] but i just get the concatenation of the three fields values.

2.In a report, include the page number by group, so it resets when a new group starts.
May 22 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Ahh, Your revised post was not there when I put mine together...

I think using an update is a fair bit of work, and would suggest that you simply use Val conversions on any calculated fields accidentally converted to strings in addition to Nz or whatever you are using at present.

-Stewart
May 22 '08 #4
EORTIZ
22
Thanks a lot, kind response.

I am not experiences with MS ACCESS codes, so... simple question How I define the global variable? :-P
May 22 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
Hi again. The global variable is defined at the top of the code module for the report - just below where it usually says Option Compare Database - as
Expand|Select|Wrap|Line Numbers
  1. Dim PrevSchl as String
(for the example given)
or equally valid:
Expand|Select|Wrap|Line Numbers
  1. Public PrevSchl as String
You will have to open the code module anyway to add the footer event code, so the easiest way if you have no existing public modules defined is to open the report in design view, go to the footer area of the report, right-click on white space in the footer and select properties, select the Event tab of the properties set, click On Format and choose build, code to open the code module and create a skeleton of the On Format event code. You can then scroll to the top of the code module and add the DIM or Public declaration.

I would caution that you may experience difficulties in using even a simple programmed approach such as the resettable page number described if you are totally unfamiliar with VBA code. You may find that until you gain experience and familiarity it is best to stick with the use of the built-in page numbering. This is your choice, of course, but I feel it is important that you understand that any programmed approach is dependent on a degree of familiarity with the VBA environment and conventions. There are no pre-built wizards or packages which will do this for you.

-Stewart
May 22 '08 #6
EORTIZ
22
Thanks a LOTTTTTTTT !!!! I could make it work!! Excellent Thanks thanks
May 23 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
4
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
6
by: Ryan | last post by:
I have a query which is quite complex. It's based on a set of data in a complex view which takes the data from several tables. In this complex query, if I allow the various parts of the query to...
2
by: Good Man | last post by:
Hi there Yes, I've read about JOINs, albeit after coding for a couple of years already using queries like the following: "SELECT m.LastName, m.FirstName, o.Address FROM members m, offices o...
4
by: Ray | last post by:
I need to create a report showing the candidate, class, score of judge 1, 2, 3, 4. Currently, I manage to obtain the result of candidate and score of judge 1, 2, 3 and 4 but not the class. I would...
4
by: Bob Alston | last post by:
Some more, rather specific Access performance questions. IN a split front-end & back-end Access/Jet ONLY LAN situation, and with all query criteria fields and join fields indexed: 1. Is is...
0
by: Gordon.E.Anderson | last post by:
short description: i've got a .net web site set up using a tableadapter attached to a sql server table - returning results only, no update of data. i've got a query (qry code below) set up to...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
7
by: ghjk | last post by:
I have a php search page which is having more than one search felds. This is my query SELECT * FROM vehicles WHERE VehicleType='$type' AND VehicleMake='$make' AND VehicleModel='$model' AND Year=...
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...
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
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
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...

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.