T
Ted Allen
Hi Kathy,
Glad to hear that it is working, even if not quite
correct yet.
For some reason I can't see your last post in the
Microsoft Newsgroup website. Luckily I decided to check
out the MSDN site and saw your post through a search
there. Anyway, my response is below, and I have posted
your last post copied from MSDN for others that may see
this (I couldn't reply there because I couldn't remember
my passport info - it's been a long time since I used
that). I have posted this at the top of the thread in
case there is some limit as to how many levels it will
allow.
If your numbers go to 8 decimals, you should probably
change intTotalRet to be double instead (floating point
number capable of holding up to about 15 significant
digits). Try changing the lines that this variable
occurs in to the following (taken out of context to list
individual lines).
Dim dblTotalRet As Double
dblTotalRet = 100
!RunTotRet = dblTotalRet
dblTotalRet = dblTotalRet * (1 + Nz(!TotRet, 0))
This should eliminate the problem with rounding in the
loop. I believe that when you assign the value to the
RunTotRet field, it will be rounded as appropriate (if it
is an integer field it will round to an integer). So, if
your results are still being rounded too much, you can
change the number type for the RunTotRet field in your
table to single, or double, or currency.
Hopefully that will take care of your rounding problem.
Post back and let me know whether the results are correct.
-Ted Allen
---------------------------------------------------------
Listed below is your original message for reference.
---------------------------------------------------------
I did find one record that had a blank field. I fixed
that, but also changed the code just in case it's an
issue in the future as you suggested. After that, the
code ran!!!!
However, you were correct that there is a problem with
the results rounding. Even though the RunTotRet field is
formatted for two decimal points, it is rounding to a
whole number. The TotRet field does not contain whole
numbers. It is formatted for two decimal places, but
some of the actual data entered in the field can have up
to 8 decimal points.
What are your suggestions?
'Dimension Variables
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strMsg As String
Dim intStartMonth As Integer
Dim intStartYear As Integer
Dim intTotalRet As Integer
strMsg = "Please enter a number from 1 to 12 for the"
strMsg = strMsg & "month that you would like "
strMsg = strMsg & "to use as the baseline for the "
strMsg = strMsg & "calculation of cumulative returns."
intStartMonth = InputBox(strMsg, "Enter Start Date")
strMsg = "Please enter a 4-digit number for the "
strMsg = strMsg & "year that you would like "
strMsg = strMsg & "to use as the baseline for the "
strMsg = strMsg & "calculation of cumulative returns."
intStartYear = InputBox(strMsg, "Enter Start Date")
strSQL = "SELECT * FROM [tbl Composite] "
strSQL = strSQL & "WHERE (Year > " & intStartYear
strSQL = strSQL & ") OR (Year = " & intStartYear
strSQL = strSQL & " AND Month >= " & intStartMonth & ") "
strSQL = strSQL & "ORDER BY Year, Month"
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
intTotalRet = 100
With rst
..MoveFirst
Do While Not .EOF
.edit
!RunTotRet = intTotalRet
intTotalRet = intTotalRet * (1 + Nz(!TotRet, 0))
.Update
.MoveNext
Loop
End With
Set rst = Nothing
Set db = Nothing
End Sub
starting point in a particular month (9/1984) in the
IDTotRet field. I need to calculate a figure based on
two fields so that it picks up the number from the
previous month/year record.
field would be 101.27. [IDTotRet]*(1+[TotRet]).
101.27 - that was calculated for the previous month and
perform the same calculation for that month. In this
instance, the IDTotRet calculated for 10/1984 was
101.27. The TotRet for 11/1984 was 0.91%, what I want in
my new field would be 102.19.
Glad to hear that it is working, even if not quite
correct yet.
For some reason I can't see your last post in the
Microsoft Newsgroup website. Luckily I decided to check
out the MSDN site and saw your post through a search
there. Anyway, my response is below, and I have posted
your last post copied from MSDN for others that may see
this (I couldn't reply there because I couldn't remember
my passport info - it's been a long time since I used
that). I have posted this at the top of the thread in
case there is some limit as to how many levels it will
allow.
If your numbers go to 8 decimals, you should probably
change intTotalRet to be double instead (floating point
number capable of holding up to about 15 significant
digits). Try changing the lines that this variable
occurs in to the following (taken out of context to list
individual lines).
Dim dblTotalRet As Double
dblTotalRet = 100
!RunTotRet = dblTotalRet
dblTotalRet = dblTotalRet * (1 + Nz(!TotRet, 0))
This should eliminate the problem with rounding in the
loop. I believe that when you assign the value to the
RunTotRet field, it will be rounded as appropriate (if it
is an integer field it will round to an integer). So, if
your results are still being rounded too much, you can
change the number type for the RunTotRet field in your
table to single, or double, or currency.
Hopefully that will take care of your rounding problem.
Post back and let me know whether the results are correct.
-Ted Allen
---------------------------------------------------------
Listed below is your original message for reference.
---------------------------------------------------------
I did find one record that had a blank field. I fixed
that, but also changed the code just in case it's an
issue in the future as you suggested. After that, the
code ran!!!!
However, you were correct that there is a problem with
the results rounding. Even though the RunTotRet field is
formatted for two decimal points, it is rounding to a
whole number. The TotRet field does not contain whole
numbers. It is formatted for two decimal places, but
some of the actual data entered in the field can have up
to 8 decimal points.
What are your suggestions?
'Dimension Variables
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strMsg As String
Dim intStartMonth As Integer
Dim intStartYear As Integer
Dim intTotalRet As Integer
strMsg = "Please enter a number from 1 to 12 for the"
strMsg = strMsg & "month that you would like "
strMsg = strMsg & "to use as the baseline for the "
strMsg = strMsg & "calculation of cumulative returns."
intStartMonth = InputBox(strMsg, "Enter Start Date")
strMsg = "Please enter a 4-digit number for the "
strMsg = strMsg & "year that you would like "
strMsg = strMsg & "to use as the baseline for the "
strMsg = strMsg & "calculation of cumulative returns."
intStartYear = InputBox(strMsg, "Enter Start Date")
strSQL = "SELECT * FROM [tbl Composite] "
strSQL = strSQL & "WHERE (Year > " & intStartYear
strSQL = strSQL & ") OR (Year = " & intStartYear
strSQL = strSQL & " AND Month >= " & intStartMonth & ") "
strSQL = strSQL & "ORDER BY Year, Month"
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
intTotalRet = 100
With rst
..MoveFirst
Do While Not .EOF
.edit
!RunTotRet = intTotalRet
intTotalRet = intTotalRet * (1 + Nz(!TotRet, 0))
.Update
.MoveNext
Loop
End With
Set rst = Nothing
Set db = Nothing
End Sub
together by the Month and Year in order to have a 100%-----Original Message-----
I have a query that joins two tables. They are joined
starting point in a particular month (9/1984) in the
IDTotRet field. I need to calculate a figure based on
two fields so that it picks up the number from the
previous month/year record.
the TotRet for 10/1984 were 1.27%, what I want in my newComposite Table: contains fields - month, year, TotRet
Inception Table: contains fields - month, year, IDTotRet
For example, if the IDTotRet for 9/1984 were 100% and
field would be 101.27. [IDTotRet]*(1+[TotRet]).
the calculation in the new field for 10/1984 - theNow for the next month, I want the expression to pick up
101.27 - that was calculated for the previous month and
perform the same calculation for that month. In this
instance, the IDTotRet calculated for 10/1984 was
101.27. The TotRet for 11/1984 was 0.91%, what I want in
my new field would be 102.19.
And [Year]=1984,100,[IDTotRet]*(1+[TotRet]))I started with this expression ITotRet: IIf([Month]=9
to get it to pick up from the previous record.However, I donâ?Tt know where to include the information