账户余额

时间:2012-05-17 04:02:17

标签: vba

如何使用vb 6.0计算帐户余额并访问dbase 例如:

January 2000   2500.00
Febuary 2000   2300.00

当用户选择2000年1月时,它将显示2500.00,当用户选择2000年2月时,它将显示4800.00。

希望有人能帮助我 感谢

Dim TOTAL, FMACT As Double 
TOTAL = 0 
Set rs2 = New ADODB.Recordset 

With rs2 
    .Open "SELECT * FROM SW WHERE dtaYear=" & cmbYear.Text & " ", Con, 1, 2 
    Let FMACT = rs2![SWACTFM] 

    If .RecordCount > 0 Then 
        TOTAL = TOTAL + FMACT 
        .MoveFirst 

        Do While rs2.EOF = True 
            .MoveNext 
            ExcelWS.Cells(6, 7) = TOTAL 
        Loop 
    End If 

   .Close 
   Set rs2 = Nothing 
End With

1 个答案:

答案 0 :(得分:0)

您的代码存在问题

  1. 由于Total超出Do While循环,因此不会累积
  2. .MoveNext应在ExcelWS.Cells(6,7) = TOTAL之后执行,否则您将错过第一条记录
  3. 我不确定你想要什么,如果你想每次只从数据库中获取一条记录,你似乎不需要while循环。你可以试试这个。

    Private Total As Double
    Total = 0
    
    Private Sub Cal_Total()
        Set rs2 = New ADODB.Recordset 
    
        With rs2 
            .Open "SELECT * FROM SW WHERE dtaYear=" & cmbYear.Text & " ", Con, 1, 2 
    
            .MoveFirst
            If .RecordCount > 0 Then 
                Do While rs2.EOF = False 
                    Total = Total + rs2![SWACTFM]
                    .MoveNext
                Loop
    
                ExcelWS.Cells(6,7) = Total
            End If 
    
           .Close 
           Set rs2 = Nothing 
        End With
    End Sub