會(huì)員登錄 - 用戶注冊(cè) - 網(wǎng)站地圖 Office中國(office-cn.net),專業(yè)Office論壇
當(dāng)前位置:主頁 > 技巧 > Access技巧 > 查詢視圖 > 正文

如何實(shí)現(xiàn)動(dòng)態(tài)查詢余額

時(shí)間:2005-02-06 00:07 來源:Office中國/Access中國 作者:lwwvb『文… 閱讀:

----------------------------------------------------------------
表:
----------------------------------------------------------------
id 自動(dòng)增加 長整
in 貨幣
out 貨幣

----------------------------------------------------------------
代碼:
----------------------------------------------------------------

Option Compare Database
Option Explicit


Public gcurLastBalance As Currency               '上次計(jì)算的余額
Public glngLastID As Long                        '上次的 ID

'查詢余額

'Version 1.0
'2003-05-06-15-15
'By Roadbeg

'要求以 Id 作為判斷依據(jù).(長整型)

Public Function GetBalance(ID As Long) As Currency
On Error GoTo Doerr

    Dim curIn As Currency, curOut As Currency
    Dim curRe As Currency
    
    If glngLastID <> 0 Then
        If ID > glngLastID Then
            curIn = Nz(DSum("[IN]", "TEST", "ID <=" & str(ID) & " and ID>" & str(glngLastID)))
            curOut = Nz(DSum("[OUT]", "TEST", "ID <=" & str(ID) & " and ID>" & str(glngLastID)))
            curRe = gcurLastBalance + curIn - curOut
        ElseIf ID < glngLastID Then
            curIn = Nz(DSum("[IN]", "TEST", "ID >" & str(ID) & " and ID<=" & str(glngLastID)))
            curOut = Nz(DSum("[OUT]", "TEST", "ID >" & str(ID) & " and ID<=" & str(glngLastID)))
            curRe = gcurLastBalance - curIn + curOut
        ElseIf ID = glngLastID Then
            curRe = gcurLastBalance
        End If
    Else
        curIn = DSum("[IN]", "TEST", "ID<=" & str(ID))
        curOut = DSum("[OUT]", "TEST", "ID<=" & str(ID))
        curRe = curIn - curOut
    End If
    
'    Debug.Print ID
    glngLastID = ID
    gcurLastBalance = curRe
    
    GetBalance = curRe
Doerr:
End Function

'改變了 test 表的記錄值后,請(qǐng)調(diào)用此函數(shù)以強(qiáng)制 GetBalance 函數(shù)刷新.

Public Sub ResetBalance()
    gcurLastBalance = 0
    glngLastID = 0
End Sub

'這是 lwwvb 版主的函數(shù),我將它改為以 id 作為計(jì)算依據(jù)了,原理不變.

Public Function f(d As Long) As Currency
  Dim a As Currency
  Dim b As Currency
  
  a = Nz(DSum("[in]", "test", "id <=" & str(d)))
  b = Nz(DSum("[out]", "test", "id <=" & str(d)))
  
  f = a - b
  
End Function

'請(qǐng)使用以下函數(shù)產(chǎn)生 600000 條隨機(jī)記錄,以檢驗(yàn)函數(shù)在記錄較多時(shí)的效果.

Public Sub 產(chǎn)生隨機(jī)記錄()
    Dim rst As DAO.Recordset
    Dim i As Long
    
    Debug.Print Now()
    Set rst = CurrentDb.OpenRecordset("select [in] as dataa,[out] as datab from test")
    For i = 0 To 600000
        rst.AddNew
        rst!dataa = CLng(Rnd() * 100)
        rst!datab = CLng(Rnd() * 100)
        rst.Update
    Next i
    rst.Close
    Debug.Print Now()
End Sub


'一下是一組時(shí)間測(cè)試
Function t2()
Dim c1 As New class1
Dim rs As ADODB.Recordset

c1.Reset
Set rs = CurrentProject.Connection.Execute("SELECT [id], [in], [out], getbalance([id]) AS 余額 FROM test ORDER BY [id];")

Debug.Print c1.Elapsed
Set rs = Nothing
Set c1 = Nothing
End Function

Function t3()
Dim c1 As New class1
Dim rs As ADODB.Recordset

c1.Reset

Set rs = CurrentProject.Connection.Execute("SELECT [id], [in], [out], f([id]) AS 余額 FROM test ORDER BY [id]")

Debug.Print c1.Elapsed
Set rs = Nothing
Set c1 = Nothing
End Function

Function t1()
Dim c1 As New class1
Dim rs As ADODB.Recordset

c1.Reset

Set rs = CurrentProject.Connection.Execute("SELECT [id], [in], [out], (SELECT SUM(b.[in]-b.[out]) AS bb FROM test b WHERE a.[id] <= b.[id]) AS ye FROM test a ORDER BY [id]")


Debug.Print c1.Elapsed
Set rs = Nothing
Set c1 = Nothing
End Function

 

(責(zé)任編輯:admin)

頂一下
(1)
100%
踩一下
(0)
0%
發(fā)表評(píng)論
請(qǐng)自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
評(píng)價(jià):