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

How to sum total values in multiple column?

Hi,
Is there any possibility to sum all the values in many (say n) columns in a single table using VBA in access 2003?
Say I have a table with 200 columns. Say the table name is TableCOUNT and the columns inside the table have names such as LX1, LX2,....,LX200.
If I use DSum for a single (or a few) columns giving the name of the column, everything is OK. I get a total for this (these) ciolumns:

Expand|Select|Wrap|Line Numbers
  1. Open "N:\FM AM\TEST_LOG1.txt" For Output As #intOutFile
  2. Print #intOutFile, "Printing sum of a single column LX5 in TableCOUNT table"
  3.  
  4.        curX1 = DSum("[LX5]", "TableCOUNT")
  5.  
  6. Print.Debug curX1
  7. Print #intOutFile, curX1
  8. Close intOutFile
  9.  
  10.  
But if I want to go through 200 columns with a loop, the DSum instead of returning the sum in each column, returns the sum of the NUMBER OF CELLS IN EACH COLUMN!!! (eg. If I have 200 columns with 10 cells I get a sum of 2000, with intermediate values in the LOOP being 10, 20, 30,...,2000:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Open "N:\FM AM\TEST_LOG1.txt" For Output As #intOutFile
  3. Print #intOutFile, "Printing sum of each column in TableCOUNT table"
  4.  
  5. For yy = 1 To 200
  6.  
  7.        curX1 = DSum(yy, "TableCOUNT")
  8.        If curX1dif < 1 Then Print #intOutFile, "WARNING!!! All values in this Fund are ZERO"
  9.  
  10. Print.Debug curX1
  11. Print #intOutFile, curX1
  12. Next yy
  13. Close intOutFile
  14.  
Any ideas to solve this problem?

Thanks in advance
Jan 12 '10 #1
5 7625
nico5038
3,080 Expert 2GB
This DSUM() approach will be slooow.
I would start with creating a GroupBy (Use the "E" or "Sigma" button to activate) and place all fields. Next add Sum() as the aggregation function.
Now all 10 rows will be totalled and when you want a loop you can loop through the individual (SumOf...) fields.
Faster is however to create a new query based on this groupby query and add all fields like:
Expand|Select|Wrap|Line Numbers
  1. SELECT L1 + L2, + L3 +.... FROM qryGroupBy
Now a simple single DLOOKUP can be used to write the result to your file or even better a Docmd.Transfertext can be used to create a textfile from the query result.

Idea ?

Nic;o)
Jan 12 '10 #2
MMcCarthy
14,534 Expert Mod 8TB
Please use code tags when posting code

Check out How to ask a question
Jan 12 '10 #3
MMcCarthy
14,534 Expert Mod 8TB
Nico is right about the DSUM approach and I would advise following his strategy. However, if for some reason you need to keep the DSUM approach then you can use a FOR ... LOOP to output your data as follows:

Expand|Select|Wrap|Line Numbers
  1. Dim i as Integer
  2.  
  3.     For i = 1 To 200
  4.         curX1 = DSum("[LX" & i & "]", "TableCOUNT")
  5.         If curX1dif < 1 Then Print #intOutFile, "WARNING!!! All values in this Fund are ZERO"
  6.  
  7.         Print.Debug curX1
  8.         Print #intOutFile, curX1
  9.     Next i
  10.  
Jan 12 '10 #4
Thank you very much for quick response. I didn't try Nico5038's strategy yet, but simple loop proposed by msquared works great!
Thanks again. That solved my big issue.
Jan 12 '10 #5
MMcCarthy
14,534 Expert Mod 8TB
I'm glad you got it working. I would take the time to explore Nico's suggestions though as his approaches would be a lot more efficient. Even if you don't use them this time there is always the next :D
Jan 12 '10 #6

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

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
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...
3
by: CSDunn | last post by:
Hello, I have 14 fields on a report that hold integer values. The field names use the following naming convention: T1Number, T2Number ....T14Number. I need to get a 'sub total' of all fields as...
13
by: scorpion53061 | last post by:
Very urgent and I am very close but need a little help to get me over the edge........ I need to write these columns to a html file with each row containing these columns (seperated by breaks)....
7
by: am72de | last post by:
Hi all, I've posted this problem some weeks ago, but noone had a solution. Perhaps now someone could help me. I have the following tables: Create Table T1 ( ID1 int Not Null , ID2 int Not...
0
by: James Hallam | last post by:
I have searched through the news groups and found many threads close to what I want but cannot get any of them to work... I have a table with expenses and invoices, what I want is a bar chart...
0
by: =?Utf-8?B?SEdK?= | last post by:
I am looking for a beter way to show column total at the bottom of a DataGridView. The way I've done by setting labels at outside the bottom of the DataGridView and show the total values in the...
4
by: drago | last post by:
Hi guys, I will be quick this time... Just want to know how we can calculate generic values in the same column. I am using invoice form with a services subform . I have a column of 'total' in subform...
0
by: Taxman | last post by:
Windows XP, MS Office Excel 2003 If the tasks, I’m trying accomplish have been addressed previously (separately or in combination). Please, provide the links or keyword search to find them. I’ve...
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
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
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
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...

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.