Results 1 to 6 of 6

Thread: Access Formula (DiffDate)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Cairo, Egypt
    Posts
    275

    Access Formula (DiffDate)

    Hello,
    I was wondering how I can make Microsoft Access calculate the difference between dates. Where a column contains the time the employee entered in and another column containing the time the employee left. I would like to have in a decimal form (for example: if somone cameat 8:30 and left at 10) the calculated field would contain 1.5). Any help would be really appreciated. Thanks in advance!

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Access Formula (DiffDate)

    VB Code:
    1. MsgBox (DateDiff("n", Text0.Value, Text2.Value) / 60)

    Will accomplish what you want..I tried using hours..but it wouldnt give me a decimal number :/

  3. #3
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Access Formula (DiffDate)

    This may work a little better for you:


    VB Code:
    1. Private Sub Command1_Click()
    2. Dim intMinutes As Integer
    3. Dim intHours As Integer
    4.     Text0 = "8:30"
    5.     Text3 = "10:00"
    6.     intHours = 0
    7.     intMinutes = DateDiff("n", [Text0], [Text3])
    8.     intHours = intMinutes \ 60
    9.     intMinutes = intMinutes - (intHours * 60)
    10.    
    11.     Text5 = intHours & ":" & Format(intMinutes, "00")
    12. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Access Formula (DiffDate)

    I was reviewing you post again and I noticed that you wanted the result to be "1.5", so I adjusted my code to return both "1:30" and "1.50":

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim intMinutes As Integer
    3. Dim intHours As Integer
    4.     Text0 = "8:30"
    5.     Text3 = "10:00"
    6.     intHours = 0
    7.     intMinutes = DateDiff("n", [Text0], [Text3])
    8.     intHours = intMinutes \ 60
    9.     intMinutes = intMinutes - (intHours * 60)
    10.    
    11.     'The Following line will return '1:30'
    12.     Text5 = intHours & ":" & Format(intMinutes, "00")
    13.  
    14.     'The Following line will return '1.50'
    15.     Text7 = intHours & "." & Format(100 / (60 / intMinutes), "00")
    16. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Access Formula (DiffDate)

    An alternative:
    Code:
    cdbl(cdate("10:00")-cdate("08:30"))*24
    Where the Cdates are you can use a field with the time in.
    Note: This will probably only work for time differences, or date/times on the same date. Different dates would produce weird results

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Access Formula (DiffDate)

    Quote Originally Posted by Ecniv
    An alternative:
    Code:
    cdbl(cdate("10:00")-cdate("08:30"))*24
    Where the Cdates are you can use a field with the time in.
    Note: This will probably only work for time differences, or date/times on the same date. Different dates would produce weird results

    Nice piece of code!!!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width