【问题标题】:Dynamic Calculated Field in VBA AccessVBA 访问中的动态计算字段
【发布时间】:2017-06-22 20:46:38
【问题描述】:

我正在尝试将字段添加到现有表中。该字段使用两个变量“myPrice”和“previousPrice”两个变量计算,即记录在某个日期的价格,以及“previousPrice”,即来自第一条记录的同一部分的价格,仅来自前一个月的价格。我正在使用一个循环来遍历整个价格记录集,从而每次都更改这两个变量。我的代码如下:

Function priceDifference()

Dim currentPrice As Currency
Dim previousPrice As Currency
Dim recordDate As Date
Dim previousDate As Date
Dim dbsASSP As DAO.Database
Dim priceTable As DAO.TableDef
Dim diffFld As DAO.Field2
Dim rstCurrentPrice As DAO.Recordset
Dim rstPreviousPrice As DAO.Recordset
Dim PN As String

Set dbsASSP = CurrentDb
strSQL = "SELECT * FROM qryPrice"
Set rstCurrentPrice = dbsASSP.OpenRecordset(strSQL, dbOpenDynaset)

Set priceTable = dbsASSP.TableDefs("tblPrice")
Set diffFld = priceTable.CreateField("Difference", dbCurrency)

If Not (rstCurrentPrice.EOF) Then
    rstCurrentPrice.MoveFirst
    Do Until rstCurrentPrice.EOF = True
        PN = rstCurrentPrice![P/N]
        recordDate = rstCurrentPrice![myDate]
        previousDate = Format(DateAdd("m", -1, recordDate), "M 1 YYYY")
        myPrice = rstCurrentPrice!Price
        strPreviousSQL = "SELECT * FROM qryPrice WHERE [MyDate] = #" & previousDate & "# AND [Type] = 'Net Price' AND [P/N] = " & PN & ""
        Set rstPreviousPrice = dbsASSP.OpenRecordset(strPreviousSQL, dbOpenDynaset)
        myCodeName = rstCurrentPrice!CodeName
        If DCount("[P/N]", "qryPrice", "[MyDate] = #" & previousDate & "# And [P/N] = " & PN) <> 0 Then
            previousPrice = rstPreviousPrice!Price
        Else
            previousPrice = myPrice
        End If

    rstCurrentPrice.MoveNext
    Loop
Else
    MsgBox "Finished looping through records."
End If

diffFld.Expression = myPrice - previousPrice

rstCurrentPrice.Close
rstPreviousPrice.Close

priceTable.Fields.Append diffFld

End Function

从语法上讲,它有效。然而,计算字段并没有给我正确的值,我不知道为什么,尽管我认为它与动态公式有关。

感谢任何帮助。谢谢!!

【问题讨论】:

    标签: sql vba ms-access


    【解决方案1】:

    当您将数字而不是公式附加到字段时,您的代码无法工作,但您应该 avoid calculated fields

    使用查询:

    SELECT 
        nPrice.[P/N], 
        nPrice.Price,  
        nPrice.MyDate,
        oPrice.MyDate,
        nPrice.Price-Nz(oPrice.Price,nPrice.Price) AS diff
    FROM (
       SELECT 
           [P/N],
           Price, 
           MyDate, 
           Format(DateAdd("m",-1,MyDate),"yyyymm") as previousMonthYear
       FROM 
           tblPrice) AS nPrice 
    LEFT JOIN (
        SELECT 
            [P/N], 
            Price, 
            MyDate,
            Format(MyDate,"yyyymm") AS monthYear 
        FROM 
            tblPrice)  AS oPrice 
    ON 
        nPrice.[P/N] = oPrice.[P/N] 
        AND nPrice.previousMonthYear = oPrice.monthYear
    

    这会计算动态差异,您不需要存储它。

    我假设每月最多有一个价格,否则您需要过滤子查询或只计算与之前最后价格的差额。 如果查询速度太慢([P/N] 和 MyDate 需要索引),您仍然可以使用它来更新表 tblPrice 的字段,但每次更新或插入价格时都必须更新 @ 987654324@

    【讨论】:

    • @Brian Michael,我没有验证代码本身是否正确,但这绝对是您想要使用的方法。看这里。
    • @BitAccesser 感谢您的帮助!这最终让我走上了正确的道路,我能够调整上面的代码并解决。
    【解决方案2】:

    我认为您可以通过使用此查询来避免代码循环

    SELECT A.qryPrice, A.MyDate, B.qryPrice, B.MyDate
    FROM qryPrice A LEFT OUTER JOIN qryPrice B 
    ON A.[P/N] = B.[P/N] 
    AND B.MyDate = DATEADD(month, -1, A.MyDate)
    

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多