具有访问权限的基本库存设置

Basic Inventory Setup with Access

所以我是访问新手,需要帮助来完成我的库存数据库。

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

Items: Item#, Item Desc, Item Spec Reorder Level, Qty to Reorder, Qty on Hand

Materials_Used: ID(just an Autonumber field), Project, Item#, Amount

Projects: Project

Purchase_Orders: PONum, Received Date

Receiving_Amount: ID(again autonumber), Item#, Amount, PONum

我在 2 个方面遇到问题:

  1. 在 PONum 中绑定项目 Receiving_Amount:目前我有一个 Purchase_Orders 表单,要求用户输入 datePOnum 以及数据表形式的接收金额的子表格。事情是在我输入 datePONum 并向下输入收到的项目后,我得到并输入参数值框 Purchase_Orders.ID 和 Purchase_Orders.PONumber ------ 感谢 Wayne 解决了这个问题!

  2. 一旦通过采购订单表格(添加)和材料使用表格(减去)收到物品后,物品的手头数量table自动更新

Database Relationship

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

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

那么我强烈建议跟踪您所做的更改。例如,用户输入数量 5,保存更改。明天,查看数据,看到 5 并想将其更改为 6……这会破坏真实的库存。一种防止方法是为 PO 行项目设置 'flag',表明它们已被处理并阻止再次更新。

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

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

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

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