DSum with multiple criteria

accesconfused

New member
Local time
Today, 07:53
Joined
Sep 28, 2015
Messages
4
Hi all,
I am attempting to create a DSum argument with a year criterion(Yr =2016) (Yr is a text field in my table) and an ID criterion (Me.ID)(ID is a number field in my table). I can successfully create functions that are either criteria independently, but when I try to combine the criteria with an "AND" I receive a type mismatch error. If each criterion argument works well independently, what must I do differently to have both criteria present in one DSum function? Please see my code excerpt below:


IndvTotHours = DSum("Hours", "tbl_HoursLog", "Vol_ID = " & Me.ID) ' works correctly

YrHours = DSum("Hours", "tbl_HoursLog", "Yr='2016'") 'works correctly

IndvYrHours = DSum("Hours", "tbl_HoursLog", "Vol_ID =" & Me.ID And "Yr='2016'") ' type mismatch
 
IndvYrHours = DSum("Hours", "tbl_HoursLog", "Vol_ID =" & Me.ID & " And Yr='2016'")
 
worked like a charm. I did not realize the AND also had to be enclosed in the quotations separating out the second criterion argument. Thank you so much!
 
worked like a charm. I did not realize the AND also had to be enclosed in the quotations separating out the second criterion argument. Thank you so much!

Think of it this way: the third parameter is basically the WHERE clause of a SQL statement, and thus must resolve to a single string.
 

Users who are viewing this thread

Back
Top Bottom