Results 1 to 4 of 4
Thread: explanation of the code

051903, 12:56 #1Registered User
 Join Date
 Oct 2002
 Posts
 12
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

051903, 14:33 #2Registered User
 Join Date
 May 2003
 Location
 Providence, RI
 Posts
 12
<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 (in6.773 out7) 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!

051903, 14:42 #3Registered User
 Join Date
 Oct 2002
 Posts
 12
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

051903, 14:56 #4Registered User
 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
Take care.