|
用ADO实现的,数据量太大时执行效率不高。
'===============================================================================
'-函数名称: 计算余额
'-功能描述: 计算余额
'-输入参数说明: 参数1: 必选 str表 As String 计算余额的表名称
' 参数2: 必选 str日期 As String 排序的字段名称
' 参数3: 必选 str借方 As String 计算余额增加方字段名称,字段类型:数字
' 参数4: 必选 str贷方 As String 计算余额减少方字段名称,字段类型:数字
' 参数5: 必选 str余额 As String 计算的余额字段,字段类型:数字
'-返回参数说明: 计算成功返回True;计算失败返回False
'-使用语法示例: bln = 计算余额("银行存款", "日期", "存入", "提款", "余额")
'-参考:
'-使用注意: 使用本函数时请保留函数信息内容,需要引用ADO
'-兼容性: 2000,XP,2003
'-作者: fan0217@163.com
'-更新日期: 2006-06-02
'===============================================================================
Function 计算余额(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 strSQL As String
Dim dblBalance As Double
Set conn = CurrentProject.Connection
strSQL = "SELECT * FROM " & 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
计算余额 = True
rs.Close
Set rs = Nothing
Set conn = Nothing
Exit_计算余额:
Exit Function
Err_计算余额:
计算余额 = False
Set rs = Nothing
Set conn = Nothing
MsgBox Err.Description
Resume Exit_计算余额
End Function
|
|