Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2002
    Posts
    12

    Post Unanswered: explanation of the code

    hi there,
    can somebody explain me the following code.
    i know it takes the taim variable ad caluate the time difference from 12:00 AM(i.e elapsed time from 12:00am)
    but could not understand the code line by line


    Function GetElapsedTime(interval)

    Dim totalhours As Long, totalminutes As Long
    Dim days As Long, hours As Long, minutes As Long

    days = Int(CSng(interval))
    totalhours = Int(CSng(interval * 24))
    totalminutes = Int(CSng(interval * 1440))
    hours = totalhours Mod 24 + (days * 24)
    minutes = totalminutes Mod 60

    GetElapsedTime = hours & " " & "Hours and" & " " & minutes & " " & "Minutes"

    End Function


    Thanyou in advance,
    asif

  2. #2
    Join Date
    May 2003
    Location
    Providence, RI
    Posts
    12

    Talking

    <snip>


    Function GetElapsedTime(interval) 'This line declares the name of the function and allows you to call it from anywhere in the database, instead of typing the name in over and over.

    Dim totalhours As Long, totalminutes As Long 'Declare the variables and their data type (long integer).
    Dim days As Long, hours As Long, minutes As Long 'Same as above

    days = Int(CSng(interval)) 'This line loads the interval into the days variable. The "Int" part returns an integer (in-6.773 out-7) and the CSng forces a single precision number (a bit redundant, if you ask me). The (interval) is the input from a form, etc., in days.
    totalhours = Int(CSng(interval * 24)) 'Same as above, but multiplies the input by 24 to get the hours in a day
    totalminutes = Int(CSng(interval * 1440)) 'Same, but multiplied by 1440 to get the total minutes in a day.
    hours = totalhours Mod 24 + (days * 24) 'Here, the totalhours variable is divided by 24 and the remainder of the division process is taken and added to days*24 to get the actual number of hours. (The Mod operator is also redundant, as you've already stripped the "days" variable to an integer.
    minutes = totalminutes Mod 60 'Same as above, essentially.

    GetElapsedTime = hours & " " & "Hours and" & " " & minutes & " " & "Minutes" 'This will load the variable GetElapsedTime (or a form control, perhaps) with the result of the equation above. So, if you chose 7.2 for the interval, this is what would happen:

    days = 7
    totalhours = 168
    totalminutes = 10080
    hours = 175
    minutes = 168

    GetElapsedTime = 175 Hours and 168 Minutes 'The & concatenates a string together with other strings or variables or whatever. However, this is a weird way of doing things. You should try

    Dim h, m, Diff

    DateDiff ("h", SomeDateandTime, Now())
    DatePart ("n", Now())
    Diff = h & " Hours and " & m & " minutes"

    'Granted, this won't return the exact number of minutes elapsed, but it will give you an approximation, and if you are calculating from midnight on the date, you're fine.

    Good luck!

  3. #3
    Join Date
    Oct 2002
    Posts
    12

    Talking

    hi popfly,
    thanks a lot your explanation ws great. i have one more question...what it re4ally seem is that the varaible passed to the elapsedtime function seemto be the time for example 12:45:00(short time)wondering how it is able to pases this....the code works fine. thanks
    asif

  4. #4
    Join Date
    May 2003
    Location
    Providence, RI
    Posts
    12
    Originally posted by mdasif
    hi popfly,
    thanks a lot your explanation ws great. i have one more question...what it re4ally seem is that the varaible passed to the elapsedtime function seemto be the time for example 12:45:00(short time)wondering how it is able to pases this....the code works fine. thanks
    asif
    Well, the Date Format of Access holds the date and time as a number. That number is the number of days elapsed since 1/1/1900. The fractions of the number are used to calculate the time. So, 10045.5 would be (assuming today is the 10045th day since 1/1/1900) noon today. 10046.25 would be tomorrow at 6 am, etc. You can grab the date and time separately in a form and concatenate them in a query, then use the new value (datetime) to calculate the number of hours, minutes, seconds, whatever, elapsed.

    Take care.

Posting Permissions

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