How to use the DSum Method with multiple Criterias

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • llariosc
    New Member
    • Mar 2010
    • 1

    How to use the DSum Method with multiple Criterias

    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]", "tblInventarioM ovimientos", "[TipoMovimiento] <> [Text2] AND [Codigo] = " & [Text1])

    End Sub
  • patjones
    Recognized Expert Contributor
    • Jun 2007
    • 931

    #2
    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:

    Code:
    Private Sub Command0_Click()
    Dim Test As Currency
    Test = DSum("[CostoTotal]", "tblInventarioMovimientos", "[TipoMovimiento] <> " & [Text2] & " AND [Codigo] = " & [Text1])
    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


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

    Comment

    Working...