Home

Macros and Programming

 

CardboxForumsMacros and Programming > "Automatic addition of two field output into a third field"

Automatic addition of two field output into a third field

Automatic addition of two field output into a third field

Current user: [none]
Register / Log In · Help

Posted By Post

greenkeeper

19-Nov-2015 10:20

i have attempted to go through the manual and found the script that should work and put in the field details that i wanted, and firstly put it to show in a message box. but this wouldn't work at all.

fields("Cost 12") + 0 + Fields("as total cost")
next
'the proof:
Msgbox

Any help?

greenkeeper

19-Nov-2015 11:12

ok so have found a way round this but it will only let me do this whilst editing a recording is there any way i can change this?
here is the code
Fields("AS_INC_TREAT")=Fields("COST_12")+0+Fields("AS_TOTAL_COST")
Command cmdEnd+cmdControl
Typetext ".00"
SaveRecord

the .00 is to enable a full price

bert

19-Nov-2015 11:17

Try this (did not test, but this should work):

set arec = ActiveRecord
if editing = true then SaveRecord
arec.edit
arec.Fields("AS_INC_TREAT")=NumberToCardbox(arec.Fields("COST_12")+arec.Fields("AS_TOTAL_COST"))
arec.save

greenkeeper

19-Nov-2015 11:21

nope same again can only be during editing

greenkeeper

19-Nov-2015 12:24

i don't mind overly if this is the case, but with regards to auto creation of data in two fields, i have about 6 fields which the data is all generated from one original field, COST_12, i have experimented with just repeating the above code in the its various formats, whether is be multiplication or addition. Singularly they work fine but to make life that much easier is there not a way that i could combine these all on to the one macro?

as i say i have tried but it always come back with an error

greenkeeper

19-Nov-2015 12:46

i have go this code
Fields("LJCOST")=Fields("COST_12")*2.5
Command cmdEnd+cmdControl
Typetext ".00"

Fields("CGCOST")=Fields("COST_12")*4
Command cmdEnd+cmdControl
Typetext ".00"

Fields("WLCOST")=Fields("COST_12")*1.5
Command cmdEnd+cmdControl
Typetext ".00"

Fields("ML_COST")=Fields("COST_12")*2
Command cmdEnd+cmdControl
Typetext ".00"

Fields("AER_COST15")=Fields("COST_12")*3
Command cmdEnd+cmdControl
Typetext ".00"

Fields("SCAR_COST15")=Fields("COST_12")*4
Command cmdEnd+cmdControl
Typetext ".00"

Fields("AS_TOTAL_COST")=Fields("SCAR_COST15")+0+Fields("AER_COST_15")
Command cmdEnd+cmdControl
Typetext ".00"

Fields("AS_INC_TREAT")=Fields("COST_12")+0+Fields("AS_TOTAL_COST")
Command cmdEnd+cmdControl
Typetext ".00"
SaveRecord

however that adds lots of .00 to whichever field the cursor is in and then crashes halfway through. It almost needs a way of completing one and then moving on to the next. I need the .00 to be added to each individual field. if possible :)

greenkeeper

19-Nov-2015 13:56

Yea ok after doing more reading have got that to work. is there a line of code that would for example if this number in field x end in .50 round down to .00?

bert

19-Nov-2015 14:07

- If you want to write something in a record: you have to edit...
- What goes wrong in my example?
- What tells the error message you in your macro?

greenkeeper

19-Nov-2015 15:09

on reflection that code that says can only be done while editing will only ever be used while editing as you mentioned, so thats fine. Am looking into the round issue now and i dont understand at all.

greenkeeper

19-Nov-2015 15:20

rounding issue thats meant to say sorry, from what ive read i need to use the fix(x) function but i cant see how i can write that in to the desired to field for it to take effect

bert

19-Nov-2015 15:28

You could first try my example..
If in that example a message appears that the record is edited, then is in **some** Cardbox window, that record in edit mode.
How many Cardbox windows you have opened? In one of them, that record is in edit mode!
In some incidental cases a Cardbox macro only thinks a record is in edit mode. Close Cardbox, an open it again will fix this.

This is what happens in the example:
'if the record on your screen is in edit mode, save it first (we do not want to write using the macro in this mode).
if editing = true then SaveRecord
'make an object of the current record
set arec = ActiveRecord
'edit that object
arec.edit
'write in the oject the result of the calculation. NumberToCardbox will force you are busy with numbers.
arec.Fields("AS_INC_TREAT")=NumberToCardbox(arec.Fields("COST_12")+arec.Fields("AS_TOTAL_COST"))
'save your object
arec.save
In field AS_INC_TREAT you will see the result.

If that works, then we gonna think how to fix on .50 or .00.

greenkeeper

19-Nov-2015 15:37

yep that works fine now
thanks now for that rounding down to.00 issue?

greenkeeper

19-Nov-2015 15:38

one query what is with the arec bit? as i haven't seen that in any previous codes?

bert

19-Nov-2015 16:16

arec = is name for the current record given by this line :
set arec = ActiveRecord

bert

19-Nov-2015 16:22

You want:
 .01 until .50 -> .50
and
 .51 until .00 -> .00?

or b.e.
 .01-.25 -> .00
 .26-.74 -> .50
 .74-.00 -> .00?

greenkeeper

20-Nov-2015 09:07

the only other number it would be is .50 or .00
but the .50 is always rounded down to .00 so it would be .01 until .50 -> .00

And is that just line of code needed?

bert

20-Nov-2015 09:48

I understand:
.01 until .49 will increase to .50
.51 until .99 will increase to .00 (and plus 1 to the integer)
Right?

greenkeeper

20-Nov-2015 09:55

There is only one section that it would be needed for this is the code in place to generate the field value

GoToField("LJCOST")
Fields("LJCOST")=Fields("COST_12")*2.5
Command cmdEnd+cmdControl
Typetext ".00"

the .50 will only occur when the cost 12 field is an odd number. and the .50 is always rounded down in our business so only 0.50 to .00 interger below
e.g 25.50 needs to be just 25.00

bert

20-Nov-2015 10:20

Then is it simple:
Fields("LJCOST")=Int(Fields("COST_12")*2.5) & ".00"

greenkeeper

20-Nov-2015 10:24

Ah i am a fool thank you, so simple when someone knows what they are doing you shows you

thank you again

Quick Reply

Please log in or register before trying to post a reply.

 
© 2010 Cardbox Software Limited   Home