Runfieldcode problem at startup

I have three integer fields, and a Total field. The three fields have Automatic Field Code that sums them and places the result in the Total field. It has been working, but now a problem is occurring when the file is first launched.

All four fields are displayed in a form, and a popup button calls a proc that looks up a value and places it into one of the integer fields. The proc then uses a runfieldcode statement to trigger the Field Code.

When the file is first opened, with the form in front, the popup works, the looked-up value appears in the first integer field, but the Auto Field Code refuses to run. The Total does not change, in my form, nor in the Data Sheet.

This continues for any record, until I click into any one of the three Text Editors associated with the integer fields. As soon as I do that, the Field Code runs immediately, and also thereafter when the popup triggers its procedure, and the Total is updated as expected.

And, I just discovered that running my Duplicate Record procedures, which clear some fields (but not the integer fields) causes the problem to reappear. The Auto Code does not run, even after changing records, until I click once in one of the integer field TEs. Then everything works fine, until I duplicate another record.

I would have done this by using the Formula pane for the field properties, so I would have put in the Total field the formula [FieldName1]+[FieldName1]+[FieldName3]; of course, they are all number fields.

After changing (with a procedure) the value of FieldName1, 2 or 3, I would include the statements

FieldName1  //Put any field name that's in the formula
runfieldcalculations

The Total value will be recalculated.

I don’t know why your approach doesn’t work, but I have used my approach many times and it is quite reliable.

It’s very difficult to assist when you haven’t provided the actual code you are running.

However, I suspect that your code is somehow dependent on the current field, so it isn’t working unless you have somehow set the current field to a particular field. When you click on one of the integer fields, the current field is set and then the code works. You could either modify the code so that it isn’t dependent on what the current field is, or you would need to add a field statement before using runfieldcode.

field "Some Field"
runfieldcode "Some Field"

Thank you Jim, that fixed it, and apologies for not including my code (see below). And thanks to CooperT as well, it is good to remember that there are many ways to accomplish a task, and we should try alternatives when we get stuck!

I thought that since the runfieldcode statement specifies a field, we wouldn’t need to also make it active ahead of time. Is there a reason why it doesn’t just make it active automatically, if that’s what is needed? But, since the auto code specifies exactly what to do, and the runfieldcode specifies which auto code to run, why does it need to be active?

I guess the takeaway is that auto field code isn’t exactly equivalent to placing that same code (in this case an assignment) into a procedure at the location of a runfieldcode statement??

I added the field statement as you said, and it now works as expected:

field «Cost1»
runfieldcode «Cost1»

My three fields are Cost1, Cost2, and Cost3, and each one has the Auto Code in the data sheet to sum them into my Total field:

TotCost=sum(“CostΩ”)

Yes, it is exactly equivalent.

Now that I know the code in your field, I made a database exactly like yours and it worked.

So here’s the problem. You used the code:

runfieldcode «Cost1»

The correct code is

runfieldcode "Cost1"

The parameter the runfieldcode statement needs is the name of the field, not the value of the field. You have to use the quotes to pass the name instead of the value. Note that the documentation for runfieldcode mentions this, though there is no example given.

Once the code is corrected, the field statement is not required. (In fact, I don’t understand how your code could possible work even with the field statement.)

Thank you again, Jim, for your testing and explaining. I am especially happy to hear that the auto-code is equivalent to code inside a procedure, that is much more logical! I was very concerned about trying to remember if they weren’t.

My code is working well now!