If you want to use the Dsum in VBA.
Download Sample Database
Here is the code-
Sub use_dsum()
Dim sumfield As String
Dim tblname As String
' if you want to find the sum of sales for rep name = a
sumfield = "sales"
tblname = "sales_detail"
MsgBox DSum(sumfield, tblname, "[Rep Name] = 'a'"), vbOKOnly
' with query
Dim SQLresult As String
Dim rs As Recordset
SQLresult = "SELECT Sum([sales_detail].sales) AS Filtersales FROM [sales_detail] WHERE ([sales_detail].[rep name]='a')"
Set rs = CurrentDb.OpenRecordset(SQLresult)
MsgBox rs(0), vbInformation
End Sub
No comments:
Post a Comment