Normal Topic Subform Calculation Errors (Read 1782 times)
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Subform Calculation Errors
Mar 22nd, 2004 at 1:38am
Print Post Print Post  
There seems to be a problem with totaling up a group of subform values!  (Version 1.0.2)

Here is a sample of the code I am using.
Code
Select All
var vCnt As Int
var vLoop As Int
var vNetGain as double

vCnt = @FormResultSetTotal("Transactions Form")
vLoop = 1

while vLoop <= vCnt
 {
 vNetGain = vNetGain + @ToMoney( @FormFieldValue("Transactions Form", "Amount", vLoop) )
 // writeln("vLoop= " + vloop + "	vNetGain= " + vnetgain)
 vLoop += 1
 }

@msg("Net Gain = $" + vNetGain) 


This is set to activate at MainForm :: OnElementEntry.  After testing, I'm pretty certain the problem is that the @FormFieldValue() function is only able to retrieve the value of the first subrecord in time to be used in the loop.

So what happens is the @FormFieldValue gets the first subform's value, then since it's NOT being updated with the rest of them, the loop just keeps calculating with that first value (essentially multiplying it by the number of subrecords).

The reason I think the function is not getting the values in time, is that if you click on another element to re-trigger the code, it produces the correct total.  And by that time, all the subrecords have already been retrieved.  So this indicates that the code is not flawed.

Lanticans, can you let me know what your experience is with this?
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Subform Calculation Errors
Reply #1 - Mar 22nd, 2004 at 12:29pm
Print Post Print Post  
I just checked this in Version 1.0 & 1.0.1, and it works perfectly correct!

Unfortunately, it looks like something that was "fixed" caused this to break. Wink
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Calculation Errors
Reply #2 - Mar 22nd, 2004 at 1:23pm
Print Post Print Post  
Carl,

When your code runs, what is the value of vCnt?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Subform Calculation Errors
Reply #3 - Mar 22nd, 2004 at 2:01pm
Print Post Print Post  
vCnt correctly reflects the number of subrecords.  You can see it if the "//" is removed from the writeln() line.  I updated the writeln() here:
Code
Select All
while vLoop <= vCnt
 {
 vNetGain = vNetGain + @ToMoney( @FormFieldValue("Transactions Form", "Amount", vLoop) )
 writeln("vCnt= " + vcnt + "     vLoop= " + vloop + "     vNetGain= " + vnetgain)
 vLoop += 1
 }
 


Occasionally it will total the amounts correctly upon entering the form, but 95% of the time it is wrong if there is more than one subrecord, until you move focus to another element to re-trigger the calculation after all of the subforms are diplayed.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Calculation Errors
Reply #4 - Mar 22nd, 2004 at 10:20pm
Print Post Print Post  
Carl,

You've uncovered a fairly complex timing issue having to do with table view subforms. We've made an adjustment for 1.0.3 that should take care of it.

For now, you'll need to move your code so it's not trying to run immediately when the Form is opened. You can put it in an OnElementExit event, so it updates as soon as you press Tab or Enter. You can put it on a button that User can click to update the calculated values. Etc.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Subform Calculation Errors
Reply #5 - Mar 23rd, 2004 at 2:45am
Print Post Print Post  
Quote:
You can put it on a button that User can click to update the calculated values. Etc.

No problem.  I'm my own developer and user.  i.e. I'm the only one that uses my apps.  Since I'm only using it in @msg(), I can just remember to tab to another element to see the correct total until 1.0.3 is out.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Subform Calculation Errors
Reply #6 - Mar 23rd, 2004 at 2:10pm
Print Post Print Post  
I hate to throw gasoline on the fire, but when the code listed in the 1st post above is combined with this next piece of code, it causes Sesame to crash!

It crashes when advancing to a new/blank form in update mode.  It appears that it crashes after backing up and displaying the previous record (the last one in the result set).

Located in [Form] :: On Form Entry
Code
Select All
If @Update and System ID = ""
 {
 @Msg("No more records. Press Esc to exit or F9 for previous form.")
 ResultSetCurrentPosition(@ResultSetCurrentPosition() - 1)
 }
 


Again, this only happens when BOTH of these codes are in programming.  Individually, the don't cause any problems.

I can deal with not using these together, but do you think this may also be related the same timing issue?
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Calculation Errors
Reply #7 - Mar 23rd, 2004 at 2:31pm
Print Post Print Post  
Carl,

Could you please post the entire program that is crashing?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Subform Calculation Errors
Reply #8 - Mar 23rd, 2004 at 4:33pm
Print Post Print Post  
Erika,

I have sent the app to support@lantica.com at your attention in the subject line.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Subform Calculation Errors
Reply #9 - Sep 19th, 2004 at 2:31am
Print Post Print Post  
I have posted this update for the benefit of anyone that finds this archive, and as a reminder to myself.

All the issues in this thread are resolved in Version 1.0.4 (and probably were fixed in 1.0.3 as well, but I just found this old thread, and I re-tested everything here to make sure all is fixed.)
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged