By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,398 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,398 IT Pros & Developers. It's quick & easy.

How to use the DSum Method with multiple Criterias

P: 1
I have a table with multiple records and I need to sum the Total Amount [CostoTotal] when the type of record [TipoMovimiento] is one field in my form and the Code of the product is another filed in my form.

This is an inventory table and my code is below

Private Sub Command0_Click()
Dim Test As Currency
Test = DSum("[CostoTotal]", "tblInventarioMovimientos", "[TipoMovimiento] <> [Text2] AND [Codigo] = " & [Text1])

End Sub
Mar 31 '10 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 931
The way that you wrote the criteria in the DSum function isn't going to work because [Text2] is included inside the string rather than being concatenated with it. Try this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2. Dim Test As Currency
  3. Test = DSum("[CostoTotal]", "tblInventarioMovimientos", "[TipoMovimiento] <> " & [Text2] & " AND [Codigo] = " & [Text1])
  4. End Sub 

This will work if the comparisons involve numbers. But if [Text1] and [Text2] are string types then this has to be modified slightly as

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2. Dim Test As Currency
  3. Test = DSum("[CostoTotal]", "tblInventarioMovimientos", "[TipoMovimiento] <> '" & [Text2] & "' AND [Codigo] = '" & [Text1] & "'")
  4. End Sub 
Mar 31 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.