Results 1 to 9 of 9
  1. #1
    stryder09 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    85

    Another calculation problem

    Okay I have 5 fields that I am looking at each field was assigned a value of either 0,2,5 or 20 depending on the criteria it meets. I now have a summary that looks at these values and I need it to only take the highest one. How do I do that?



    ex. [field1]=0, [field2]=5, [field3]=2, [field4]=20 and [field5]= 2. I need the summary to take the highest and apply it to [fieldsummary]. In this example [field4]=20 so [fieldsummary] needs to also =20.

    I hope I have given everything needed.

    Thanks

    Brad

  2. #2
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,536
    Here is a solution. Not the prettiest, but if I understand your situation, it will work.
    Code:
    SELECT Table1.ID, Table1.Field1, Table1.Field2, Table1.Field3, Table1.Field4, Table1.Field5,
     IIf([field1]>[field2] And [field1]>[field3] And [field1]>[field4] And [field1]>5,[field1],
    IIf([field2]>[field1] And [field2]>[field3] And [field2]>[field4] And [field2]>[field5],[field2],
    IIf([field3]>[field1] And [field3]>[field2] And [field3]>[field4] And [field3]>[field5],[field3],
    IIf([field4]>[field1] And [field4]>[field2] And [field4]>[field3] And [field4]>[field5],[field4],[field5])))) AS Fieldsummary
    FROM Table1;
    alan

  3. #3
    stryder09 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    85
    So I will have to write this in code then?

  4. #4
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,536
    Or cut and paste it. Look at the attachment also.
    If you need to change the field names, cut and paste into a word document and do a find and replace. Then cut and paste into your query.

    Alan

  5. #5
    stryder09 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    85
    Thank you it worked perfectly for me.

    Brad

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Perhaps simpler and more flexible:

    Minimum or Maximum Value Across Fields of Record
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    stryder09 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    85
    Ok I kind of have this working with the equation below. My only problem now is if any of the 5 fields has the same number it will not give me anything. Any ideas?

    =IIf([pH1Demerits]>[pH2Demerits] And [pH1Demerits]>[pH3Demerits] And [pH1Demerits]>[pH4Demerits] And [pH1Demerits]>[pH5Demerits],[pH1Demerits],IIf([pH2Demerits]>[pH1Demerits] And [pH2Demerits]>[pH3Demerits] And [pH2Demerits]>[pH4Demerits] And [pH2Demerits]>[pH5Demerits],[pH2Demerits],IIf([pH3Demerits]>[pH1Demerits] And [pH3Demerits]>[pH2Demerits] And [pH3Demerits]>[pH4Demerits] And [pH3Demerits]>[pH5Demerits],[pH3Demerits],IIf([pH4Demerits]>[pH1Demerits] And [pH4Demerits]>[pH2Demerits] And [pH4Demerits]>[pH3Demerits] And [pH4Demerits]>[pH5Demerits],[pH4Demerits],IIf([pH5Demerits]>[pH1Demerits] And [pH5Demerits]>[pH2Demerits] And [pH5Demerits]>[pH3Demerits] And [pH5Demerits]>[pH4Demerits],[pH5Demerits])))))

    Thanks

    Brad

  8. #8
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    When I tried the formula, I kept getting a compile error (in VBA). The last IIF() was missing the "FalsePart" if the function.

    This is what finally worked:

    Code:
    = IIf([pH1Demerits] > [pH2Demerits] And [pH1Demerits] > [pH3Demerits] And [pH1Demerits] > [pH4Demerits] And [pH1Demerits] > [pH5Demerits], [pH1Demerits], IIf([pH2Demerits] > [pH1Demerits] And [pH2Demerits] > [pH3Demerits] And [pH2Demerits] > [pH4Demerits] And [pH2Demerits] > [pH5Demerits], [pH2Demerits], IIf([pH3Demerits] > [pH1Demerits] And [pH3Demerits] > [pH2Demerits] And [pH3Demerits] > [pH4Demerits] And [pH3Demerits] > [pH5Demerits], [pH3Demerits], IIf([pH4Demerits] > [pH1Demerits] And [pH4Demerits] > [pH2Demerits] And [pH4Demerits] > [pH3Demerits] And [pH4Demerits] > [pH5Demerits], [pH4Demerits], IIf([pH5Demerits] > [pH1Demerits] And [pH5Demerits] > [pH2Demerits] And [pH5Demerits] > [pH3Demerits] And [pH5Demerits] > [pH4Demerits], [pH5Demerits], 1111)))))
    I also wrote a UDF: (order of the arguments doesn't matter)

    Code:
    Public Function MaxDemerit(p1 As Integer, p2 As Integer, p3 As Integer, p4 As Integer, p5 As Integer) As Integer
       Dim MaxValue As Integer
    
       MaxValue = 0
    
       If p1 > MaxValue Then
          MaxValue = p1
       End If
       If p2 > MaxValue Then
          MaxValue = p2
       End If
       If p3 > MaxValue Then
          MaxValue = p3
       End If
       If p4 > MaxValue Then
          MaxValue = p4
       End If
       If p5 > MaxValue Then
          MaxValue = p5
       End If
    
       MaxDemerit = MaxValue
    
    End Function
    To call it, use:

    for a textbox on a form:
    Code:
    = MaxDemerit(pH1Demerits, pH2Demerits, pH3Demerits, pH4Demerits, pH5Demerits)
    in a query:

    Code:
    Fieldsummary: MaxDemerit(pH1Demerits, pH2Demerits, pH3Demerits, pH4Demerits, pH5Demerits)

  9. #9
    stryder09 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    85
    So when I use the formula you gave me Steve it returns the value 1111 if I have two or more values the same? It still will not just give me the highest value. Can I not just create a formula for the form page or does this have to be done in Visual Basic? I am not at all fluent in Visual BAsic.

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

Similar Threads

  1. another calculation problem
    By stryder09 in forum Access
    Replies: 15
    Last Post: 02-11-2011, 04:53 PM
  2. Need help with calculation please
    By Gabriel984 in forum Forms
    Replies: 6
    Last Post: 09-09-2010, 12:06 PM
  3. Calculation problem
    By cometdragon in forum Queries
    Replies: 2
    Last Post: 07-28-2010, 08:54 PM
  4. Calculation Help
    By ErnieS in forum Access
    Replies: 13
    Last Post: 07-08-2010, 08:35 AM
  5. Calculation
    By thestclair in forum Reports
    Replies: 1
    Last Post: 04-29-2006, 11:03 AM

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