Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble using an expression in a function procedure

Status
Not open for further replies.

MaDmiX

Technical User
Dec 10, 2002
19
US
Hey there,

I have creted the following expression to return a framecount from a user entry of "hours:mins:secs:frames"

= (Val(Mid([timecode], 1, 2)) * 60 * 60 * 29.97) + (Val(Mid([timecode], 4, 2)) * 60 * 29.97) + (Val(Mid([timecode], 7, 2)) * 29.97) + Val(Mid([timecode], 10, 2))

"timecode" is the user entered value, in hh:mm:ss:ff.

Now I have been racking my brain trying to put this into a function, that will simply return the framecount... something like:

=Framecount ([user_entry])

If I can get this far, then I will be able to do really cool things like:
=Framecount ([segment_end]) - Framecount ([segment_start])

This will at least be 1/2 the battle. Converting back from a framecount to hh:mm:ss:ff will be the other 1/2. Call me a VB newbie. Can ya help here?

Thanks much,

Ken


 
Ken,

Looks like you've done the heavy lifting putting together your expression. Here it is wrapped up as a function:

Code:
Function Framecount( timecode As String ) As Double

  Framecount = (Val(Mid(timecode, 1, 2)) * 60 * 60 * 29.97) + (Val(Mid(timecode, 4, 2)) * 60 * 29.97) + (Val(Mid(timecode, 7, 2)) * 29.97) + Val(Mid(timecode, 10, 2))

End Function

Keep in mind there is no error checking/handling.


HTH
Mike
 
Mike,

Thank you sooo much. This function worked perfectly!!!

I have an idea of how to convert back from my framecount (just a rough scetch)... I imagine that I now have to divide the framecount by the same values that I previously multiplied by. But how will access know that after it calculates as many "whole hours" as it can, it must make the remaining frames into minutes, and secs and so on? Heres what I have so far:

hours = ((framecount / 3600) / 29.97) As Int
Mins = (((framecount / 3600) / 29.97) - ((framecount / 3600) / 29.97) As Int) As Int
Secs = Ouch!!! my head hurts!

Am I at least on the right track?

Many thanks,

Ken
 
Ken,

I must admit I didn't spend much time looking at your actual calculation initially. I'm assuming the constant 29.97 is the number of frames/second. Is that right? What is the "frames" parameter in "Hours:Mins:Secs:frames"?

Regards,
Mike
 
Hi Mike,

Yes, you are correct. The frames parameter is the # of frames and 29.97 is the # of frames / sec. I worked out a VB function that I think will handle the conversion from total frames back to hh:mm:ss:ff. Is my code correct, because I haven't gotten it to work yet? I don't have very much experience with VB, but I'm learning as I go. Here it is:

Function CnvToTime(framecount)
Dim hours, mins, secs, frames As Long

hours = ((framecount / 29.97) / 60) / 60
mins = (framecount / 29.97) / 60
secs = (framecount / 29.97)
frames = framecount

result = Format(hours) & ":" & (mins) & ":" & (secs) & ":" & (frames)

CnvToTime = result

End Function

Thank you very much for you help!

Ken

 
Ken,

Again, I am not 100% sure I know what your calculations are doing. Tell me if this is wrong: You want to convert a frame count back to elapsed time, expressed as Hours:Minutes:Seconds. If so, then you were on the right track before. Here is how I would break it down: 1) Total number of frames (framecount) divided by the frame rate in seconds gives the total elapsed time in seconds (taking the integer portion only). 2) Total time divided by 3600 and taking the integer portion gives the number of complete hours. 3)Compute the time left over, in seconds by subtracting Hours*3600 from the total time. 4) Divide by 60 and take the integer portion to get the minutes. 5) Compute the time left over by subtracting Hours*3600 and Minutes*60 from the total time; this is the seconds.

Here is my version of your funtion:

Code:
Function CnvToTime(FrameCount As Long) As String
Const FRate = 29.97
Dim Hours As Long
Dim Mins As Long
Dim Secs As Long
Dim TotalTime As Long

  TotalTime = Int(FrameCount / FRate)
  
  Hours = TotalTime / 3600
  Mins = Int((TotalTime - Hours * 3600) / 60)
  Secs = TotalTime - Hours * 3600 - Mins * 60

  CnvToTime = Format(Hours, "00") & ":" & Format(Mins, "00") & ":" & Format(Secs, "00") & ":" & FrameCount

End Function

It's not necessary but I like to use Named constants rather than hardcoded, hence FRate.

Regards,
Mike
 
Thanks Mike,

You're right on the money. That's exactly what I'm trying to do. I added some code to give me the frames, as well as the hours, mins and secs. I think I did it correctly:

Function CnvToTime(FrameCount As Long) As String
Const FRate = 29.97
Dim Hours As Long
Dim Mins As Long
Dim Secs As Long
Dim Frames As Long
Dim TotalTime As Long

TotalTime = Int(FrameCount / FRate)

Hours = TotalTime / 3600
Mins = Int((TotalTime - Hours * 3600) / 60)
Secs = TotalTime - Hours * 3600 - Mins * 60
Frames = Framecount - TotalTime * Frate * 60

CnvToTime = Format(Hours, "00") & ":" & Format(Mins, "00") & ":" & Format(Secs, "00") & ":" & Format(Frames, "00")

End Function

Unfortunately I'm getting an error when I use the function in an expression, saying "the expression contains an ambiguous name" and "you may have two or more functions with the same name in different modules".

Did I at least write the code correctly? I want to be able to rule that out.

Ken

 
Mike,

I got it!!! Heres the final code:

Function CnvToTime(FrameCount As Long) As String
Const FRate = 29.97
Dim Hours, Mins, Secs, Frames, TotalTime As Long

TotalTime = Int(FrameCount / FRate)

Hours = TotalTime / 3600
Mins = Int((TotalTime - Hours * 3600) / 60)
Secs = TotalTime - Hours * 3600 - Mins * 60
Frames = FrameCount - TotalTime * FRate

CnvToTime = Format(Hours, "00") & ":" & Format(Mins, "00") & ":" & Format(Secs, "00") & ":" & Format(Frames, "00")

End Function

The reason I was getting that error before is because I had saved an earlier (backup) version of the same module with a similar name.

Thank you so much for all your help. I learned alot.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top