K
kdaniel7979
Earlier I posted a problem:
In my database I would like to have the following fields:
Cost
Start Date
End Date
I would like to be able to figure out how to spread out this cost
between the two dates and summerize these cost by months.
For example:
Input Information
A - Cost $30, Start Jan-06, End Mar-06
B - Cost $40, Start Feb-06, End Mar-06
C - Cost $10, Start Feb-06, End Mar-06
etc
Created Export table or Query:
Cost Start End Jan-06 Feb-06 Mar-06
A $30 Jan-06 Mar-06 $10 $10 $10
B $40 Feb-06 Mar-06 $20 $20
C $10 Feb-06 Mar-06 $5 $5
Sum $10 $35 $35
And Rob Oldfield was nice enough to recommend:
"The way that I would do it would be loop through each record of the
table,
then loop through each of the months that make up that record, and
append
each month to a temp table. Air code would be...
dim db as database
dim rsin as recordset
dim rsout as recordset
set db=currentdb
set rsin=db.openrecordset("SourceTable")
db.execute "delete from TargetTable"
set rsout=db.openrecordset("TargetTable")
rsin.movefirst
dim amount as double,months as integer, d as date, sd as date, i as
integer
do while not rsin.eof
sd=rsin("Start Date")
months=datediff("m",sd,rsin("End Date"))+1
amount=rsin("Cost")/(months+1)
d=dateserial(year(sd),month(sd),1)
for i=0 to months-1
rsout.addnew
rsout("Date")=dateadd("m",i,d)
rsout("Amount")=amount
rsout("ID")=rsin("ID") 'where ID is your PK field
rsout.update
next
rsin.movenext
loop
That'll give you a list broken down by month. To get what you want
after
that, if would just be a crosstab query (row headings would be the pk,
column headings the dates, summing amounts). You could then link that
back
to your source table if it contained any further information that you
wanted
to include.
Note that the reason to pick out the first of each month is so that the
grouping works easily. If you're just formatting as dates without the
day
showing later, then that doesn't matter.
I realise that, if you get that working, your next question is going to
be
how to create a report to show your data. I'd suggest worrying about
that
later."
However, this code is a little beyond me. Can someone give me some
recommendations or where to start with this?
Thx
Dan
In my database I would like to have the following fields:
Cost
Start Date
End Date
I would like to be able to figure out how to spread out this cost
between the two dates and summerize these cost by months.
For example:
Input Information
A - Cost $30, Start Jan-06, End Mar-06
B - Cost $40, Start Feb-06, End Mar-06
C - Cost $10, Start Feb-06, End Mar-06
etc
Created Export table or Query:
Cost Start End Jan-06 Feb-06 Mar-06
A $30 Jan-06 Mar-06 $10 $10 $10
B $40 Feb-06 Mar-06 $20 $20
C $10 Feb-06 Mar-06 $5 $5
Sum $10 $35 $35
And Rob Oldfield was nice enough to recommend:
"The way that I would do it would be loop through each record of the
table,
then loop through each of the months that make up that record, and
append
each month to a temp table. Air code would be...
dim db as database
dim rsin as recordset
dim rsout as recordset
set db=currentdb
set rsin=db.openrecordset("SourceTable")
db.execute "delete from TargetTable"
set rsout=db.openrecordset("TargetTable")
rsin.movefirst
dim amount as double,months as integer, d as date, sd as date, i as
integer
do while not rsin.eof
sd=rsin("Start Date")
months=datediff("m",sd,rsin("End Date"))+1
amount=rsin("Cost")/(months+1)
d=dateserial(year(sd),month(sd),1)
for i=0 to months-1
rsout.addnew
rsout("Date")=dateadd("m",i,d)
rsout("Amount")=amount
rsout("ID")=rsin("ID") 'where ID is your PK field
rsout.update
next
rsin.movenext
loop
That'll give you a list broken down by month. To get what you want
after
that, if would just be a crosstab query (row headings would be the pk,
column headings the dates, summing amounts). You could then link that
back
to your source table if it contained any further information that you
wanted
to include.
Note that the reason to pick out the first of each month is so that the
grouping works easily. If you're just formatting as dates without the
day
showing later, then that doesn't matter.
I realise that, if you get that working, your next question is going to
be
how to create a report to show your data. I'd suggest worrying about
that
later."
However, this code is a little beyond me. Can someone give me some
recommendations or where to start with this?
Thx
Dan