As long as you didn't name the module ConvertTime, that should be all you
need to do.
Actually, though, I just noticed there are some errors in my code. (I made
some changes to variable names, and didn't propagate the changes
everywhere). The function should be:
Function ConvertTime(InputValue As String) As Single
Dim intDaysP As Integer
Dim intHoursP As Integer
Dim intDays As Integer
Dim intHours As Integer
Dim intMinutes As Integer
intDaysP = InStr(InputValue, " Days ") + 6
intHoursP = InStr(InputValue, " Hours ") + 7
intDays = Val(InputValue)
intHours = Val(Mid(InputValue, intDaysP, 2))
intMinutes = Val(Mid(InputValue, intHoursP, 2))
ConvertTime = CLng(intDays) * 24 + _
CLng(intHours) + _
CLng(intMinutes) / 60#
End Function
Actually, if you're going to be using this with a table, where it's possible
that fields you're passing to the function might contain Null values, you
should probably change that to:
Function ConvertTime(InputValue As Variant) As Single
Dim intDaysP As Integer
Dim intHoursP As Integer
Dim intDays As Integer
Dim intHours As Integer
Dim intMinutes As Integer
If Not IsNull(InputValue) Then
intDaysP = InStr(InputValue, " Days ") + 6
intHoursP = InStr(InputValue, " Hours ") + 7
intDays = Val(InputValue)
intHours = Val(Mid(InputValue, intDaysP, 2))
intMinutes = Val(Mid(InputValue, intHoursP, 2))
ConvertTime = CLng(intDays) * 24 + _
CLng(intHours) + _
CLng(intMinutes) / 60#
End If
End Function
To use this in a query, simply put ConvertTIme([MyField]) in the query.
(replace MyField with the actual field name)
As to your other problem, when you say "ElapsedTime is my field formated
with 00 Days etc...", are you saying that you've created a module that has a
variable named ElapsedTime in it? How did you declare the variable? The
function is expecting it to be declared as a string: if you're not
explicitly declaring variables, ElapsedTime would be a variant, and you'll
get that problem. The second version of the function above will solve that
problem.
If you're not explicitly declaring variables, you should be. If you haven't
got Access set up to force you to declare variables, so that it
automatically puts "Option Explicit" at the top of each module, you should.
(you do this on the Module tab through Tools | Options while in the VB
Editor). Doing so will vastly decrease the amount of time you need to spend
on debugging!
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
joe said:
This is great! However, I'm not sure quite how to use it
I created a new
module, copied the function into it.
I have a query that creates a new field and I would like the function to
populate that new field. What do I put in the query?
NewField: ConvertTime (now what?, the field name of the field which contains
the data to be converted?).
The second question is that I tried the code in the immediate window. I typed
?ConvertTime (ElapsedTime) -ElapsedTime is my field formated with 00 Days
etc.... When I do so I get a compile error ByRef Argument Error
If I type
?ConvertTime (00 Days 00 Hours 00 Minutes) I get the msg. Compile error:
Expected: list separator or )
I'm sorry for all the questions!!