命令按钮不能正常工作

时间:2015-02-18 17:17:02

标签: ms-access

我在表单上设置了一个命令按钮,用于为已提供直接付款信息的持有人创建发票。我有四张桌子,持有人,产品(与持有人有关),交易(与产品有关)和发票(与持有人有关)。该按钮搜索所有已指示直接借记的持有人,循环访问这些持有人并查找之前未开具发票的所有交易,将交易总和然后将金额输入到发票表中的新记录中。然后,它应循环完成用于生成发票的事务,并将新创建的发票ID添加到事务中。这是我正在使用的代码:

Private Sub 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

End Sub

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

1 个答案:

答案 0 :(得分:0)

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