迭代(for 循环)ms 使用过去值进行访问



我试图将代码从VBA excel翻译为访问。我的数据是一列价格,我想计算回报。这是excel中的原始VBA代码:

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

要了解我在 excel 中的输出,请单击此处。在 Access 中,我在价格列旁边创建了一个新列,我想像在 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

我有下表,您可以在此处看到我无法管理循环。最后我的收藏中没有物品。我查看了像这里这样的其他循环示例,但我没有找到如何使用最后一个结果进行迭代。

抱歉,我真的是Ms Access和SQL的初学者。我从本周开始,所以如果我的问题非常基本,我深表歉意。

编辑:我添加了图像,并将Firsttransaction和Lasttransaction替换为"FirstPrice"和"EndPrice"。

编辑2:由于我的新特权,我可以为感兴趣的人分享一个样本。

我已经将您的完整代码更新为应有的内容。同样,我没有方便的 Access 数据库来测试它,但它可以编译并且应该可以工作:

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

最新更新