迭代(for-loop)ms访问过去值

时间:2015-06-16 10:12:21

标签: vba for-loop access-vba iteration recordset

我尝试将代码从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中的输出,请点击here。 在Access中,我在price'列旁边创建了一个新列,我想在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

我有下表,您可以看到here 我无法管理循环。最后我的收藏中没有任何项目。 我查看了像here这样的循环的其他示例,但我没有找到如何使用最后的结果进行迭代。

抱歉,我真的是Access和SQL的初学者。我本周开始,所以如果我的问题非常基本,我会道歉。

编辑:我添加了图像,我用“FirstPrice”和“EndPrice”替换了Firsttransaction和Lasttransaction。

EDIT2:感谢我的新特权,我可以为感兴趣的人分享a sample

1 个答案:

答案 0 :(得分:1)

我已将完整的代码更新为应有的代码。同样,我没有一个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