![](http://tiebapic.baidu.com/forum/w%3D580/sign=9c17c53e1a6034a829e2b889fb1349d9/35b091dda144ad345cb0cbb896a20cf431ad8511.jpg?tbpicau=2024-09-12-05_8b7e5a0887bdc81328f66d25bdae914e)
如图,前3列为表中数据,第4列(红色数据)为想通过函数求和数据.
尝试做的,但不好用!
Function Total(Code As String, DatF As Date, Xj As Single, n As Integer) As Double
Dim rs As DAO.Recordset
Dim Sql As String
Dim Total As Double
Dim Count As Integer
Total = 0
Count = 0
Sql = "SELECT Top " & n + 1 & " Dat,Dm,Xj FROM usysA WHERE Dm= '" & Code & "' ORDER BY Dat DESC"
Set rs = CurrentDb.OpenRecordset(Sql)
Do While Not rs.EOF
Total = Total + rs.Fields(1)
Count = Count + 1
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Function