请教:环境:Excel VBA + ADO + SQL + Access问题:如何实现Access数据库表字段“余额”值计算?例如:
(表字段)      日期      业务类型        摘要         借方     贷方    余额方向    余额(示例数据)  2007-7-30   销售开单   XSD20070730001   10000        0       借      10000
              2007-8-2    销售收款   XSS20070802001       0       7000     借        ?
写如下代码:Sub YuEr()  ''修改应收帐余额记录
    Dim Sql As String
    Call mycnn  ''连接数据库
    Sql = "Update 应收款 Set 余额 = 借方 - 贷方 where 摘要 = '" & TextBox1 & "'"
    cnn.Execute Sql
    cnn.Close
    Set RST = Nothing: Set cnn = Nothing
End Sub以上代码只能实现:余额=借方0-贷方7000希望实现:余额=余额10000+借方0-贷方7000不知该如何取得上一行余额值?

解决方案 »

  1.   

    问题解决,完整代码如下:Private Sub CommandButton1_Click()    ''修改应收帐最末行余额记录
        Dim Sql As String
        call mycnn
        Sql = "Select * From 应收款"
        rst.Open Sql, cnn, 3, 2, 1
        For i = 1 To rst.RecordCount
            If i = 1 Then
                Sql = "Update 应收款 Set 余额 = 借方 - 贷方 where id =" & i
            Else
                rst.Close
                Sql = "select 余额 from 应收款 where id = " & i - 1
                rst.Open Sql, cnn, 3, 2, 1
                x = rst.Fields("余额")
                Sql = "Update 应收款 Set 余额 = " & x & "+ 借方 - 贷方 where id =" & i
                cnn.Execute Sql
            End If
        Next i
        cnn.Close
        Set rst = Nothing: Set cnn = Nothing
    End Sub
      

  2.   

    经调试,是我代码有误,正确代码应写为:Sql = "Select 余额 From 应收款 where 日期 <  #" & TDate & "#  And ID in (select  max(Id) from 应收款 group by 日期)"Rst.Open Sql, Cnn, 1, 3
    For i = 1 To Rst.RecordCount
        If Rst.Fields(0) <> "" Then
           Me.TextBox1.Value = Rst.Fields(0)
        End If
            Rst.MoveNext
    Next iRst.Close: Cnn.Close
    Set Rst = Nothing: Set Cnn = Nothing另外: 请问结帖方式如何操作?这是我第1次在CSDN结帖,我查看了论坛帮助,为何不能给您得分50分
    (提示: 只有未结和0分帖子才可以给分),但我并未结帖啊!我之前发过20分的帖,无人解(自己解决),未结帖.
    已回帖请版主结帖.是否与此有关?我再试一试.