' '=============================================================================== Function 计算余额(str表 As String, _ str日期 As String, _ str分类 As String, _ str借方 As String, _ str贷方 As String, _ str余额 As String) As Boolean On Error GoTo Err_计算余额 Dim conn As New ADODB.Connection Dim rs As New Recordset Dim rsTemp As New Recordset Dim strSQL As String Dim dblBalance As Double Set conn = CurrentProject.Connection strSQL = "SELECT DISTINCT " & str分类 & " FROM " & str表 rsTemp.Open strSQL, conn, adOpenKeyset, adLockOptimistic Do While Not rsTemp.EOF strSQL = "SELECT * FROM " & str表 strSQL = strSQL & " WHERE " & str分类 & " = " & rsTemp(str分类) '如果分类字段的数据类型为文本,请使用以下这句代码 'strSQL = strSQL & " WHERE " & str分类 & " = '" & rsTemp(str分类) & "'" strSQL = strSQL & " ORDER BY " & str日期 & ";" rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic dblBalance = 0 Do While Not rs.EOF rs(str余额) = Nz(rs(str借方), 0) - Nz(rs(str贷方), 0) + dblBalance dblBalance = rs(str余额) rs.Update rs.MoveNext Loop rs.Close rsTemp.MoveNext Loop 计算余额 = True
rsTemp.Close Set rsTemp = Nothing Set rs = Nothing Set conn = Nothing
Exit_计算余额: Exit Function
Err_计算余额: 计算余额 = False Set rsTemp = Nothing Set rs = Nothing Set conn = Nothing MsgBox Err.Description Resume Exit_计算余额 End Function