iteration (for-loop) ms Access with past value

1.6k views Asked by At

I tried to translate a code from VBA excel to access. My data is a column of prices and I want to compute the returns. This is the original VBA code in excel:

DerCol = Cells(T.Row, Columns.Count).End(xlToLeft).Column
Cells(T.Row, DerCol + 1) = "Returns"

For i = T.Row + 2 To T.End(xlDown).Row
    Cells(i, DerCol + 1) = Application.WorksheetFunction.Ln(Cells(i, T.Column)) - Application.WorksheetFunction.Ln(Cells(i - 1, T.Column))
Next i

To get an idea of the output that I have in excel, click here. In Access, I created a new column next to the prices' column and I would like to fill in exactly like in excel:

Sub vardaily()
    Dim db As Database, T As Object, DerCol As Integer, y As TableDef
    Dim rs As DAO.Recordset, i As Integer, strsql As String

    'idea = SELECT prices FROM dailypricing, then creates newtable "VAR", copy and prices, compute historical and parametric VAR '

    'create a new table var_daily'
    Set db = CurrentDb() 

    'insert the pricing date and the prices from dbo_daily'
    db.Execute "CREATE TABLE VAR_daily" _
                & "(PricingDate CHAR, Price Number);" 

    'where clause to select the same traded product only'
    db.Execute " INSERT INTO VAR_daily " _
                & "SELECT PricingDate, Price " _
                & "FROM dbo_PricingDaily " _
                & "WHERE IndexId = 1;" 

    db.Execute " ALTER TABLE VAR_daily " _
                & "ADD COLUMN Returns Number;"

    'sql request to store prices'       
    strsql = "SELECT First(Price) as FirstPrice, Last(Price) as EndPrice FROM VAR_daily;" 

    'dao.recordset of the store prices'
    Set rs = db.OpenRecordset(strsql, dbOpenDynaset) 

    'loop to change the prices'
    For i = 2 To i = rs.RecordCount 
        rs.Edit
        rs!Price(i) = Log(rs!Price(i)) - Log(rs!Price(i - 1))
        rs.Update
    Next i

    db.Execute "INSERT INTO VAR_daily " _
                & "(Returns) VALUES " _
                & "(" & rs![Price] & ");"
End Sub

I have the following table that you can see here I can not manage with the loop. I have no item in my collection at the end. I looked at other example of loops like here but I did not find how to make an iteration with the last result.

Sorry, I really am a beginner in Ms Access and SQL. I started this week so I apologize if my question is very basic.

EDIT: I added the images and I replaced Firsttransaction and Lasttransaction by "FirstPrice" and "EndPrice".

EDIT2: Thanks to my new privilege, I can share a sample for those who are interested.

1

There are 1 answers

2
Paul Ogilvie On BEST ANSWER

I have updated your complete code to what it should be. Again, I don't have an Access database handy to test it but it compiles and should work:

Sub vardaily()
    Dim db As Database
    Dim rs As DAO.Recordset, i As Integer, strsql As String
    Dim thisPrice, lastPrice

    'idea = SELECT prices FROM dailypricing, then creates newtable "VAR", copy and prices, compute historical and parametric VAR '

    'create a new table var_daily'
    Set db = CurrentDb()

    'insert the pricing date and the prices from dbo_daily'
    db.Execute "CREATE TABLE VAR_daily" _
                & "(PricingDate CHAR, Price Number);"

    'where clause to select the same traded product only'
    db.Execute " INSERT INTO VAR_daily " _
                & "SELECT PricingDate, Price " _
                & "FROM dbo_PricingDaily " _
                & "WHERE IndexId = 1 " _
                & "ORDER BY PricingDate;"

    db.Execute " ALTER TABLE VAR_daily " _
                & "ADD COLUMN Returns Number;"

    'sql request to retrieve store prices'
    strsql = "SELECT * FROM VAR_daily ORDER BY PricingDate;" ' just get all fields

    'dao.recordset of the store prices'
    Set rs = db.OpenRecordset(strsql, dbOpenDynaset)

    'loop to change the prices'
    lastPrice = rs.Fields("Price")     ' get price from first record and remember
    rs.MoveNext                        ' advance to second record and start loop
    While (Not rs.EOF())
        thisPrice = rs.Fields("Price")
        rs.Edit
            rs!Returns = Log(thisPrice) - Log(lastPrice)
        rs.Update
        lastPrice = thisPrice ' remember previous value
        rs.MoveNext           ' advance to next record
    Wend

End Sub