命令按钮无法正常工作

Command button not working properly

我在表单上设置了一个命令按钮,旨在为提供直接借记信息的持有人创建发票。我有四个 tables,Holder,Product(与 Holder 相关),Transaction(与 Product 相关)和 Invoice(与 holder 相关)。该按钮搜索所有指示直接借记的持有人,循环浏览这些持有人并查找以前未开具发票的所有交易,对交易求和,然后将金额输入发票 table 的新记录中。然后它应该循环通过它用来制作发票的交易并将新创建的发票 ID 添加到交易中。这是我使用的代码:

私人订阅 btnDebitInvoices_Click()

Dim db As DAO.Database
Dim rstHolder As DAO.Recordset
Dim rstTrans As DAO.Recordset
Dim rstSum As DAO.Recordset
Dim rstInvoice As DAO.Recordset
Dim strSQL As String
Dim strSQLSum As String
Dim strSQLHolder As String

Set db = CurrentDb

'Select all unique holders that have provided direct debit information
strSQLHolder = "SELECT DISTINCT HolderID " _
    & "FROM tblHolder " _
    & "WHERE (tblHolder.DirectDebit = 40);"

Set rstHolder = db.OpenRecordset(strSQLHolder)

rstHolder.MoveFirst

Do While Not rstHolder.EOF

    'Select all transactions that have not previously been invoiced and sum the TxAmount fields
    strSQL = "SELECT * " _
    & "FROM (tblTransaction " _
    & "INNER JOIN tblProduct ON tblTransaction.fkProductID = tblProduct.ProductID) " _
    & "INNER JOIN tblHolder ON tblProduct.fkHolderID = tblHolder.HolderID " _
    & "WHERE (((tblTransaction.TxInvoice) IS NULL) " _
    & "AND ((tblTransaction.TxType) = 'Fee') " _
    & "AND ((tblHolder.HolderID) = '" & rstHolder!HolderID & "'))"

    Set rstTrans = db.OpenRecordset(strSQL)
    rstTrans.MoveFirst

    strSQLSum = "SELECT SUM(tblTransaction.TxAmount) As InvoiceSum " _
        & "FROM (tblTransaction " _
        & "INNER JOIN tblProduct ON tblTransaction.fkProductID = tblProduct.ProductID) " _
        & "INNER JOIN tblHolder ON tblProduct.fkHolderID = tblHolder.HolderID " _
        & "WHERE (((tblTransaction.TxInvoice) IS NULL) " _
        & "AND ((tblTransaction.TxType) = 'Fee') " _
        & "AND ((tblHolder.HolderID) = '" & rstHolder!HolderID & "'))"

    Set rstSum = db.OpenRecordset(strSQLSum)

    If rstSum("InvoiceSum") > 0 Then
        'Add a new record to the invoice table with the sum of the amount and tax fields of all transactions not previously invoiced
        Set rstInvoice = db.OpenRecordset("tblInvoice")
        rstInvoice.AddNew
        rstInvoice("fkHolderID").Value = rstHolder("HolderID")
        rstInvoice("InvoiceDate").Value = Format(Now, "m/dd/yyyy")
        rstInvoice("Amount").Value = rstSum("InvoiceSum")
        rstInvoice("Tax").Value = rstInvoice("Amount") * 0.05
        rstInvoice.Update

        rstTrans.MoveFirst

        Do While Not rstTrans.EOF

            'Add newly created invoice number to transactions used to create invoice
            rstTrans.Edit
            rstTrans("TxInvoice").Value = rstInvoice("InvoiceID")
            rstTrans.Update

            rstTrans.MoveNext

        Loop

        rstTrans.Close
        rstInvoice.Close

    Else
        rstHolder.MoveNext
    End If
        DoCmd.RunCommand acCmdSaveRecord
        rstHolder.MoveNext
Loop

rstHolder.Close
db.Close

结束子

它在一定程度上起作用,但不完全。当我单击按钮时,它会为第一个持有人创建发票(尽管只有在我关闭按钮所在的表单然后重新打开它之后)然后在 rstTrans("TxInvoice").Value = rstInvoice("InvoiceID") 行表示没有选择当前记录。如果在重新打开表单后,我再次单击按钮,它会正确创建所有发票,但会将第一次单击按钮后创建的发票中的发票 ID 添加到事务 table 中的所有事务。我有一种感觉,我很接近,但不知道是什么导致了失败。提前感谢您提供的任何帮助。

tblInvoice中新添加的记录不保证是AddNew后的当前记录。我建议尝试以下操作来保存键值以备后用:

旧代码:

    rstInvoice.AddNew
    rstInvoice("fkHolderID").Value = rstHolder("HolderID")
    ...

        rstTrans.Edit
        rstTrans("TxInvoice").Value = rstInvoice("InvoiceID")
    ...

新代码:

 Dim lInvoiceID As Long
    rstInvoice.AddNew 
    lInvoiceID = rstInvoice("InvoiceID")    ' Save the new key
    rstInvoice("fkHolderID").Value = rstHolder("HolderID")
    ...

        rstTrans.Edit
        rstTrans("TxInvoice").Value = lInvoiceID