Results 1 to 15 of 15
  1. #1
    ebolbol is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2023
    Posts
    15

    Access calculated field expression (need math function)

    Hi folks, hope you're all well.

    I have scoured the internet in search of what Access' equivalent is to Excel's =MIN function in a formula. NOTHING.

    I'm trying to calculate work hours in an Access table with calculated fields to calculate Regular time, over time, and double time respectively. As you can see, the Excel formulas below make it easy to do this.

    I am so close with Access, but I can't seem to find the =MIN function, which in Excel, returns the smallest number in a set of values or formula.


    Time In Time Out Less Meal
    6:00 AM 8:30 PM 0.50





    EXCEL (correct)

    Regular time results = 8
    =MIN(((TimeOut-TimeIn)*24)-LessMeal,8)


    Over time results = 4


    =MIN((((TimeOut-TimeIn)*24)-LessMeal)-RegHrs,4)


    Double time = 2
    =(((TimeOut-TimeIn)*24)-LessMeal)-RegHrs-OTHrs





    ACCESS (not correct)

    Regular time results = 14
    ((([TimeOut]-[TimeIn])*24)-[LessMeal],8)


    and since OT and DT are calculated based on RegHrs it affects those hours too. Any help would be appreciated.

    Ed


  2. #2
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,821
    Hi
    The following example calculates the average discount amount of all orders in the Northwind.mdb database. It multiplies the values in the UnitPrice and Discount fields to determine the discount amount of each order and then calculates the average. You can use this expression in an SQL statement in Visual Basic code:
    SELECT Avg(UnitPrice * Discount) AS [Average Discount] FROM [Order Details];

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I would probably use an IIF() function.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Access Min() is an aggregate function. If you need to compare values in same record and take the lowest, you have to build your own. An IIf() expression might be adequate, otherwise write a VBA custom function that can be called from query or textbox.
    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
    ebolbol is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2023
    Posts
    15
    Quote Originally Posted by mike60smart View Post
    Hi
    The following example calculates the average discount amount of all orders in the Northwind.mdb database. It multiplies the values in the UnitPrice and Discount fields to determine the discount amount of each order and then calculates the average. You can use this expression in an SQL statement in Visual Basic code:
    SELECT Avg(UnitPrice * Discount) AS [Average Discount] FROM [Order Details];
    Thank you for your reply. I'm really confused what average discount amount has to do with time calculation in a calculated field in a table and why I have to use VB code to accomplish this.

    There has to be a simple way to do this.

  6. #6
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,821
    The example is for calculating an Avg
    Replace with Avg with Min + Your calculations and see what you get.

  7. #7
    ebolbol is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2023
    Posts
    15
    Quote Originally Posted by Welshgasman View Post
    I would probably use an IIF() function.


    the syntax for what you suggested is
    Code:
    IIf(expr, truepart, falsepart)

    How would I apply that to
    Code:
    ((([TimeOut]-[TimeIn])*24)-[LessMeal],8)
    ?

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by ebolbol View Post
    the syntax for what you suggested is
    Code:
    IIf(expr, truepart, falsepart)

    How would I apply that to
    Code:
    ((([TimeOut]-[TimeIn])*24)-[LessMeal],8)
    ?
    How does Min() do it?

    Code:
    IIF(((TimeOut-TimeIn)*24)-LessMeal < 8,((TimeOut-TimeIn)*24)-LessMeal,8)
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    ebolbol is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2023
    Posts
    15

    Access calculated field expression (need math function)

    Quote Originally Posted by Welshgasman View Post
    How does Min() do it?

    Code:
    IIF(((TimeOut-TimeIn)*24)-LessMeal < 8,((TimeOut-TimeIn)*24)-LessMeal,8)

    Dear Welshgasman,


    I'm not at your level, so forgive the elementary questions. Having said that, I believe you just solved my problem and I'm very grateful to you for that! Two thumbs up to you!!!


    Best regards,

    Ed

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You could even create your own function, perhaps called MyMin and pass the two values and return the lowest with a simple if, if that is easier for you? That would emulate the excel min function.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by Welshgasman View Post
    You could even create your own function, perhaps called MyMin and pass the two values and return the lowest with a simple if, if that is easier for you? That would emulate the excel min function.
    In fact Vlad (@Gicu) has done it for you with Min2, so you can have exactly as per the Excel function.
    Check out Utility Functions at http://forestbyte.com/vba-code-samples/

    There is that function and a lot more.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    ebolbol is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2023
    Posts
    15
    Quote Originally Posted by Welshgasman View Post
    In fact Vlad (@Gicu) has done it for you with Min2, so you can have exactly as per the Excel function.
    Check out Utility Functions at http://forestbyte.com/vba-code-samples/

    There is that function and a lot more.
    Excellent! Welshgasman.... can't thank you enough!

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by ebolbol View Post
    Excellent! Welshgasman.... can't thank you enough!
    Thank @Gicu,
    He created them.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #14
    ebolbol is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2023
    Posts
    15
    Quote Originally Posted by Welshgasman View Post
    Thank @Gicu,
    He created them.

    Thank you @Gicu!

  15. #15
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    You're very welcome !
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Calculated field using Expression Builder
    By Pyeman in forum Access
    Replies: 2
    Last Post: 10-25-2021, 12:14 PM
  2. Replies: 3
    Last Post: 06-05-2015, 11:44 AM
  3. Replies: 1
    Last Post: 05-09-2013, 07:54 PM
  4. Replies: 3
    Last Post: 04-18-2012, 07:10 AM
  5. Math error in calculated field
    By nashgas in forum Queries
    Replies: 2
    Last Post: 04-03-2012, 11:49 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