【问题标题】:Access SQL update into another table in VBA from a function从函数访问 SQL 更新到 VBA 中的另一个表
【发布时间】:2016-07-11 18:14:38
【问题描述】:

我有一个函数可以将值放入一个名为 InventoryTransaction 的表中,并且运行良好。但是,我需要使用添加到 InventoryTransaction 中的记录来更新另一个表。 但是我在尝试更新它一直给我错误的值时遇到了困难,任何指导将不胜感激

错误信息是:“Undefined function 'IT.QuantityWHERE' in expression”

Function EditTransaction(IT As InventoryTransaction, Optional CustomerOrderID, Optional PurchaseOrderID) As Boolean
Dim rsw As New RecordSetWrapper
Dim SQL As String




If rsw.OpenRecordset("InventoryTransaction", "[TransactionID] = " & IT.InventoryID) Then
    With rsw.Recordset
        If IT.TransactionType <= 0 Then
            Exit Function
        ElseIf IT.InventoryID = m_cNew_InventoryID Then
            rsw.AddNew
        ElseIf .EOF Then
            Exit Function
        Else
            rsw.Edit
        End If


        ![ItemID] = IT.ProductID
        ![TransactionQty] = IT.Quantity
        ![TransactionType] = IT.TransactionType
        ![LocationID] = IT.LocationID
        ![Time] = Now()

        EditTransaction = rsw.Update

        If IT.InventoryID = m_cNew_InventoryID Then
            rsw.Recordset.Bookmark = rsw.Recordset.LastModified
            IT.InventoryID = ![TransactionID]
        End If


End With

                SQL = "UPDATE Inventory " & _
              "SET Inventory.Qty = Inventory.Qty + IT.Quantity" & _
              "WHERE (Inventory.ItemID = IT.ProductID And Inventory.LocationID = IT.LocationID)"
        DoCmd.RunSQL SQL
End If

【问题讨论】:

  • 具体是什么错误?
  • 糟糕,错误提示“表达式中未定义函数 'IT.QuantityWHERE'

标签: sql vba ms-access


【解决方案1】:

它正在更新所有记录,因为您的 UPDATE 语句是这样说的。 我不知道您为什么切换到最新的 UPDATE 语句。 您应该坚持使用 Gustav 提供的 UPDATE 语句。

【讨论】:

    【解决方案2】:

    更正你的 SQL:

    SQL = "UPDATE Inventory " & _
          "SET Inventory.Qty = Inventory.Qty + IT.Quantity " & _
          "WHERE (Inventory.ItemID = IT.ProductID And Inventory.LocationID = IT.LocationID)"
    DoCmd.RunSQL SQL
    

    此外,在任何情况下,您都不应在记录集打开时退出函数:

    With rsw.Recordset
        ' Do stuff.
        ' Don't exit function.
        ' Do more stuff.
        .Close
    End With
    Set rsw = Nothing    
    ' Exit function now allowed.
    

    【讨论】:

    • 感谢您提供的信息,我已经更改了我的 SQL 代码,但问题是我只尝试更新 1 条记录,该记录是通过函数添加的。但现在,它正在尝试更新我的 InventoryTransactions 中的所有记录。我做错了吗?
    • 这是 SQL:code SQL = "UPDATE Inventory" & _ "INNER JOIN InventoryTransaction ON [Inventory].[LocationID] = [InventoryTransaction].[LocationID]" & _ "SET [ Inventory].[Qty] = [Inventory].[Qty] + [InventoryTransaction].[TransactionQty]" & _ "WHERE (([Inventory].[ItemID] = [InventoryTransaction].[ItemID]) 和 ([Inventory] .[LocationID] = [InventoryTransaction].[LocationID]))" DoCmd.RunSQL SQL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2020-08-04
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多