Very Hot Topic (More than 25 Replies) Running Total for subform lines (Read 4618 times)
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Running Total for subform lines
Dec 7th, 2005 at 4:40am
Print Post Print Post  
Maybe someone has been here already.??

I want to have a dynamic running total Cost element in tableview of a subform.  As line items are added/modified/deleted the running total will updated on each line.  Seems like i will need to keep looping when one line changes, starting at record1, to record n.

Execute when enter and exit sub form, when exit Cost element, and when enter parent form?

Same as steps when calculating total for all lines in subform but do subtotal calculation and write to element on each record when counting them and getting total? Element will probably not be bound to anything.

Hmmmm, I think I am almost there, just typing this out has helped to make it more clear in my mind.

Thanks for listening......I guess the tree does make a sound when no one is there to hear it.....
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Running Total for subform lines
Reply #1 - Dec 7th, 2005 at 10:06am
Print Post Print Post  
Never mind, I got it done a few hours ago ......... Grin

But thanks for thinking about it for me .... Cheesy
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Running Total for subform lines
Reply #2 - Dec 8th, 2005 at 6:07pm
Print Post Print Post  
Do you mind sharing what worked for you and maybe a snipit  of the code? 

I will need to do this within the month so any help would be appreciated.

Thanks
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Running Total for subform lines
Reply #3 - Dec 8th, 2005 at 9:43pm
Print Post Print Post  
Glad to oblige

No access to code right now, but should happen in next few
days, possibly later tonight.
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Running Total for subform lines
Reply #4 - Dec 9th, 2005 at 12:52am
Print Post Print Post  
Got to it sooner than I thought......here is the code:
Code
Select All
SubRoutine srCalculateTotalCost()

/*
1. Purpose is to calculate a Total for parent form and RunningTotal for subform
2. Structure is Parent Form name is frmParent, subform with natural link is named sfrmChild.
3. Currency element on frmParent, named TotalCost to show total of Cost for all lines in subform.
4. Currenty element on sfrmChild named Cost to show Cost for that individual record.
5. Unbound element on sfrmChild, named RunningTotal, ReadOnly, to show running total for Costs for all records in the subform.
6. Defined as subroutine so can be called on sfrmChild-Cost-OnExitElement and on sfrmChild-Cost-OnFormExit.
*/
//==============================================

var vCount As Int		//Count number of records in subForm
var vLoop As Int		//While loop counter
var vTotal As Double		//Total Cost for all records in subForm
var vRunningTotal As Double	//Running total of Cost

If @FormIsStandalone() = 0 Then {

	vTotal = 0
	vRunningTotal = 0
	vCount = @FormResultSetTotal("sfrmChild")

	vLoop = 1
	While vLoop <= vCount {
		vTotal +=    @ToNumber(@FormFieldValue("sfrmChild","Cost",vLoop))
		vRunningTotal = vTotal
		RunningTotal = vRunningTotal
		vLoop += 1
		}

	If vTotal > 0 Then {
		FormFieldValue("frmParent","TotalCost",0,vTotal)
		}
	}

End SubRoutine
//======================================

 


I have removed other code specific to my application, and changed form names to provide a more generic sample code.

I use a pretty rigid naming convention.  Many elements have a prefix followed by InitialCapsName, no spaces, no underscores.
I use "frm" as a prefix for most forms, and "sfrm" for subforms.
Most variables start with lowercase "v".
SubRoutines use "sr".
Databases start with "tbl".
Text type of elements do not have a prefix, but do use InitialCapsNames.  But the names of the fields that they are bound to in the underlyiing tblDatabase do have a prefix.

But that is a whole other discussion.  Just provided here to help read the sub routine code.
-----------------------------------------------
This is only being used on a test application right now.  I would like to do some more testing in the real world; I am  still concerned about the RunningTotal going onto the correct line item in the proper sequence.  What happens if the lnes are resorted?  Since this is unbound, it should not be a big issue,

I think this code is probably good.  If a line on the subform is deleted, it appears to correct itself, but I am not yet comfortable that this is rock solid.  So use with caution, and any feedback is appreciated.

Edited on 3/8/07. Code sample above was modifed to remove calling function from frmParent.  Problem was found by CapitalG.  Thanks for reporting that.
« Last Edit: Mar 8th, 2007 at 10:25pm by Bob_Hansen »  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Running Total for subform lines
Reply #5 - Dec 9th, 2005 at 4:01pm
Print Post Print Post  
Thank you.  Your help is greatly appreciated.
  
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #6 - Feb 28th, 2007 at 5:42pm
Print Post Print Post  
Bob_Hansen wrote on Dec 9th, 2005 at 12:52am:
Got to it sooner than I thought......here is the code:
Code
Select All
SubRoutine srCalculateTotalCost()

/*
Purpose is to calculate a Total for parent form and RunningTotal for subform
Structure is Parent Form name is frmParent, subform with natural link is named sfrmChild.
Currency element on frmParent, named TotalCost to show total of Cost for all lines in subform.
Currenty element on sfrmChild named Cost to show Cost for that individual record.
Unbound element on sfrmChild, named RunningTotal, ReadOnly, to show running total for Costs for all records in the subform.
Defined as subroutine so can be called on sfrmCost-OnExitElement and on sfrmCost-OnFormExit,  
Is also defined and called on frmParent-OnFormEntry.
*/
//==============================================

var vCount As Int		//Count number of records in subForm
var vLoop As Int		//While loop counter
var vTotal As Double		//Total Cost for all records in subForm
var vRunningTotal As Double	//Running total of Cost

If @FormIsStandalone() = 0 Then {

	vTotal = 0
	vRunningTotal = 0
	vCount = @FormResultSetTotal("sfrmChild")

	vLoop = 1
	While vLoop <= vCount {
		vTotal +=    @ToNumber(@FormFieldValue("sfrmChild","Cost",vLoop))
		vRunningTotal = vTotal
		RunningTotal = vRunningTotal
		vLoop += 1
		}

	If vTotal > 0 Then {
		FormFieldValue("frmParent","TotalCost",0,vTotal)
		}
	}

End SubRoutine
//======================================

 


I have removed other code specific to my application, and changed form names to provide a more generic sample code.

I use a pretty rigid naming convention.  Many elements have a prefix followed by InitialCapsName, no spaces, no underscores.
I use "frm" as a prefix for most forms, and "sfrm" for subforms.
Most variables start with lowercase "v".
SubRoutines use "sr".
Databases start with "tbl".
Text type of elements have do not have a prefix, but do use InitialCapsNames.  But the names of the fields that they are bound to in the underlyiing tblDatabase do have a prefix.

But that is a whole other discussion.  Just provided here to help read the sub routine code.
-----------------------------------------------
This is only being used on a test application right now.  I would like to do some more testing in the real world; I am  still concerned about the RunningTotal going onto the correct line item in the proper sequence.  What happens if the lnes are resorted?  Since this is unbound, it should not be a big issue,

I think this code is probably good.  If a line on the subform is deleted, it appears to correct itself, but I am not yet comfortable that this is rock solid.  So use with caution, and any feedback is appreciated.


Bob,

We've done a bit of redesigning our database since purchasing Sesame this past Fall.  Thanks for your help awhile back when I asked a question regarding the sizing of our Subform fields  Smiley
I am a complete novice at programming, but searched and found this thread...it looks like what I have in mind for getting a running total of our Parts expenditures in our Repair Subform.  This Subform is linked relationally by Serial Number to our Parent Inventory form.  I'd like to dive in and attempt some programming in a copy of our database.  Your opinion would be invaluable.  

I'll try to post a screenshot of our database.


Charlie


http://us.a2.yahoofs.com/users/456dd110z2ef31adb/f7f4scd/__sr_/3f5fscd.jpgphIDc5...
  
Back to top
 
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Running Total for subform lines
Reply #7 - Feb 28th, 2007 at 9:22pm
Print Post Print Post  
Charlie,

When you post your screen shot, give us the specifics of what exactly you want to do. We all like to help and learn.

I see you are in Sacramento, Bob Hansen has a Sesame user group back east (SANE) maybe we can get one going out in California, maybe we can have meetings somewhere between you and LA. Say Bakersfield or ? There must be a bunch of us Sesame users out here that can all benefit from working with each other. Just something to think about.  Smiley

Robert
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #8 - Feb 28th, 2007 at 11:45pm
Print Post Print Post  
BOBSCOTT wrote on Feb 28th, 2007 at 9:22pm:
Charlie,

When you post your screen shot, give us the specifics of what exactly you want to do. We all like to help and learn.

I see you are in Sacramento, Bob Hansen has a Sesame user group back east (SANE) maybe we can get one going out in California, maybe we can have meetings somewhere between you and LA. Say Bakersfield or ? There must be a bunch of us Sesame users out here that can all benefit from working with each other. Just something to think about.  Smiley

Robert


Robert,
I'll definitley be more specific-as much as possible - when attaching a screenshot or anything else

In this "scene" below, I have two databases - the parent is our Inventory, the Subform is derived from our Repair database.  I found that viewing the subform as a Table was, of course, more informative to anyone searching records for any given piece of equipment...the bonus for us when we migrated from Q&A to Sesame, was to be able to confirm, through serial numbers, that a given piece of equipment was actually on our Inventory when we were logging it in for repair without having to open a seperate database.  On our least expensive equipment, we have target Repair amounts*PARTS*) that we look at to make a decision on whether it is cost effective to repair...if the repair Parts  totals are very high and the estimated repair cost approaches 3/4 of the cost of a new machine, we scrap and replace it.  Having our Subform programmed to total our PARTS records when we bring up a specific serial number in search/update mode would be a wonderful thing.  Whether this "Parts Total" calculation was actually displayed within the main  form, the Subform or??? is not critical...as long as we can see the total Parts amount.

On the Table (Subform) you can see "Parts" amounts of:

25.50
22.00
6.00
7.50
140.75
1.00

(I haven't yet sorted the dates in the Repair.db)

We'd like to program the Subform so there is a "Total" amount displayed for "Parts" ...this specific example is a relatively small repair record for one piece of equipment.  We have some items that are  in service and have repairs going back to 1988 (yikes)...20+ repairs for many items.

http://us.a2.yahoofs.com/users/456dd110z2ef31adb/f7f4scd/__sr_/3f5fscd.jpgphIDc5...

I like the idea of a users group "out west" here...time is always at a premuim, but time can always be found for things worthwhile Wink

I'm much "newer" than most here, so please feel free to correct me and slap me around as situations warrant  Roll Eyes

Thwe software is great as is the forum...fantastic resource.
  
Back to top
 
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Running Total for subform lines
Reply #9 - Mar 1st, 2007 at 6:51am
Print Post Print Post  
Charlie,

I am not sure I interpreted your explanation correctly but I might be doing something very similar. I have a database that tracks equipment, each piece of equipment is made up of numerous components. When you enter a record for a specific piece of equipment there is a tab that has all components listed. You can choose either Complete. Need or unknown for each component that makes up this piece of equipment. Every time you select need it adds that item to a tab on the form that is a list of needed components. It then automatically does a lookup to my parts vendor database and lists the approved vendor, price and lead time needed for items. It keeps a running total at the bottom so we can identify the total cost to get that piece of equipment running. We then have a button that prints this data out as a service tag with barcode tracking number that stays with the unit or can be used as a purchase order tool. It does almost the same thing for all parts marked unknown; just on a separate tab. below is the subroutines I use to achieve most of this. I am sure there are probably other ways to do what I am doing (probably even better) but this works for me. Just like you I am not a programmer, however Erika and the Lantica team have taught me that anything can be done by the most inexperienced user by just taking your time and breaking the task down into many many steps and just worry about one step at a time. I am not good at posting pictures but if you want I can e-mail screen shots to you. The reason I used the subroutine was because as long as my element name matches my inventory name the only thing I need to do is add the element to my form and it automatically gets added to the entire process. This makes for easy parts additions.

Keep us posted on how your project is going.  Smiley

Robert

P.S. I agree with you Sesame is great!
P.S.S. you do not need to worry about all the @replace code you see below. I needed that for my lookups because my parts database is derived from my vendors inventory database that I constantly use to replace my parts databse and they have a funky format.

Subroutine PartsStatus() // updates parts needed and unknown

// Declare Variables

Var vElements as String
Var vCnt as Int
Var vLoop as Int
Var vName as String
Var vNlist as String
Var vlabel as String
Var vlabel1 as String
Var vvendor as String
Var VVenPartNum as String
Var VVenCost as Double
Var Vline as String
Var Vtotal as Double      
Var VtotalUknown as Double

// Set Variables


     vnlist = ""
       eqp_partsNeeded = ""
     eqp_partsUnknown = ""
     vvendor = ""
     VVenPartNum = ""
     VVenCost = ""
     Vtotal = 0
     VtotalUknown = 0



     vElements = @StringArrayElementList() // Reads all my elements


   
     vCnt = @CountStringArray(vElements) // counts the anount of elements so my loop knows how many times to run
     vLoop = 0


         While vLoop < vCnt
       {
           vLoop = vLoop + 1
           vName = @AccessStringArray(vElements, vLoop)

           SetThisElement(vName)
           vlabel = @label(thiselement) // this gets the label name and assigns it to Vlabel
           
             
//writeln(velements)
//writeln(thiselement + " " + Vlabel) // lists contents of all elements
//writeln(Vname + "stop") // lists all element names


           
                 If Thiselement = "need" Then

                 {      
           
//writeln(thiselement + " " + Vlabel) // lists contents of all elements


                 vlabel1 = @replace(vlabel, "\" , "\\")

                  vlabel1 = @replace(vlabel1, ")" , "\)")
                  vlabel1 = @replace(vlabel1, "(" , "\(")
                  vlabel1 = @replace(vlabel1, "/" , "\/")
                 
                  vlabel1 = @replace(vlabel1, "-" , "\-")
                  vlabel1 = @replace(vlabel1, "." , "\.")
                  vlabel1 = @replace(vlabel1, "&" , "\&")
                  vlabel1 = @replace(vlabel1, "+" , "\+")
//Writeln(vlabel1)
                 Vvendor = @XLookup(@fn, Vlabel1, "VenEqpTrak!VenEqp_partName", "VenEqp_partVendor")
                 VVenPartNum = @XLookup(@fn, Vlabel1, "VenEqpTrak!VenEqp_partName", "VenEqp_vendorpartNum")
                 VVenCost = @XLookup(@fn, Vlabel1, "VenEqpTrak!VenEqp_partName", "VenEqp_vendorpartCost")
                 
                 Vtotal = Vtotal + @tomoney(vvencost)


                 


                 Vline = Vlabel + "  -  " + Vvendor + "  -  " + VvenPartNum + "  -  " + FormatMoney(VvenCost)




                 eqp_partsNeeded =  eqp_partsNeeded  + @newline() + vline

                 Eqp_TotNeedCost =  Vtotal

                 Eqp_TotalCost = Eqp_TotNeedCost + Eqp_UknownNeedCost

                 //writeln(vnlist)
                 }

// ********************

                 If Thiselement = "Unknown" Then

                 {

                 vlabel1 = @replace(vlabel, "\" , "\\")

                  vlabel1 = @replace(vlabel1, ")" , "\)")
                  vlabel1 = @replace(vlabel1, "(" , "\(")
                  vlabel1 = @replace(vlabel1, "/" , "\/")
                 
                  vlabel1 = @replace(vlabel1, "-" , "\-")
                  vlabel1 = @replace(vlabel1, "." , "\.")
                  vlabel1 = @replace(vlabel1, "&" , "\&")
                  vlabel1 = @replace(vlabel1, "+" , "\+")
//Writeln(vlabel1)
                 Vvendor = @XLookup(@fn, Vlabel1, "VenEqpTrak!VenEqp_partName", "VenEqp_partVendor")
                 VVenPartNum = @XLookup(@fn, Vlabel1, "VenEqpTrak!VenEqp_partName", "VenEqp_vendorpartNum")
                 VVenCost = @XLookup(@fn, Vlabel1, "VenEqpTrak!VenEqp_partName", "VenEqp_vendorpartCost")
                 
                 VtotalUknown = VtotalUknown + @tomoney(vvencost)


                 


                 Vline = Vlabel + "  -  " + Vvendor + "  -  " + VvenPartNum + "  -  " + FormatMoney(VvenCost)




                 
                 eqp_partsUnknown =  eqp_partsUnknown + @newline() + vline
                 Eqp_UknownNeedCost =  VtotalUknown
                 Eqp_TotalCost = Eqp_TotNeedCost + Eqp_UknownNeedCost
                 }


       }
     UnSetThisElement()
     Throwfocus(nothing)


End Subroutine



Subroutine StatChoices() // choices for equipment itiem status

var Str1 as string
var Str2 as string

// If @isblank(ThisElement)
     
           If ThisElement <> "Complete"



     Then
{
           PopupSelectPosition(4, @XPos(thiselement), @YPos(ThisElement) )
           str1 = "Need;Unknown;Complete"
           Str2 = @popupmenu(Str1, "please Select")
           ThisElement =Str2
}


End subroutine
« Last Edit: Mar 1st, 2007 at 4:12pm by BOBSCOTT »  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #10 - Mar 1st, 2007 at 5:37pm
Print Post Print Post  
BOBSCOTT wrote on Mar 1st, 2007 at 6:51am:
Charlie,

I am not sure I interpreted your explanation correctly but I might be doing something very similar. ...............


Robert,

You are doing something very similar to what we will eventually have running here...You have email.  Thanks for the reply.

Charlie
  
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #11 - Mar 1st, 2007 at 6:31pm
Print Post Print Post  
Robert,

I received your screenshots...WOW! Shocked Smiley Smiley Smiley

Amazing.  I'm going to be working on a few things in between "other work".  I'll keep you posted on how things go...a little at a time Wink  Many thanks for sharing!

Charlie
  
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #12 - Mar 5th, 2007 at 6:52pm
Print Post Print Post  
Undecided
Such a simple task, I'm sure...I've taken from examples to try to get programming to run that will total my Parts amount at Search/Update and post that total in filed named "TotalRepairs".  I've tested the code, no errors, but the total is not visible.  My excuse is that I never did any programming in Q&A (or any other software programs).  Bob Hansen's examples are som that we will want to add to our database eventually-amazing stuff; I'd better be able to do some simple programming tasks first.  I will learn to program Sesame...even if I have to take a leave of absence and lock myself in a room for a few weeks Roll Eyes

My subform settings are:

Display Form:  Repair1
Subrecords are "relational" by serial number of my equipment.
Subrecord Field Name: Repair_Subform
Layout Element is: TotalParts

The subform is displayed in Table View on the Parent Form.

We'd like the Parts total (money) to display on the fly during search/update...I've run the code in Global Code with no errors, but no total in my Layout Element

Code I've been attempting is:


SubRoutine CalculateParts()

Var vCount as Int
Var vTotal as Double
Var vRecord as Int


vRecord = 1  //Record variable

vCount = @FormResultSetTotal("Repair_Subform")

If vCount > 0 Then
{
    While vRecord <= vCount
    {
          //Loop through subrecords adding up totals.
          vTotal = vTotal + @TM(@FormFieldValue("Repair_Subform", "Parts", vRecord))
          vRecord = vRecord + 1
    }

    //Place values in elements on form
    TotalRepairs = vTotal
}
End SubRoutine


  
Back to top
 
IP Logged
 
Ben
Lantica Support
*****
Offline



Posts: 218
Joined: Apr 7th, 2005
Re: Running Total for subform lines
Reply #13 - Mar 5th, 2007 at 7:45pm
Print Post Print Post  
charliebrown wrote on Mar 5th, 2007 at 6:52pm:

My subform settings are:

Display Form:  Repair1
Subrecords are "relational" by serial number of my equipment.
Subrecord Field Name: Repair_Subform
Layout Element is: TotalParts

The subform is displayed in Table View on the Parent Form.

We'd like the Parts total (money) to display on the fly during search/update...I've run the code in Global Code with no errors, but no total in my Layout Element

Code I've been attempting is:


SubRoutine CalculateParts()

Var vCount as Int
Var vTotal as Double
Var vRecord as Int


vRecord = 1  //Record variable

vCount = @FormResultSetTotal("Repair_Subform")

If vCount > 0 Then
{
     While vRecord <= vCount
     {
          //Loop through subrecords adding up totals.
          vTotal = vTotal + @TM(@FormFieldValue("Repair_Subform", "Parts", vRecord))
          vRecord = vRecord + 1
     }

     //Place values in elements on form
     TotalRepairs = vTotal
}
End SubRoutine


It appears that you are using the name that you assigned to the subform, rather than the name of the form being displayed as a subform.  You will want to use "Repair1" in place of "Repair_Subform".

-Ben
  
Back to top
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #14 - Mar 5th, 2007 at 10:38pm
Print Post Print Post  
Quote:
charliebrown wrote on Mar 5th, 2007 at 6:52pm:

My subform settings are:

Display Form:  Repair1
Subrecords are "relational" by serial number of my equipment.
Subrecord Field Name: Repair_Subform
Layout Element is: TotalParts

The subform is displayed in Table View on the Parent Form.

We'd like the Parts total (money) to display on the fly during search/update...I've run the code in Global Code with no errors, but no total in my Layout Element

Code I've been attempting is:


SubRoutine CalculateParts()

Var vCount as Int
Var vTotal as Double
Var vRecord as Int


vRecord = 1  //Record variable

vCount = @FormResultSetTotal("Repair_Subform")

If vCount > 0 Then
{
    While vRecord <= vCount
    {
          //Loop through subrecords adding up totals.
          vTotal = vTotal + @TM(@FormFieldValue("Repair_Subform", "Parts", vRecord))
          vRecord = vRecord + 1
    }

    //Place values in elements on form
    TotalRepairs = vTotal
}
End SubRoutine


It appears that you are using the name that you assigned to the subform, rather than the name of the form being displayed as a subform.  You will want to use "Repair1" in place of "Repair_Subform".

-Ben


I understand completely Ben...thanks for setting me straight there.  Still not getting any data to appear in the TotalRepairs field after editing programming...
I have a link to a few screenshots of the database, subform values...

http://pg.photos.yahoo.com/ph/cspedden/album?.dir=f7f4scd&.src=ph&store=&prodid=...

Programming changes were in Global Code as follows:


SubRoutine CalculateParts()

Var vCount as Int
Var vTotal as Double
Var vRecord as Int


vRecord = 1 //Record variable

vCount = @FormResultSetTotal("Repair1")

If vCount > 0 Then
{
While vRecord <= vCount
{
//Loop through subrecords adding up totals.
vTotal = vTotal + @TM(@FormFieldValue("Repair1", "Parts", vRecord))
vRecord = vRecord + 1
}

//Place values in elements on form
TotalRepairs = vTotal
}
End SubRoutine

Of course the maddening part of this is that it's more than likely something incredibly simple that's wrong... Embarrassed
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Running Total for subform lines
Reply #15 - Mar 5th, 2007 at 10:49pm
Print Post Print Post  
Let's make sure your code is running and get an idea of what it's doing. We'll do this by interspersing some WriteLn calls that will pop up a little window with some information. Here is your code with the WriteLns in it.

SubRoutine CalculateParts() 

Var vCount as Int 
Var vTotal as Double 
Var vRecord as Int 

WriteLn("STARTING CALC")

vRecord = 1 //Record variable 

vCount = @FormResultSetTotal("Repair1") 

WriteLn("GOT " + @Str(vCount) + " RECORDS")
If vCount > 0 Then 

While vRecord <= vCount 

//Loop through subrecords adding up totals. 
vTotal = vTotal + @TM(@FormFieldValue("Repair1", "Parts", vRecord)) 
WriteLn("vTotal: " + @Str(vTotal))
vRecord = vRecord + 1 


//Place values in elements on form 
TotalRepairs = vTotal 

WriteLn("DONE CALC")
End SubRoutine


Run this on a record.

If you don't ever see "STARTING CALC" then you are never running the Subroutine. Make sure you are actually calling it someplace.

If GOT # RECORDS always says 0, your form name is wrong.

If vTotal is always 0, you don't have an element called Parts in the subform.

Let us know what happens.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #16 - Mar 5th, 2007 at 11:17pm
Print Post Print Post  
Smiley Thanks boss...I'm such a rookie... Roll Eyes

I'll run this as soon as I get home.  It's a little less hectic there and I'm less likely to type lines in the code such as, "vRecord = 1 // no i haven't had lunch yet @FormResultSetTotal i need to finsh this leave me alone and put the phones on send for the rest of the week End Subroutine..."

I'll post the result...sincere thanks!



  
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #17 - Mar 6th, 2007 at 4:43pm
Print Post Print Post  
Hammer wrote on Mar 5th, 2007 at 10:49pm:
Let's make sure your code is running and get an idea of what it's doing. We'll do this by interspersing some WriteLn calls that will pop up a little window with some information. Here is your code with the WriteLns in it.

SubRoutine CalculateParts()  

Var vCount as Int  
Var vTotal as Double  
Var vRecord as Int  

WriteLn("STARTING CALC")

vRecord = 1 //Record variable  

vCount = @FormResultSetTotal("Repair1")  

WriteLn("GOT " + @Str(vCount) + " RECORDS")
If vCount > 0 Then  
{  
While vRecord <= vCount  
{  
//Loop through subrecords adding up totals.  
vTotal = vTotal + @TM(@FormFieldValue("Repair1", "Parts", vRecord))  
WriteLn("vTotal: " + @Str(vTotal))
vRecord = vRecord + 1  
}  

//Place values in elements on form  
TotalRepairs = vTotal  
}  
WriteLn("DONE CALC")
End SubRoutine


Run this on a record.

If you don't ever see "STARTING CALC" then you are never running the Subroutine. Make sure you are actually calling it someplace.

If GOT # RECORDS always says 0, your form name is wrong.

If vTotal is always 0, you don't have an element called Parts in the subform.

Let us know what happens.


Good morning:)

Here is a link to various screenshots of my databases (Inventory and Repair) and screenshots of the Program Editor window (Code tests without errors, btw)...

http://pg.photos.yahoo.com/ph/cspedden/album?.dir=/e5c3scd&.src=ph

I've run in preview mode, saved it, reconciled it, run multiple search/update, run in add data mode...I'm not getting a WriteLn window popping up or any data in my TotalRepairs field.   Huh  I'm obviously missing something that's preventing code from running properly, even though it's error free when I test it.

An double shot of espresso is in order me thinks... Undecided
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Running Total for subform lines
Reply #18 - Mar 6th, 2007 at 5:32pm
Print Post Print Post  
I suspect you are not actually calling the Subroutine at all.  You can write a subroutine, but it does not actually run until you tell it to. Try this:

1. If your subroutine is not in the GLOBAL CODE area of your Mass Update, put it there.

2. In the Mass Update event for any other element in your Mass Update, type:
Code
Select All
CalculateParts() 



See how that does.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Running Total for subform lines
Reply #19 - Mar 8th, 2007 at 8:36pm
Print Post Print Post  
I am finally getting to work with the original code posted by Bob Hansen.  When I test the code, I get the following error:

Error while parsing module "  GLOBAL CODE :srCalculateTotalCost":
Symbol expected [}], symbol found [Identifier].
Line 29, position 17: [Identifier:RunningTotal]
  RunningTotal =  vRunningTotal
                       ^

Any suggestions?

Thanks
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Running Total for subform lines
Reply #20 - Mar 8th, 2007 at 8:46pm
Print Post Print Post  
It looks like you lost a curly brace someplace. It says it was looking for } and found RunningTotal instead. Look for missing curly brace, parentheses, quote marks... Stuff like that.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Running Total for subform lines
Reply #21 - Mar 8th, 2007 at 8:50pm
Print Post Print Post  
Thanks.

That's what I thought it was telling me.  However, I am not very good with programming.  I could not see a missing curly brace.  Here is my code :

SubRoutine srCalculateTotalCost()

/*
Purpose is to calculate a Total for parent form and RunningTotal for subform
Structure is Parent Form name is frmParent, subform with natural link is named sfrmChild.
Currency element on frmParent, named TotalCost to show total of Cost for all lines in subform.
Currenty element on sfrmChild named Cost to show Cost for that individual record.
Unbound element on sfrmChild, named RunningTotal, ReadOnly, to show running total for Costs for all records in the subform.
Defined as subroutine so can be called on sfrmCost-OnExitElement and on sfrmCost-OnFormExit, 
Is also defined and called on frmParent-OnFormEntry.
*/
//==============================================

var vCount As Int            //Count number of records in subForm
var vLoop As Int            //While loop counter
var vTotal As Double            //Total Cost for all records in subForm
var vRunningTotal As Double      //Running total of Cost

If @FormIsStandalone() = 0 Then {

     vTotal = 0
     vRunningTotal = 0
     vCount = @FormResultSetTotal("sfrmChild")
     
     vLoop = 1
     While vLoop <= vCount {
           vTotal +=    @ToNumber(@FormFieldValue("sfrmChild","Cost",vLoop))
           vRunningTotal = vTotal
           RunningTotal = vRunningTotal
           vLoop += 1
           }

     If vTotal > 0 Then {
           FormFieldValue("frmParent","TotalCost",0,vTotal)
           }
     }

End SubRoutine
//======================================


  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Running Total for subform lines
Reply #22 - Mar 8th, 2007 at 9:17pm
Print Post Print Post  
Do you have an element on your form called RunningTotal? If not, you will get this error.
  

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


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Running Total for subform lines
Reply #23 - Mar 8th, 2007 at 9:30pm
Print Post Print Post  
Hold on.....

I just pasted my code from the forum into a test database and I am getting the same error.  Let me find out what is wrong.

Again, the code I submitted was an edited version of a working application, I must have missed something.  As Erika points out, it would seem to a missing or extra } or similar item.    Stay tuned......

Edited......
RunningTotal is an element on sfrmChild, it does not exist on frmParent.

So this needs to be modified, by adding an invisible RunningTotal element to frmParent, or making the line in code a conditional line..........    Stay tuned......

Edited again.....
GOT IT !
As noted above the code is referencing a missing element.  At one time I was calling the function from both frmParent and frmChild.  I forgot to remove the call from frmParent in my instructions.

THE FIX:
Do not call this function from frmParent.  Only call from elements on sfrmChild.


I have also modified my original posting to include that element in the set up instructions.

Thanks CapitalG for pointing out the problem.
Sorry about any confusion.   Embarrassed


« Last Edit: Mar 8th, 2007 at 10:31pm by Bob_Hansen »  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Running Total for subform lines
Reply #24 - Mar 9th, 2007 at 3:05pm
Print Post Print Post  
Grin Grin   Thanks!    Grin Grin
  
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #25 - Mar 12th, 2007 at 4:06pm
Print Post Print Post  
Bob_Hansen wrote on Mar 8th, 2007 at 9:30pm:
Hold on.....



Thanks CapitalG for pointing out the problem.
Sorry about any confusion.   Embarrassed



No need to apologize to me Bob...it's refreshing to have someone confusing me other than myself  Roll Eyes
  
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #26 - Mar 14th, 2007 at 5:33pm
Print Post Print Post  
Hammer wrote on Mar 6th, 2007 at 5:32pm:
I suspect you are not actually calling the Subroutine at all.  You can write a subroutine, but it does not actually run until you tell it to. Try this:

1. If your subroutine is not in the GLOBAL CODE area of your Mass Update, put it there.

2. In the Mass Update event for any other element in your Mass Update, type:
Code
Select All
CalculateParts() 



See how that does.


Thank you!!!  That was it Erika.   Runs great-all data seems to be intact...still testing a bit.  One question:

I've tried to have the value in the TotalParts field show as "money" with 2 decimal points, "$" sign...getting errors when I test the code.  I tried going back and creating a new LE bound to money on a copy of the design form...doesn't run. 

This is the line I was trying to add to my above code:

TotalRepairs \#$#,#0.00

Is there something I'm missing?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Running Total for subform lines
Reply #27 - Mar 14th, 2007 at 5:52pm
Print Post Print Post  
charliebrown wrote on Mar 14th, 2007 at 5:33pm:
This is the line I was trying to add to my above code:

TotalRepairs \#$#,#0.00

Is there something I'm missing?


The line you show above will do something if you enter it as a switch in a mergefield in a Microsoft Word document. Sesame, however, considers it to be gobbledygook! (That's the technical term.)

Get rid of that line and just format your element as Money with 2 decimal places.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #28 - Mar 14th, 2007 at 7:16pm
Print Post Print Post  
Hammer wrote on Mar 14th, 2007 at 5:52pm:
charliebrown wrote on Mar 14th, 2007 at 5:33pm:
This is the line I was trying to add to my above code:

TotalRepairs \#$#,#0.00

Is there something I'm missing?


The line you show above will do something if you enter it as a switch in a mergefield in a Microsoft Word document. Sesame, however, considers it to be gobbledygook! (That's the technical term.)

Get rid of that line and just format your element as Money with 2 decimal places.


Gobbledygook is nothing I want that in my programming  Shocked  Thank you Erika...again... Embarrassed
  
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #29 - Mar 14th, 2007 at 8:59pm
Print Post Print Post  
Hammer wrote on Mar 14th, 2007 at 5:52pm:
charliebrown wrote on Mar 14th, 2007 at 5:33pm:
This is the line I was trying to add to my above code:

TotalRepairs \#$#,#0.00

Is there something I'm missing?


The line you show above will do something if you enter it as a switch in a mergefield in a Microsoft Word document. Sesame, however, considers it to be gobbledygook! (That's the technical term.)

Get rid of that line and just format your element as Money with 2 decimal places.



As ordered...garbage in, garbage out  Sad

And of course, when everything is as it should be...wonderous things happen  Grin  Thank you Erika, Bob and everyone...  SmileyRoll Eyes

http://us.a2.yahoofs.com/users/456dd110z2ef31adb/e5c3scd/__sr_/df5bscd.jpgphQJG....

http://pg.photos.yahoo.com/ph/cspedden/detail?.dir=e5c3scd&.dnm=df5bscd.jpg&.src...
  
Back to top
 
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #30 - Mar 16th, 2007 at 3:32pm
Print Post Print Post  
Hammer wrote on Mar 14th, 2007 at 5:52pm:
charliebrown wrote on Mar 14th, 2007 at 5:33pm:
This is the line I was trying to add to my above code:

TotalRepairs \#$#,#0.00

Is there something I'm missing?


The line you show above will do something if you enter it as a switch in a mergefield in a Microsoft Word document. Sesame, however, considers it to be gobbledygook! (That's the technical term.)

Get rid of that line and just format your element as Money with 2 decimal places.


In my last post, everything runs fine...what I must have not figured out is when I add a NEW repair record, do I have to do a mass update every time in order for the "RepairTotal" to re-calculate?...or can something be placed in the Global Code that will do that automatically?

Thanks again Erika...I'll get better at programming...or else! Smiley
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Running Total for subform lines
Reply #31 - Mar 16th, 2007 at 5:26pm
Print Post Print Post  
Use an element (the element that you are totaling is probably best) in the subform to call the subroutine that is in global code.  Call subroutine from On Element Exit/Change, or OnFormExit.  Do not need to do a Mass Update.
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #32 - Mar 16th, 2007 at 6:06pm
Print Post Print Post  
Bob_Hansen wrote on Mar 16th, 2007 at 5:26pm:
Use an element (the element that you are totaling is probably best) in the subform to call the subroutine that is in global code.  Call subroutine from On Element Exit/Change, or OnFormExit.  Do not need to do a Mass Update.  



I'm on it...thank you again Robert.  I need to take a 90 day leave of absence just to concentrate on Sesame...I do love this program...the possibilites are virtually endless.
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Running Total for subform lines
Reply #33 - Mar 16th, 2007 at 9:24pm
Print Post Print Post  
Quote:
I'm on it...thank you again Robert.


Bob Hansen says you are welcome.
Robert (Scott) also deserves thanks too, along with Erika , Ben, CapitalG, (got em all?)

Quite a resemblance between you and Robert Scott, are you twins?
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
charliebrown
Junior Member
Members
**
Offline


Beware...novice at large

Posts: 97
Location: Sacramento, CA
Joined: Apr 19th, 2006
Re: Running Total for subform lines
Reply #34 - Mar 16th, 2007 at 10:50pm
Print Post Print Post  
Bob_Hansen wrote on Mar 16th, 2007 at 9:24pm:
Quote:
I'm on it...thank you again Robert.


Bob Hansen says you are welcome.
Robert (Scott) also deserves thanks too, along with Erika , Ben, CapitalG, (got em all?)

Quite a resemblance between you and Robert Scott, are you twins?



How could I possibly get you two mixed up?...I'm just sitting here editing code, replying to emails, Nextel 2-way in one hand, land line in the other...Tongue...Sorry Robert...

Thanks Bob!!! Smiley)))))))))))))))
  
Back to top
 
IP Logged