120: Extra Credit: Running Code Automatically

Previous: 119: Automating Tasks with Procedures

The Action menu isn’t the only way code can run. There are many situations where code can run automatically in response to different events, including opening a database, adding or removing a record, or editing data.

Here is a short program designed to run in response to a phone number being edited. Using the formatphone( function, it automatically formats the phone number in the format `(999) 999-9999` format. If the area code is missing, it automatically adds a default area code (in this case 714). If the number contains less than 7 digits, it displays a notification.

    local digits
    digits = striptonum(Phone)
    if length(digits) = 0 or length(digits)>10
        return
    elseif length(digits) = 7
        digits = "714"+digits
    endif
    if length(digits) = 10
        Phone = formatphone(digits)
        return
    endif
    if length(digits)<7
        nsnotify "Bad Phone Number?","text","Only "+pattern(length(digits),"# digit~")
    endif

To run this code in response to a phone number being entered, place this code in the Code section of the Field Properties Panel for the Phone field.

This code will run automatically whenever a phone number is entered into the database (or modified).

Next: 121: Extra Credit: Importing and Cleaning Up More Names

See Also

- ?( -- allows a formula to make a true/false, yes/no decision.
- Automatic Field Code -- running a short program when data is entered into a field.
- Code -- basics of programming code.
- endif -- marks the end of statements controlled by a previoius if statement.
- formatphone( -- formats a US phone number using the standard *(aaa) nnn-nnnn* format.
- if -- decides what code to execute next.
- length( -- counts the number of characters in a text value (also called a string).
- nsnotify -- delivers a notification.
- pattern( -- converts a number into text using a pattern.
- return -- ends a subroutine.
- striptonum( -- removes everything but numeric digits from a text item.

History

Version | Status | Notes
10.0 | Updated | Carried over from Panorama 6.0, with updates.