Results 1 to 5 of 5
  1. #1
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187

    Using DMax() in VBA on a query and getting incorrect result


    I have a query that sums data from a table. I am then using DMax() from VBA on this query but it is not giving me the max value from the results of the query. When I run this query in Access and I review the resulting values, it shows me a number in the hundreds and many other higher numbers, while DMax() returns 9. Does DMax() find the max of the individual values that were used to calculate the sum? I would think it would give me the max value in the result of the query. Please help.

    Thank you

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Should pull the max value of the aggregate calcs.

    Post the SQL statement of the query and the VBA code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187
    Quote Originally Posted by June7 View Post
    Should pull the max value of the aggregate calcs.

    Post the SQL statement of the query and the VBA code.
    Will post shortly. I cannot post the tables that feed it, is that ok?

  4. #4
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187
    Ok, so I fixed it, but still not sure why I need a work around in this scenario. I noticed in the query results, this column was left aligned which IIRC indicates its text and not numeric so it was probably giving me the max ascii value? The fields in this table are all set to numbers so it should be giving me numbers in the query? Doing a + 0 gives me what I want.

    Sum(MonthSum) + 0 As AvgNum

    Pay not attention to the 'AvgNum' naming convention. This is shared with things that use averages.
    Code:
    Public Function RangeQueryAttendanceSum(ByVal strField As String) As String
    RangeQueryAttendanceSum = _
        "SELECT tblAgents.ID As AgentID, Sum(MonthSum) + 0 As AvgNum, " & lstMonths.ItemsSelected.Count & " As NumRecords" & vbCrLf & _
        "FROM tblAgents" & vbCrLf & _
            "LEFT JOIN (" & vbCrLf & _
                "SELECT tblAttendance.AgentID As AgentID, Sum(" & strField & ") As MonthSum, Count(*) As NumErrorRecords" & vbCrLf & _
                "FROM tblAttendance" & vbCrLf & _
                "WHERE tblAttendance.RepType=" & cmbRepType & " AND " & BuildMonthCondition() & vbCrLf & _
                "GROUP BY tblAttendance.AgentID" & vbCrLf & _
            ") As GetAttendance" & vbCrLf & _
            "ON tblAgents.ID=GetAttendance.AgentID" & vbCrLf & _
        "WHERE tblAgents.RepType=" & cmbRepType & vbCrLf & _
        "GROUP BY tblAgents.ID;"
    End Function

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    I also do not understand why the data was returning as text.

    Regardless, the max returned should have been the actual text, not an ASCII.

    However, as long as it works, guess that is what's important.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 2
    Last Post: 02-05-2015, 11:02 PM
  2. DMAx Question for Report - DMax <= Dtae
    By crimedog in forum Access
    Replies: 8
    Last Post: 12-29-2014, 09:31 PM
  3. Query DMax
    By domivax in forum Queries
    Replies: 3
    Last Post: 08-15-2014, 09:40 AM
  4. Use DMAX in query
    By sarahjh in forum Queries
    Replies: 1
    Last Post: 02-13-2013, 02:49 PM
  5. Replies: 6
    Last Post: 07-24-2012, 03:02 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