我希望你能帮助我。我正在使用MS Access 2003并创建了一个包含以下字段的表 RefNo,TransactionDate,Detail,Debit,Credit和balance。交易日期在表格中编制索引。我创建了一个表单,我在借方或贷方字段中输入日期和金额,它将计算余额。我的问题是,按顺序完成是好的,但是如果我需要在旧日期中间输入一个丢失的交易,则需要查看该日期的最后余额,并根据信用卡或借记金额添加或减去并且应该还调整了后续的平衡。任何人都可以帮我写代码吗?先感谢您。
答案 0 :(得分:0)
这就是我想出的。它需要一个非唯一的复合索引 您桌上的RefNo,TransactionDate,Debit和Credit(所有升序)(在我的示例中称为交易)
示例数据如下所示
Id RefNo TransactionDate Detail Debit Credit Balance
-- ----- --------------- --------------- ------ --------- -------
1 1 31/12/2010 Brought Forward £502.79
2 1 01/01/2011 Debit Item 1 £11.40
3 1 01/01/2011 Debit Item 2 £11.40
4 1 01/01/2011 Debit Item 3 £11.40
5 1 02/01/2011 Debit Item 1 £50.00
6 1 02/01/2011 Crebit Item 1 £72.20
7 1 03/01/2011 Debit Item 1 £11.40
8 1 03/01/2011 Debit Item 2 £11.40
9 1 03/01/2011 Credit Item 1 £1,200.00
11 2 01/01/2011 Debit Item 1 £120.00
12 2 01/01/2011 Credit Item 1 £800.00
13 2 02/01/2011 Debit Item 1 £10.00
VBA代码
Public Sub CalculateBalance(refNo As Long, transactionDate As Date)
Dim db As DAO.Database
Set db = CurrentDb
Dim transactions As DAO.Recordset
Set transactions = db.OpenRecordset("transactions")
transactions.Index = "RefNo_TransactionDate_Debit_Credit"
Dim balance As Currency
Dim previousBalanceBookmark As Variant
If Not (transactions.BOF And transactions.EOF) Then
transactions.Seek ">=", refNo, transactionDate, Null, Null
' Find the previous balance
Do While transactions.Fields("RefNo").Value = refNo
If Not IsNull(transactions.Fields("Balance").Value) Then
previousBalanceBookmark = transactions.Bookmark
If transactions.Fields("TransactionDate").Value < transactionDate Then
Exit Do
End If
End If
transactions.MovePrevious
If transactions.BOF Then Exit Do
Loop
If IsEmpty(previousBalanceBookmark) Then
' Create an opening balance
transactions.AddNew
transactions.Fields("RefNo").Value = refNo
transactions.Fields("TransactionDate").Value = transactionDate - 1
transactions.Fields("Detail").Value = "Brought Forward"
transactions.Fields("Balance").Value = 0
transactions.Update
' bookmark the balance
previousBalanceBookmark = transactions.LastModified
End If
' Re-calculate balance from the bookmarked record onwards
transactions.Bookmark = previousBalanceBookmark
balance = transactions.Fields("Balance").Value
Dim previousDate As Date
previousDate = transactions.Fields("TransactionDate").Value
transactions.MoveNext
Do Until transactions.Fields("RefNo") <> refNo
' Update the balance of previous days last record
If transactions.Fields("TransactionDate").Value > previousDate Then
transactions.MovePrevious
transactions.Edit
transactions.Fields("Balance").Value = balance
transactions.Update
transactions.MoveNext
End If
' Clear any existing balances
If Not IsNull(transactions.Fields("Balance").Value) Then
transactions.Edit
transactions.Fields("Balance").Value = Null
transactions.Update
End If
balance = balance - Nz(transactions.Fields("Debit").Value, 0)
balance = balance + Nz(transactions.Fields("Credit").Value, 0)
previousDate = transactions.Fields("TransactionDate").Value
transactions.MoveNext
If transactions.EOF Then Exit Do
Loop
' Update the last record
transactions.MovePrevious
transactions.Edit
transactions.Fields("Balance").Value = balance
transactions.Update
End If
transactions.Close
Set db = Nothing
End Sub
当您保存提供Refno和交易日期的新交易记录时,请调用此例程,它将重新计算当前交易日期之前的最近日期的每日余额。如果找不到先前的余额,则根据当前交易日期减去1天创建零余额。
其他强>
应将VBA代码粘贴到单独的模块中,即不在后面的表单代码中。
在表单上,您应该有一个保存按钮。您需要在按钮的On Click事件中添加一些代码以保存记录,然后运行CalculateBalance代码。这是一个例子
Private Sub SaveRecord_Click()
Application.RunCommand acCmdSave
Call CalculateBalance(Forms!transactions!RefNo, Forms!transactions!TransactionDate)
End Sub
请注意,当您添加命令按钮并使用向导配置按钮操作时,Access会创建一个宏,而不是在窗体后面插入代码。您需要将按钮的点击后属性从[Embedded Macro]
更改为[Event Procedure]
。
希望这有帮助