Use the Code property in the Field Properties Panel to set up a short program that will run when data is entered into the field. (Starting with Panorama X 10.1, you can also set up code to run immediately when a field is clicked in the Data Sheet, see Running Code when a Field is Clicked at the bottom of this page.)

In this example, the code checks the value entered into the field and displays a warning if it is too small or too large.

With this code in place, Panorama checks values entered into the A field.

Each field can have its own code.

When this code is running, Panorama automatically defers display of any database changes until the code is completely finished. Normally you won’t even notice this, but to learn more, see the showlater statement.

Calling a Named Procedure

If multiple fields share a significant amount of code, you can set up a named procedure (see Procedures) with the shared code and use the call statement to call it. For example, I could set up a procedure called checkFieldLimits to validate entered data against defined limits.

Notice that instead of using specific field names (A, B, C, etc.) this procedure uses «». The «» symbol can be used in any formula to access the value of the current field. Use of the «» symbol allows this procedure to work with any field without modification.

Now that this procedure is set up, we can call it from the code for each field.

This same subroutine can also be called from fields B, C and D, only changing the limits passed as parameters as necessary. Then if you ever need to modify the limit checking code, you only have to make that change in one place, not over and over again for each field. For example, the message statement could be changed to nsnotify. With this change, any entry of an out-of-range value will cause a notification instead of an alert that has to be dismissed.

In this example the code in the field has been completely replaced by a single call statement, but that’s not necessary. You can mix in whatever code you want into the field’s code, including multiple call statements if necessary.

Automatic Code vs. Automatic Field Calculations

Only the code associated with the field being edited will run. If Automatic Field Calculations cause other fields to be modified, the code associated with those other fields will not run. For example, suppose code has been associated with the Total field, as shown here. You might think that this code would run automatically when you modify data in A, B, C or D, since this causes a formula to run that modifies the Total. However, this doesn’t happen – only the code in the edited field (in this case A) will run.

If you want the code for another field to also run, you must add the runfieldcode statement, like this. This statement has one parameter, the name of the field with the code you want to run.

Note: When a field’s code is run with the runfieldcode statement, the current field is still the original field. So if the code for the target field uses «» to get the current field’s value, it won’t work.

If you want to run the code for multiple fields, you must use runfieldcode multiple times, like this:

localparameters minValue,maxValue
if «» < minValue or «» > maxValue
    nsnotify "Invalid Value",
        "TEXT","Value must be from "+minValue+" to "+maxValue
endif
runfieldcode "Total"
runfieldcode "Avg"

Running code when ANY field changes

As an alternative to setting up code for specific fields, you can also set up a special procedure named .ModifyRecord that will be run whenever any field is modified. See Implicitly Triggered Procedures for more information.

Running Code when a Field is Clicked

Starting with Panorama X 10.1, you can add a special click: label to the code and specify actions that will run immediately when a field is clicked in the Data Sheet. Here is an example that overrides what happens when you click on the Use field in this database.

With this code, clicking on the Use field will toggle between Public and Private.

Note: You can still press the Tab key after clicking and manually edit the cell (unless you have un-checked the Editable option).

A common application is to toggle between blank and a value, like this.

This code takes advantage of the the fact that «» means “the current field name”, and also uses the ?( true-false function. You can copy and paste this code into any field you want.

click:
    «»=?(«»="","Y","")
    return

You don’t have to use the letter "Y", you can use any text you want. A handy trick is to use an emoji, like this:

You can set up a toggle for three or more choices, but if the list gets long you might want to use a pop-up menu. Here is code that will do that for the Type field in the example airport database:

return

click:

local choice
let choices=commatocr("Airport,Balloonport,Gliderport,Heliport,SeaplaneBase,Ultralight")
popupclick choices,"",choice
if choice<>""
  Type=choice
endif

And here is the code in action.

You may have noticed that the code in all of these examples begins with a return statement, and then the click: label. This return statement will be run if the user uses the Tab key to open the standard editor window, then presses Return or Enter. If you want any other actions to run in this situation, put them above the return statement. The return statement ensures that the click code doesn’t run when the editing window closes.

if «»="Y" or «»=""
    return
endif
message "Must be blank or Y, no other values allowed"
«»=""
return

click:
    «»=?(«»="","Y","")
    return

Alternatively, you can add a modify: label to the code. If Panorama sees this label, it will run the code afterwards when the editing window closes. Here is an example that includes both the click: and modify: labels. This works exactly the same as the previous example.

click:
    «»=?(«»="","Y","")
    return

modify:
    if «»="Y" or «»=""
        return
    endif
    message "Must be blank or Y, no other values allowed"
    «»=""
    return

Earlier on this page it was described how the call statement can be used to move the automatic code into a separate, named procedure (see Calling a Named Procedure above). This works even if the code contains the special click: and modify: labels. If the call statement is used by itself (no other statements in the code), Panorama will look in the named procedure for the special labels and run the code as needed.

Automatic Code and the Run Loop

The operation of automatic field code is tightly intertwined with the system’s run loop for dispatching events. See Understanding the Run Loop for more on this topic.


See Also


History

VersionStatusNotes
10.1UpdatedPanorama can now run code immediately when a field is clicked in the Data Sheet.
10.0UpdatedCarried over from Panorama 6.0, but now formulas and code are separate.