Results 1 to 13 of 13
  1. #1
    bsegge is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19

    Question Adding Criteria in MsgBox DSum sub

    Hello,



    I am attempting to write code that sums the values in a volume field. It is as follows:

    Private Sub Command128_Click()

    MsgBox DSum("Volume", "Volume")

    End Sub

    It works just fine, however, I want to add criteria that allows me to sum the volume of fields with a similar separate variable (codename).

    In other words, instead of summing the entire "Volume" field, I want it to sum the volume of all rows with a certain codename that I input.

    I attempted to write it this way:


    Private Sub Command128_Click()

    MsgBox DSum("Volume", "Volume", WHERE [CodeName] LIKE "*' & Me.txtVolume & '*")

    End Sub

    But it didn't work. txtVolume is a textbox object I added to the form object I am working in.

    Any help is appreciated!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    See if this helps:

    DLookup Usage Samples
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    bsegge is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19
    So I was able to figure out the code!

    Here is what I have:

    Private Sub cmdVolumeCount_Click()
    Dim curVol As Long
    Dim txtVolume As String

    If IsNull(txtVolume = Me.txtVolume) Then
    curVol = DSum("Volume", "Volume")
    Else
    txtVolume = Me.txtVolume
    curVol = DSum("Volume", "Volume", "[CodeName] LIKE '*" & txtVolume & "*'")
    End If
    MsgBox curVol
    End Sub

    It works great if the text box (txtVolume) is empty, or contains a string of text that matches a [CodeName] in the respective field.

    However, if I enter a value into the txtVolume object that is not in the [CodeName] field, it gives a null error report.

    Does anyone have any further suggestions as to how to fix this issue?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Where to start? Given

    If IsNull(txtVolume = Me.txtVolume) Then

    does the first refer to the variable or the control? In other words, don't name a variable the same as a control. You'll confuse Access and yourself. On the same line, does that ever evaluate to True? I wouldn't expect so, as the equation would either be true or false, thus never Null. Could be wrong though. Your error probably comes from the fact that a Currency variable can't hold Null, which is probably what comes from the function. Either declare the variable as Variant or wrap the function in the Nz() function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Looks like I was wrong about the test:

    ?null=4
    Null
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    bsegge is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19
    The txtVolume variable is whatever data that is input into the txtVolume object (text box) that is within the form object I'm working with.

    It is True whenever the entry into the txtVolume object matches a value in the [CodeName] field.

    My code works for when the txtVolume object is both completely blank, and when the data entered matches the [CodeName] field.

    The only time it doesn't work is when the data entered does not match the [CodeName] field

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you miss:

    Quote Originally Posted by pbaldy View Post
    Your error probably comes from the fact that a Currency variable can't hold Null, which is probably what comes from the function. Either declare the variable as Variant or wrap the function in the Nz() function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    bsegge is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19
    There are no currency data types in the table.

    The fields are [CodeName] - Short Text, [VolumeDate] - Date/Time, and [Volume] - Number.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sorry, I didn't notice that you used cur to prefix a Long, but my comment applies to Long as well:

    Dim curVol As Long
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    bsegge is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19
    I apologize, I must be confused.

    I used the prefix cur to mean current. As in, current volume.

    Does using cur make it a currency?

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No, many of us prefix variables with something that represents the data type, so I assumed you had declared it as Currency. In any case, the only data type that can hold a Null is Variant, so my original suggestion remains valid:

    Either declare the variable as Variant or wrap the function in the Nz() function.

    like:

    curVol = Nz(DSum("Volume", "Volume", "[CodeName] LIKE '*" & txtVolume & "*'"), 0)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    bsegge is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19
    Thank you SO much for having the patience to work through that with me.

    I implemented the Nz() suggestion and it worked perfectly.

    I appreciate your guidance and help!

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help me ! using Dsum with criteria day
    By dahota in forum Forms
    Replies: 4
    Last Post: 06-25-2015, 11:05 AM
  2. Dsum criteria
    By Manuel Ferreira in forum Access
    Replies: 4
    Last Post: 09-12-2014, 09:54 AM
  3. dsum with criteria Not like 38,39,40,41
    By sdel_nevo in forum Programming
    Replies: 3
    Last Post: 06-13-2013, 03:23 PM
  4. Dsum criteria
    By bdaniel in forum Queries
    Replies: 4
    Last Post: 11-27-2011, 03:41 PM
  5. DSum in Query - Adding groups
    By Huddle in forum Queries
    Replies: 1
    Last Post: 02-04-2011, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums