Variable number of Field to add up

hcabs99

Active Member
Joined
May 9, 2006
Messages
257
Hi

I have a data table which will hold several weeks worth of data , fields marked as week 1, week 2 up to and including week 9

I need to add up the data in these fields, but the number of weeks that I need to add up will need to be variable based on an entry that a user keys into a form.

So, user enters in 2, I need the database to total up the data for week 1 and week 2 ,

I know I could write 9 separate queries which would run based on the selection, but I would like to know if there is some clever coding way round this.

Any suggestions?

Cheers
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
If you have a numeric field for week number (i.e. 1, 2,...,9), then you can simply use a Parameter Query to get what you want, i.e. the Criteria under the week number field should look like:
Code:
<= [Enter max week number]
Then whenever you run this query, it will prompt you for the maximum week number, and return all records less than or equal to the number entered.
 
Upvote 0
I think he's saying his table has 9 fields
Field_1
Field_2
...
Field_9

and if the person enters a 2 then he wants to do
select Field_1 + Field_2

but if the person enters a 4 then he wants to do
select Field_1 + Field_2 + Field_3 + Field_4
 
Upvote 0
I think he's saying his table has 9 fields
Field_1
Field_2
...
Field_9

and if the person enters a 2 then he wants to do
select Field_1 + Field_2

but if the person enters a 4 then he wants to do
select Field_1 + Field_2 + Field_3 + Field_4
If that is the case, that is bad (not normalized) database structure.
You should NOT have a separate field for each week, just two fields, one for week number, and one for the value associated with that week number.
If it is structured properly like that, one can see how easy this request now becomes!
 
Upvote 0

Forum statistics

Threads
1,221,774
Messages
6,161,862
Members
451,725
Latest member
durzoblint87

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top