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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Calculation Variable in another Field

Status
Not open for further replies.

weathergirl

Technical User
Jul 30, 2002
29
US
I'm calculating the difference between two times in a field on a form. I need this value to be entered in the table for further calculations in reports. I created another field to manually enter the minutes or the difference between the two times. Is there any script that would make the calculated value automatically go into that field?

Thanks,
Weather Girl
 
In the newValue method of the calculated field, set the appropriate field in the table to the calculated result. Something like below (untested), assumes TimeDiff is the field in the Table which is also on the form and the table is in edit mode:

method newValue(var eventInfo Event)

;complete actions for the newValue method
doDefault
;update the field in the table:
TimeDiff.value = self.value
postRecord()

endMethod
 
I've tried using a if statement which works to get a value to appear in the right field. But the number that appears is not the right number. The formats of the fields may be are different but I can't change them to be the same.

This is the code I used:

method newValue(var eventInfo Event)
if LTCalc.value >= 60 then
Lead_Time.value = LTCalc.value
endif
endMethod
 
What about the doDefault statement? This is needed to ensure that the calculation is completed before you access the value - otherwise you will only get the value before it is changed. There is some good info on the newValue and changeValue methods in the help file.

method newValue(var eventInfo Event)
postRecord()
doDefault
if LTCalc.value >= 60 then
Lead_Time.value = LTCalc.value
postRecord()
endif
endMethod
 
I tried using that script and it gives me an error when I run the form. It said "an error occured when setting the property named 'Value' of the object named 'Lead_Time' of type 'Field'". I can't run the form when this script is inserted.
 
I think the problem is that the form is not in edit mode before the first newValue event occurs. It may be more simple to put a pushbutton on the form with the following code:

method pushButton(var eventInfo Event)
if LTCalc.value > 60 then
edit()
Lead_Time.value = LTCalc.value
endEdit()
endif
endMethod
 
OGriofa,

Actually, I prefer to use:

Code:
   if not isEdit() then
      edit()
   endIf

Hope this helps...

-- Lance
 
Weathergirl,

I don't see the correlation between your code and finding a time difference. There are a few ways to accomplish what you want to do. I don't use the newValue event. I use a variation of the changeValue event, which picks up not only initial values, but edited changes. Let's say you have three fields: start_time, end_time, and tot_minutes.

On the changeValue event of start_time and end_time, and on the arrive event of tot_minutes I would attach the following code (or you can make a custom method and call it):

Code:
if not self.isEdit()
   then self.edit()
endif

if end_time.value <> &quot;&quot; and start_time.value <> &quot;&quot;
   then tot_minutes.value = (end_time.value-start_time.value)
endif

This way the calculation will take place if the start_time or end_time fields are changed, or when the cursor arrives in the tot_minutes field. The calculation will only happen if there are values in the start and end time fields.





Mac :)

&quot;Do not delve too deeply in the arts of your enemy and so become ensnared by them&quot;

langley_mckelvy@cd4.co.harris.tx.us
 
Mac,

This script works to a certain extent. The subtracted value is put into the tot_minutes field but it doesn't subtract the hours. I don't know if it's the format of the fields; they are all in the same format of 3:30 PM. I already have a field that does the calculation by using the field properties. But I'm just trying to move that value into a different field without having to type it in the box or tab through it.

Weather Girl
 
But is it giving a correct total in minutes? If so, just divide by 60 before inserting it into the blank.

You may need to use that the formatStringAsTime() function we discussed in the other thread.

Mac :)

&quot;Do not delve too deeply in the arts of your enemy and so become ensnared by them&quot;

langley_mckelvy@cd4.co.harris.tx.us
 
It gives the correct minutes, but when I tried dividing the number was much further off. I also tried inserting the formatStringAsTime() but it gives me a syntax error when I do that.

Weather Girl
 
Okay, maybe I don't understand so let's take a look at an example. Give me the exact start time, end time and number of minutes you get. If this doesn't work, email me the form and tables with sample data and I'll take a look.

Mac :)

&quot;Do not delve too deeply in the arts of your enemy and so become ensnared by them&quot;

langley_mckelvy@cd4.co.harris.tx.us
 
I put in 5:40 PM for the start time and 5:50 PM for the end time. In the tot minutes it calculates 12:09:59. It seems to not round up the seconds and I don't need the hours or the seconds on there. Just the number of minutes.

Weather Girl
 
Weathergirl, stick this code in a script to see how it works, then adapt it for your needs. The minutes field should be a number, not a time field. This will obviously not span days (i.e. across midnight)

Code:
var
   start,end,diff longInt
   result number ; equivalent to your minutes field
endvar

;paradox stores time internally as a long INT
;which does not convert back to time very well

start=longInt(Time(&quot;05:40:00 PM&quot;)); like start time field
end=longInt(Time(&quot;05:50:00 PM&quot;)) ; like end time field

diff=end-start

result=((diff/1000)/60)

view(result) ; see it as minutes


Let me know if this works,


Mac :)

&quot;Do not delve too deeply in the arts of your enemy and so become ensnared by them&quot;

langley_mckelvy@cd4.co.harris.tx.us
 
It didn't work sorry to say. The view box of the number comes up with a &quot;default&quot; value that appeared the 1st time I used it, but it never changes. Then it inserts a number like 599940 when there's a difference of twenty-five minutes. I tried changing a couple things but that only made it worse.

Weather Girl
 
Did you paste this into a new blank script? I did, and I get the correct number of minutes. I just did it again to be sure there were no typos.

That long number you cited is the longInt equivalent of the time, so something has to have been changed in the script. It sounds like you didn't change the minutes field to a number type instead of a time type.

Mac :)

&quot;Do not delve too deeply in the arts of your enemy and so become ensnared by them&quot;

langley_mckelvy@cd4.co.harris.tx.us
 
I inserted it into a new blank script and it does the same thing. I changed the minutes field to a number and I still get the longInt.

Weather Girl
 
Something is amiss, there is no way that cannot work properly. Did you copy it straight from this board and into the blank script?

Mac :)

&quot;Do not delve too deeply in the arts of your enemy and so become ensnared by them&quot;

langley_mckelvy@cd4.co.harris.tx.us
 
I copied it straight from the board. Would it still be something to do with the format of the string?

Weather Girl
 
The only thing I can think of is that your Windows Time settings (which Paradox uses by default) have somehow been changed. See what they look like and we'll go from there.

Mac :)

&quot;Do not delve too deeply in the arts of your enemy and so become ensnared by them&quot;

langley_mckelvy@cd4.co.harris.tx.us
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top