Results 1 to 15 of 15
  1. #1
    Eranka is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    150

    Access Coding

    Hi Everyone

    I am kinda new to access coding. Appreciate if you could help me on below problem



    I have attached a picture in here. I am calculating the time difference between time_in and Time_out and displaying the difference in Actual_hours field.
    On the next field "Accepted_Hours" filed the "Actual_hours" data should appear and if a time exceed more than 4 hours it should automatically change to 4. How to do this part?

    Ex:

    Time_In Time_Out Actual_Hours Accepeted_Hours
    6:21:00 PM 9:00:00 PM 2:39 2:39 This is fine
    1:07:00 AM 6:00:00AM 4:53 4 4.53 to 4
    Attached Thumbnails Attached Thumbnails 25689.jpg  

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    In a query you can use an IIf() as a calculated field;
    Code:
    AcceptedHours: IIf([ActualHours] > 4 , 4 , [ActualHours])
    If Actual Hours is a calculated field itself (and it should be), then you can't refer to that in the same query, you would need to perform the calculation again within the IIf() statement.

  3. #3
    Eranka is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    150
    Hi

    I tried like

    =IIf(([Time_Out]-[Time_In])>4,4,([Time_Out]-[Time_In]))

    But it gives out put as below in the accepted hours. The 4.53 does not change as 4 automatically.

    Time_In Time_Out Actual_Hours Accepeted_Hours
    6:21:00 PM 9:00:00 PM 2:39 2:39
    1:07:00 AM 6:00:00AM 4:53 4.53



  4. #4
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Can you make a field to display [Time_out]-[Time_In] to see what results you are getting ?

  5. #5
    Eranka is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    150
    [Time_out]-[Time_In] = [Actual_Hours] <- this is the filed that i have created.

    I just want to display the if the
    [Actual_Hours] is > 4 then as 4.

  6. #6
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Sorry I'm being thick - Don't format the output as a time, the value isn't 4, it will be a very small number as date time are stored as decimal numbers so now is actually (Do this in the immediate window)
    ? CDec(now()) 43090.4575347222

    The integer part is the Day , the fractional part is the Time.

    So display CDec([Time_out]-[Time_In]) to see what values you are getting.

    Edit : 4 hours is
    0.166666666664241 so check for that value.

  7. #7
    Eranka is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    150
    Hi

    How did you get 4 = 0.166666666664241 value?

  8. #8
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    I used the immediate window of the VBA editor and typed in
    Code:
    ?CDec( Date() - (Date() + TimeValue("4:00")))
    So to explain what that breaks down as
    ? means Print , Date() is today at 00:00:00 and therefore Date + TimeValue("4:00") is today at 04:00:00 hence four hours.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    As Minty said, dates and times are actually stored as decimal numbers. What you see is a default formatting. Calculating the difference of those time values results in a decimal figure which is a portion of a 24-hour day. To get the result in hours multiply by 24.

    ([Time_out]-[Time_In]) * 24

    However, it is not necessary to repeat the calc. Example:

    SELECT Time_In, Time_out, ([Time_out]-[Time_In]) * 24 AS ActualHours, IIf([ActualHours]>4,4,[ActualHours]) AS Accepted FROM tablename;

    If the time span crosses midnight, this will not work because need the date part to properly calc elapsed time.
    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.

  10. #10
    Eranka is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    150
    Hi All

    Can you help to combine these two codes.

    IIf(([Session_ID]="Evening") And (([Time_Out]-[Time_In])*24)>=3,0.125,(([Time_Out]-[Time_In])))

    IIf((([Time_Out]-[Time_In])*24)>=4,0.166666666,(([Time_Out]-[Time_In])))

  11. #11
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Maybe try this: (not sure if I have the parentheses correct)
    Code:
    IIF(([Session_ID]="Evening", IIF((([Time_Out]-[Time_In])*24)>=4, 0.166666666, IIF((([Time_Out]-[Time_In])*24)>=3,0.125,([Time_Out]-[Time_In]))),0)
    -----------------------------------------------------------------------------------------------------------------------------

    What I did to get this formula:

    So you have 3 conditions:
    1) [Session_ID]="Evening",
    2) (([Time_Out]-[Time_In])*24)>=4
    3) (([Time_Out]-[Time_In])*24)>=3

    Now create the formula
    First step:
    1st condition syntax: IIF(condition, True, False). If [Session_ID] is not equal to "Evening"), return zero.
    IIF([Session_ID]="Evening", True, 0)


    Second step:
    Then add the 2nd condition in the TRUE part
    IIF([Session_ID]="Evening", IIF(condition, True, False), 0)

    Add the 2nd condition and the TRUE return value
    IIF([Session_ID]="Evening", IIF((([Time_Out]-[Time_In])*24)>=4, 0.166666666, False), 0)


    Third step:
    Since it wasn't >=4, add the next condition
    IIF([Session_ID]="Evening", IIF((([Time_Out]-[Time_In])*24)>=4, 0.166666666, IIF((([Time_Out]-[Time_In])*24)>=3, 0.125, False)), 0)

    Fourth step:
    Then the FALSE return value
    Code:
    IIF([Session_ID]="Evening", IIF((([Time_Out]-[Time_In])*24)>=4, 0.166666666, IIF((([Time_Out]-[Time_In])*24)>=3, 0.125, ([Time_Out]-[Time_In]))), 0)


    So I start out with
    IIF(condition,TRUE,FALSE) <--first test
    then add another test
    IIF(condition, IIF(condition,TRUE,FALSE),FALSE) <--second test
    then add the third test
    IIF(condition, IIF(condition,TRUE, IIF(condition,TRUE,FALSE)),FALSE) <--third (final) test





    Simple...No??

  12. #12
    Eranka is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    150
    Hi

    Thank you for the above solution.

    IIf(([Session_ID]="Evening") And (([Time_Out]-[Time_In])*24)>=3,0.125,IIf((([Time_Out]-[Time_In])*24)>=4,0.166666666,IIf(([Level_Of_Unit]="F0") And (([Time_Out]-[Time_In])*24)>=6,0.25,([Time_Out]-[Time_In]))))

    I tried the above code with slight modification, but its not working. Could you help me?

  13. #13
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    I'd write this into a function. Once you get to 3 or more IIf's it becomes hard to see what's actually happening unless you colour code it like Steve has.
    Also it makes changing any of the parameters really simple and clean in one place. Something like
    Code:
    Public Function fnMaxTimes(tInTime As Date, tOutTime As Date, sSession As String) As Date
    
        Dim TimeDiff         As Single
        Const c4hr           As Single = 0.166666666
        Const c3Hr           As Single = 0.125
        
        'Logic Rules
        '1) [Session_ID]="Evening", then rule 2 applies
        '2) (([Time_Out]-[Time_In])*24)>=4
        '3) (([Time_Out]-[Time_In])*24)>=3
    
    
        TimeDiff = tOutTime - tInTime
        
        fnMaxTimes = TimeDiff
        
        If TimeDiff > c4hr Then fnMaxTimes = c4hr
        
        If sSession = "Evening" Then
            If TimeDiff > c3Hr Then fnMaxTimes = c3Hr
        End If
      
    
    
    End Function

  14. #14
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by Eranka View Post
    I tried the above code with slight modification, but its not working. Could you help me?
    What does "not working" mean?
    Any Error messages?

    Can you provide examples of what you have and what you want to see?

    ------------------------------------------------------------------------------------------------

    Re-writing your formula in Post #12 to a standard IF() function, it looks like this:
    Code:
        If ([Session_ID] = "Evening") And (([Time_Out] - [Time_In]) * 24) >= 3 Then
            x = 0.125
        Else
            If (([Time_Out] - [Time_In]) * 24) >= 4 Then
                x = 0.166666666
            Else
                If ([Level_Of_Unit] = "F0") And (([Time_Out] - [Time_In]) * 24) >= 6 Then
                    x = 0.25
                Else
                    x = [Time_Out] - [Time_In]
                End If
            End If
        End If
    
    
    
    '-------- OR ----------
    'could use ElseIF syntax
    
        If ([Session_ID] = "Evening") And (([Time_Out] - [Time_In]) * 24) >= 3 Then
            x = 0.125
        ElseIf (([Time_Out] - [Time_In]) * 24) >= 4 Then
            x = 0.166666666
        ElseIf ([Level_Of_Unit] = "F0") And (([Time_Out] - [Time_In]) * 24) >= 6 Then
            x = 0.25
        Else
            x = [Time_Out] - [Time_In]
        End If

    You can paste this into a module, change the variables (in blue) and execute the sub to see the results (use F8 to step through the code or F5 to execute).
    Prints the result to the immediate window
    Code:
    Public Sub test()
        Dim x As Single
        Dim dTime_In As Date
        Dim dTime_Out As Date
        Dim sSession_ID As String
        Dim sLevel_Of_Unit As String
    
        '------------------------------------------------------------
        'enter variables - change values to get different results
        dTime_In = #6:00:00 AM#
        dTime_Out = #11:00:00 AM#
        sSession_ID = "Evening"
        sLevel_Of_Unit = "F0"
        '------------------------------------------------------------
    
        If (sSession_ID = "Evening") And TimePart >= 3 Then
            Debug.Print "TimePart = " & TimePart & " return 0.125"
            '   x = 0.125
        Else
            If TimePart >= 4 Then
                Debug.Print "TimePart = " & TimePart & "; return 0.166666666"
                '   x = 0.166666666
            Else
                If (sLevel_Of_Unit = "F0") And TimePart >= 6 Then
                    Debug.Print "TimePart = " & TimePart & "; return 0.25"
                    '   x = 0.25
                Else
                    Debug.Print "TimePart = " & TimePart & "; return " & Round((dTime_Out - dTime_In), 5)
                    '   x = Round(dTime_Out - dTime_In, 5)
                End If
            End If
        End If
    
    End Sub

  15. #15
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    ErankaDates-v1.zip
    Here's another approach.
    It uses a function to calculate the elapsed time between 2 dates in hours and minutes.
    The example doesn't take into account the evening session, but could be easily added, I think.
    Adapted code originally from Graham R Seach MCP MVP
    Last edited by davegri; 12-23-2017 at 12:50 AM. Reason: evening consideration

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

Similar Threads

  1. MS Access vba AfterUpdate coding question.
    By Allen Sundholm in forum Forms
    Replies: 10
    Last Post: 05-25-2017, 04:01 AM
  2. Help with access 2010 coding
    By framar in forum Access
    Replies: 5
    Last Post: 12-30-2012, 01:26 PM
  3. Replies: 6
    Last Post: 08-26-2012, 08:25 PM
  4. Need Help with Access vb coding
    By Entrimo in forum Access
    Replies: 1
    Last Post: 02-19-2012, 06:15 PM
  5. Access without coding
    By kp123 in forum Access
    Replies: 4
    Last Post: 11-25-2011, 03:50 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