Results 1 to 9 of 9
  1. #1
    GEPC is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    5

    How to pick the maximum value from variables in code

    I have four variables DHVN, DHVE, DHVS, and DHVW that are calculated in my code. Next, I need to do another calculation that involves picking the highest of these four variables. Can someone tell me how to do this?

  2. #2
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    the most direct way is compare them one by one then pick the max one.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    These would work with variables as well as fields:

    http://support.microsoft.com/default...b;en-us;209857
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    GEPC is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    5
    Thanks pbaldy but I cannot view the link you sent me.

  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,518
    Not sure why not; it's a normal Microsoft KB page. Here's the function from it:

    Code:
    Function Maximum(ParamArray FieldArray() As Variant)
       ' Declare the two local variables.
       Dim I As Integer
       Dim currentVal As Variant
    
       ' Set the variable currentVal equal to the array of values.
       currentVal = FieldArray(0)
    
       ' Cycle through each value from the row to find the largest.
    
       For I = 0 To UBound(FieldArray)
          If FieldArray(I) > currentVal Then
             currentVal = FieldArray(I)
          End If
       Next I
    
       ' Return the maximum value found.
       Maximum = currentVal
    
    End Function
    Called like:

    Maximum(DHVN, DHVE, DHVS, DHVW)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    GEPC is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    5
    After I call the function i.e.

    Call Maximum(DHVN, DHVS, DHVE, DHVW)

    I am using the variable created in the function, Maximum, for the next calculation. It is giving me an error reading:

    "The value you entered isn't valid for this field.
    For example, you may have entered text in a numeric field ot a number that is larger than the FieldSize setting permits."

  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,518
    It would help to see your actual code, and what the variables might contain. I'd expect:

    SomeNewVariable = Maximum(DHVN, DHVS, DHVE, DHVW)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    GEPC is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    5
    My code is the folowing:

    ...
    S = DHVN * F * O
    T = DHVE * G * P
    U = DHVS * H * Q
    V = DHVW * II * R
    W = Abs(S + T + U + V)

    If E = N Then
    XX = "Bad Parity"

    Dim Max As Double

    Max = Maximum(DHVN, DHVS, DHVE, DHVW)

    If W < (Max / 2) Then
    XX = "Factors are Valid"
    End If

    If XX = "" Then
    XX = "Factors are Valid"
    End If

  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,518
    What line throws the error? That code shouldn't even compile, as this bit:

    If E = N Then
    XX = "Bad Parity"

    doesn't have the required "End If".
    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. program that pick a random winner
    By georges.choueiry in forum Access
    Replies: 4
    Last Post: 09-29-2010, 01:05 AM
  2. How to pick the n-th row from a query
    By Johnny C in forum Queries
    Replies: 2
    Last Post: 07-27-2010, 05:14 AM
  3. Replies: 2
    Last Post: 06-22-2010, 07:09 AM
  4. How do I return the maximum value
    By kam in forum Queries
    Replies: 2
    Last Post: 03-17-2010, 07:38 AM
  5. Find Maximum Value of a Field and Add 1
    By jhillbrown in forum Forms
    Replies: 3
    Last Post: 03-11-2010, 11:42 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