0

所以我是一个新手,需要帮助完成我的库存数据库。

目前我有以下表格和其中的字段:(表:字段,第一个字段是主键)

项目:项目编号、项目描述、项目规格重新订购级别、要重新订购的数量、现有数量

Materials_Used:ID(只是一个自动编号字段)、项目、项目#、金额

项目:项目

Purchase_Orders: PONum, 收到日期

Receiving_Amount:ID(再次自动编号)、Item#、Amount、PONum

我在 2 个方面遇到问题:

  1. 将 PONum 与接收金额绑定:目前我有一个Purchase_Orders表格,要求用户以数据表形式输入接收金额的datePOnum和子表格。事情是在我输入一个dateand之后PONum,然后向下移动到输入 Items received I get and Enter Parameter Value box Purchase_Orders.ID 和 Purchase_Orders.PONumber ------ 这个问题由于韦恩而得到解决!

  2. 一旦通过 PO 表单(添加到其中)和使用的材料表单(减去)接收到项目,让 Items 表的现有数量自动更新

数据库关系

4

1 回答 1

0

由于您将在多用户更新环境中工作,因此您需要确保与其他用户没有冲突。最安全的方法是使用 TRANSACTION。

接下来,您需要决定如何以及何时对您的两个表进行更新。让我们使用“选项 1”,即用户在完成后单击的按钮。当他们单击按钮时,您需要调用以下子例程。您还应该跟踪用户是否对子表单进行了任何更改,但忘记单击“保存”按钮。

然后我强烈建议跟踪您所做的更改。例如,用户输入 qty of 5,保存更改。明天,查看数据,看到 5 并想将其更改为 6……这将破坏真实库存。一种防止方法是为订单行项目设置一个“标志”,指示它们已被处理并防止再次更新。

以下代码只是一个示例...我编写了我认为应该是输入和输出记录集的代码,但是您需要确保选择正确的输入行进行处理,然后为表选择输出行.

查找其中带有## 的注释......修复代码以使用您的控件名称。

如果您需要更多解释,请告诉我。

Option Compare Database
Option Explicit

'   BeginTrans, CommitTrans, Rollback Methods Example
'   After the BeginTrans method starts a transaction that isolates all the changes made,
'   the CommitTrans method saves the changes.
'   Notice that you can use the Rollback method to undo changes that you saved using
'   the Update method. Furthermore, the main transaction is nested within another transaction
'   that automatically rolls back any changes made by the user during this example.

'   One or more table pages remain locked while the user decides whether or not to accept the changes.
'   For this reason, make sure you only execute the transaction on some event - don't allow a user
'   to be interactive, else he may go to lunch and may lock pages someone else needs!

'   Add to: Receiving_Amount: ID(again autonumber), Item#, Amount, PONum
'   Subtract from: Materials_Used: ID(just an Autonumber field), Project, Item#, Amount

Sub BeginTransX_Update_Inventory()
    On Error GoTo Error_trap

    Dim wrkDefault      As DAO.Workspace
    Dim dbs             As DAO.Database
    Dim tblInput        As DAO.recordSet
    Dim tblItems        As DAO.recordSet
    'Dim tblMaterials    As DAO.recordSet

    ' Get default Workspace.
    Set wrkDefault = DBEngine.Workspaces(0)
    Set dbs = CurrentDb

    ' ## Change the following line to use the name of the form control that has your PONum
    Set tblInput = dbs.OpenRecordset("select * from MaterialsRec where PONum = " & Me.txtPONum & ";")            '<<< This will be the source of your changes. Can use a query to filter exact rows.

    ' Start transaction.
    wrkDefault.BeginTrans

    Do While Not tblInput.EOF

        Set tblItems = dbs.OpenRecordset("select * from [Items] where [Item#] = " & tblInput![item] & ";")    ' <<< This will be where the updates are applied.

        ' Increase Qty on Hand
        tblItems.Edit
        tblItems![Qty on Hand] = tblItems![Qty on Hand] + tblInput!Amount
        tblItems.Update

        '## Add a text field named 'ProcStatus' to table MaterialsRec, or delete the following update ... your choice...
        tblInput.Edit
        tblInput!ProcStatus = "updated"
        tblInput.Update

        tblInput.MoveNext
    Loop

    ' You can remove the following code if desired...
    ' Ask if the user wants to commit to all the changes made above.
    If MsgBox("Save changes?", vbYesNo) = vbYes Then
        wrkDefault.CommitTrans
    Else
        wrkDefault.Rollback
    End If

    tblInput.Close
    tblItems.Close
    'tblMaterials.Close
    Set tblItems = Nothing
    'Set tblMaterials = Nothing
    Set dbs = Nothing
    Set wrkDefault = Nothing
    Exit Sub

Error_trap:
    wrkDefault.Rollback
    MsgBox "An error was encountered, but all changes were rolled back." & vbCrLf & _
            "Err: " & Err.Number & vbCrLf & _
            "Desc: " & Err.Description
End Sub
于 2016-04-13T20:48:10.037 回答