Results 1 to 5 of 5
  1. #1
    kagoodwin13 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    181

    Max of Query Record

    I am trying to take the max across a query record. I've tried to use Max in the Expression Builder of my query, only to find out that Max isn't a recognized function in this section, even though it is in other parts of Access (rage!). I might have possibly set up my database wrong, but now I'm stuck with it, and trying to work around it.

    My query results look like this:

    PART_NUMBER JAN14 FEB14 MAR14 APR14
    1234567890 1000 2000 1000 2000
    1234567891 2000 3500 4000 3000

    I would like to get the max across each row:
    PART_NUMBER MAX
    1234567890 2000
    1234567891 4000



    I tried Max([JAN14],[FEB14],[MAR14],[APR14]) but that didn't work.
    Any ideas?

  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,771
    Requires a custom function to calculate row-level statistics.

    Here are examples

    http://support.microsoft.com/kb/209839

    http://allenbrowne.com/func-09.html
    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
    kagoodwin13 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    181
    Great, this worked! However, the output of the MaxOfList function seems to be text.

    Using the query above, I have one query that evaluates for the Max, MAX: MaxOfList([JAN14],[FEB14],[MAR14]), which gives the desired output below:
    QUERY1
    PART_NUMBER MAX
    1234567890 2000
    1234567891 4000

    However I would like to evaluate the [MAX] field and give a judgment: JUDGMENT: Switch([MAX]>3500,"RED",[MAX]<3500,"GREEN")
    But I keep getting this as a result:
    QUERY2
    PART_NUMBER MAX JUDGMENT Should be
    1234567890 2000 #Error GREEN
    1234567891 4000 #Error RED

    Same result if I use an IIf statement like JUDGMENT: IIf([MAX]>3500,"RED","GREEN")

    From my investigation, the output of QUERY1.[MAX] is indented to the left, like a text field, and I can't change the field properties of it to number or percentage.

    EDIT: I made QUERY1 into a Make Table query, which saves into a table that re-defines [MAX] as a number. Thanks for your help June7, you've helped me a lot with numerous projects.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Should avoid reserved words in naming convention. Max is a reserved word.

    The value returned is text because the function is declared as variant.

    What if the value is exactly 3500? It won't be captured by either condition.

    Shouldn't have to resort to make table. Convert the value back to number with Val() or CInt() or CDbl() or CLng() function.
    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.

  5. #5
    kagoodwin13 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    181
    Val() worked. Thanks again!

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

Similar Threads

  1. Replies: 32
    Last Post: 05-23-2013, 04:16 PM
  2. Query critieria on Sum of Record
    By ice051505 in forum Queries
    Replies: 2
    Last Post: 03-08-2013, 05:20 PM
  3. Use VBA to edit record or create new record in a query
    By ryantam626 in forum Programming
    Replies: 11
    Last Post: 08-09-2012, 02:37 AM
  4. Relating record in form with a record in query?
    By theorythree in forum Access
    Replies: 5
    Last Post: 12-05-2011, 10:05 PM
  5. Replies: 5
    Last Post: 06-29-2010, 01:24 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