Assigning Field Options
In addition to establishing fields and assigning data types, you may assign various options to your fields as well. These range in function from managing auto-entry of default data to validation checks and internal storage settings. They can vary for each field type.
After you have named a field and chosen its type on the Fields tab of the Define Database dialog box, click Create to save it to your database. You may then opt to apply further behaviors via the Options button on the right. The first set of options is the auto-entry behaviors.
Auto-Entry Field Options
When defining noncalculation fields in FileMaker Pro, you can choose to have data automatically entered into a field as records are created and/or modified. The applications for this can range from assigning default values to fields, to automatically reformatting data, or inserting values from other fields based on certain trigger events.
In some cases you might also want to prevent users from modifying these auto-generated values, such as when tracking a serial ID or applying a date you don't want adjusted afterward (see Figure 3.4).
Figure 3.4 FileMaker's auto-entry options allow you to define rules for automatically populating data into fields in your database.
Auto-entry data is inserted into a field based on some trigger event. The most common event is record creation: When a user clicks New Record, data can be prepopulated into the record and then be accessible for changes to be made. Each Auto-entry function has its own particular rules for what trigger event applies.
In addition to new record creation, other trigger events include record modification and modification of a particular field. We will cover both cases in the sections that follow.
Creation and Modification
The first two options on the Auto-Enter tab deal with tracking and applying certain values as a record is committed to your database. They behave essentially the same way, with Creation values being applied the first time a record is committed, and Modification values applied thereafter as it is subsequently modified (committed again).
Values that can be automatically entered include the current date, current time, current timestamp, current username (from the General tab of the Preferences dialog under the Edit menu), or current account name (the one entered by the user when logging in to the database).
Using this option allows you to auto-enter a number that increments every time a new record is added to the table. Often this is used to uniquely identify individual records in a table. The value can be generated either when the record is created or when it is committed. The difference is subtle: In the case of incrementing on creation, your number increments even if a user then reverts and effectively cancels a record's creation. The next record will then have skipped a number in your sequence. This doesn't have much of an effect on your database unless your business requires strict tracking of each serial number, even those voided. In those cases, choosing On Commit helps avoid spaces in the sequence.
It is possible to include text characters in addition to a number as the starting value if you want. This enables you to create serial numbers that look something like "a1, a2, a3, a4...." Only the rightmost numeric portion of the value is incremented; the text portion remains unchanged. If you do this, you will want to use a Text field to allow for the alphanumeric combination.
One of the common uses of auto-entry options is in establishing serialized key values, or IDs. This is a vital element of your database structure when you're working with more than one table, but regardless of how complex or simple your plans are, we encourage you to adopt some best practices.
For every table in your database, the first field you should create is a primary key or ID field. It is these IDs that uniquely identify each record in your database. There are several ways you could go about having the system establish unique IDs automatically; our recommendation in most cases is to use a serial number set to increment automatically.
We can't stress this practice strongly enough. If you ever want to tackle relational data structures, these serial IDs are a vital element in doing so. Further, if you ever export your data to another system or need to interact with other databases, having a key field that uniquely identifies each record in your database will guard against confusion or even possible loss of data integrity.
To create a serial key field, use the following steps:
- Define a number field. (It is generally advisable to use number-based serial keys, but it is possible to use text as well; the important thing is to make certain your keys are unique and unmodifiable.)
- Go into the Options for that field and select the Serial Number option.
- Click the Prohibit Modification of Value During Data Entry option at the bottom of the dialog. This is an important step: If you establish unique identifiers that your users can then override, you're risking the chance that they'll introduce duplicate IDs.
If you need an ID field for a business purpose (SKUs, student IDs, employee IDs from your organization, and so on), we recommend that you create separate fields for such cases. Generally, users should never need to access this serialized ID field, but you can opt to put it on a layout and allow entry in Find mode so that they can search if they choose.
For a full discussion of the use of keys (or match fields), see the discussion in "Working with Keys and Match Fields," p. 162.
Value from Last Visited Record
Used most often as a way to speed data entry when information is often repeated for groups of records, this function copies the value from a prior record into a given new record. Bear in mind that "Visited" means the last record in which you entered data. If you enter data in a record, and then view a second record without clicking into a field and activating it, it is the data from the first, edited record from which a new record obtains its value.
Here you may specify literal text for auto-entry. This is frequently used to set default states for field entry. For instance, in an Invoice table, you might have a text field called Status where you want to enter Not Paid as a default. Being a regular text field, the value is still fully modifiable by a user.
In addition to establishing a field as a calculation field, where its value will always be determined by its defined formula, it is possible to insert the results of a calculation into a field of another type—including a container field—by using an auto-entry option.
Furthermore, if you uncheck the Do Not Replace Existing Value for Field (If Any) option, the results of the calculation formula will be entered into the field, overriding any existing value, anytime a field referenced by the calculation changes.
Put differently, any field referenced in your calculation statement acts as a trigger—anytime that referenced field is updated, the calculation will be retriggered, and its result put back into the auto-entry field. In fact, the auto-entry field itself can act as such a trigger, if it's referenced in the auto-entry calculation. This enables you to dynamically reformat data as it is entered. One great example of this is a phone number field. You may always want phone numbers formatted as "(123) 456-7890" regardless of how a user entered the data. By using a calculated auto-entry function, you can reformat the phone number anytime it's modified.
For an example of this technique, refer to Figure 3.5.
Figure 3.5 By using a self-referencing calculation, FileMaker Pro is able to replace and correct data as it is entered by the user.
The actual calculation for this auto-entry option looks like this (returned as text):
Let ( [ //define variables: rawNumber = Filter (Phone_Number; "0123456789") ; length = Length (rawNumber); red = RGB (160;0;0); //set error flag for a phone number that's too short error = If ( length < 10 ; TextColor ("error: " & Phone_Number; red); "") ]; // now apply the phone formatting and return results If ( error ""; error; "(" & Left (rawNumber; 3) & ") " & Middle (rawNumber; 4; 3) & "-" & Middle (rawNumber; 7; 4) & // this condition tests for extra digits that we'll treat as an extension If ( length > 10; " x" & Middle (rawNumber; 11; length - 10); "") ) )
Here the calculation is written as though it were being applied to a field called Phone_Number. To get the results shown in Figure 3.5, you would need to either redefine the calculation for each of the three fields shown, or (better) define a single custom function that contains the bulk of the logic in the preceding listing, and call that function from the auto-entry calculation for each field you want to filter in this way.
To learn more about advanced calculation functions, including custom functions, see Chapter 14, "Advanced Calculation Techniques," p. 391.
To see a version of this function that works more flexibly and for international formats, see our companion book, FileMaker 8 Functions and Scripts Desk Reference.
This auto-entry option copies a value from a record in a related table into a field in the current table. Anytime the field controlling your association to the related record changes, FileMaker Pro updates the value in the lookup field.
For example, if a user enters a ZIP code into a given record, it's possible you could have another table and then auto-populate your city and state fields with the appropriate information.
When a user enters a ZIP code in the record in Figure 3.6, the City and State fields below are triggered to pull values from the ZipCodes table. An important fact to keep in mind is that FileMaker has copied the values from the ZipCodes table. If the source data changes or is deleted, this record remains unmodified until it is retriggered by someone editing the Zip Code field again.
Figure 3.6 Lookup functions work somewhat like relational data, but instead of displaying values from a related record, their information is copied and stored when a trigger event occurs.
Take special note that lookup auto-entry functions work just as all auto-entry functions do: They copy or insert information into a field. You are not displaying related information, nor are you controlling content by calculation. Thus, lookup values are not live links to related data. If you were to delete the records in the ZipCodes table in the preceding example, all your people records would remain untouched, preserving your city and state data.
This is an important distinction to understand, especially as we get into indexing later in this chapter. Consider an example for product prices: If you were to build an Orders database that tracked the prices of products, you'd want to store the price of each Order line item or product within the order itself. That way if your prices ever change, your historical orders will preserve their original prices.
To see how to create a lookup field, refer to Figure 3.7.
Figure 3.7 Often you'll want only exact matches, but in some cases you can use the closest value based on a comparison of the trigger values in your related table.
Remember that anytime your match field changes, your lookup refreshes. In this case, the auto-entry function does not act on record creation, but rather on committing/triggering.
When you're performing a lookup, it is possible to work with near matches, in addition to exact matches. In the case of the ZIP codes example, obviously you'd want only an exact match or you might end up with incorrect data. In a different case, however, you need not be so strict. Consider a scheduling system that automatically finds the closest available appointment: Enter a target date into a field, and the lookup function could return the closest match. Another application might be a parts database with units of measurement. You may not be able to find a .78 "wrench, but a .75" might work. This sort of requirement is easy to meet by using the Copy Next Lower Value setting.
How you set up your trigger values is important here. It's easy to compare numbers and come up with the next closest value. If your trigger field is text, FileMaker Pro uses ASCII value rules to compare and determine order.
For further discussion of lookups, see Chapter 6, "Working with Multiple Tables," p. 157.
Housekeeping Creation and Modification Fields
As a best practice, we also recommend that you create another set of fields in all tables that help track changes. Create a timestamp field and in the Auto-Enter options, choose Creation Timestamp. Define another for Modification Timestamp, and text fields for Creation and Modification Account Names.
These four fields tell you exactly when a record was created or modified and by whom (assuming that you assign an account to each individual person using your database). If you ever need to identify problem records for a given day range, time, or account, these fields allow you to do this. We strongly recommend that you add them every time you create a new table.
The only downside to following this practice is that additional storage space is required for this data; in this version of FileMaker Pro, this is unlikely to be a concern.
Storing correct and complete information is critical for generating accurate reports; establishing proper, expected conditions on which other functions and calculations are performed; and ensuring overall data integrity. Unfortunately, most data applications suffer from a chronic condition of having humans interacting with them; although some humans are worse than others, none is perfect. We all make mistakes.
As data is entered into FileMaker Pro, you may opt to apply one or more validation checks to test that certain conditions are met before allowing users to commit the record to your system. This can be as simple as ensuring that a field isn't empty, or as complex as making sure that an invoice doesn't contain multiple entries for the same product.
To review the various validation options available, see Figure 3.8.
Figure 3.8 These are common validation settings for a numeric key value meant to always remain unique and unmodified by users. Even if your users can't access a given field on any layout, it's still a good policy to validate.
This example demonstrates a common approach to ensuring that your primary keys are properly maintained. This may be overkill if you've enabled the Prohibit Modification of Value During Data Entry option on the Auto-Enter tab, but on the chance that a developer turns that option off for some reason, or that users import records into your database, this is a handy bit of insurance.
Importing records can circumvent your carefully designed field validation rules. For a full discussion, see Chapter 19, "Importing Data into FileMaker Pro," p. 567.
Validation Conditions and Failure
Field Validation simply tests whether one or more conditions, as defined in your Validation dialog, are false. If all validation tests are true, the user is not interrupted or prompted for action.
Figure 3.9 shows an example of what your users might see when validation fails.
Figure 3.9 The Yes option appears only if a user has the option to override the validation warning.
In this case, the check box allowing users to override has been left enabled, so they have the option to ignore the warning. When that function is disabled, the field does not allow bad data to be committed, and the system forces users to deal with the problem. They can choose either to revert the field to its previous state or to clear it.
When Validation Occurs
Validation occurs when users enter data manually into the field being validated; some validation will happen the moment the user leaves the field, whereas other validations are deferred until the user commits the record. Remember, however, direct entry is not the only way to get information into a field. You can also import records or use various script steps, such as Set Field().
Validation isn't triggered simply by clicking or tabbing into a field; a change needs to be attempted. And keep in mind that validation does not apply in cases in which users modify other, nonvalidated fields of a given record. A given field's validation check will be performed only when data in that specific field is changed.
At the top of the Validation tab of the Options dialog (refer to Figure 3.8), notice the Always and Only During Data Entry choices. The latter tests for validation conditions only when users modify the field in question. When the Always option is enabled, validation occurs during scripts and imports as well as during data entry.
If an import process attempts to write invalid data to a field, FileMaker Pro simply ignores the improper entry. The field remains unchanged and your data is not imported. You will see a note in the Import Records Summary dialog listing how many errors were encountered.
In the case in which the Only During Data Entry option is used, that improper data would be inserted into your database.
If you get trapped in a series of validation dialogs, refer to "Validation Traps" in the "Troubleshooting" section at the end of this chapter.
Storage and Indexing
Field storage and indexing options are found on the Storage tab in your Field Options dialog; these options control how FileMaker Pro indexes each field in order to speed up searches and sorts and form relationships.
A developer can designate a field to have global storage on the Storage tab of the Field Options dialog. Commonly fields with this option are simply referred to as global fields, and collectively they're usually referred to as globals. Global fields exist independently from any specific record in the database and hold one value per user session.
Global fields are often used by developers to establish special relationships or to display unchanging information, such as interface graphics or field labels, across multiple records and layouts.
One vital element to learn is when data is committed and stored for globals: In a single-user environment, any change to a global field is permanent and is saved across sessions. In other words, the next time you open your database, whatever value you last entered into a global will have remained. In the case of a multiuser environment—where a FileMaker Pro solution is hosted on FileMaker Server or via multiuser hosting—global values for each guest default to the value from the last time the database was in single-user mode; any change made to these defaults will then be specific only to a given user's session. Other users continue to see the default values, and after the database session is closed it reverts to its original, default state.
Using globals is a great way to keep track of certain states of your database. For example, you could use a global field to store which row of a portal was last selected. This field could then be used in scripts or calculation formulas.
For an example of using a global to drive portal behaviors, see Chapter 16, "Advanced Portal Techniques," p. 471.
Another common use of globals is for storing system graphics. Establish a container field, set it for global storage, and paste a favorite company logo, a custom button graphic, or any number of elements that you can then control globally in a field rather than having to paste discrete elements on each and every layout.
Note that FileMaker 8 offers a new feature in the form of variables that are defined within scripts (and by using the Let() function, within calculations). These variables exist in memory only and are not permanent fields that you add to your database schema. In the past, developers had to content themselves with using a slew of global fields; in FileMaker 8, the need for global fields has dropped considerably.
To learn more about variables in FileMaker, see Chapter 15, "Advanced Scripting Techniques," p. 435.
The second section of the Storage tab on the Field Options dialog lets developers allow a field to contain multiple values. Such fields are known as repeating fields. On a given layout the developer can array repetitions either horizontally or vertically, and in scripts can refer to specific repetitions within the field.
Repeating fields can be problematic. They behave just as individual fields might and are really just a shortcut for having to define multiple instances of a given field. It's possible, for example, to have no values in the first and second repetitions, but to have a value in the third. This sounds convenient and intuitively makes common sense, but imagine having to write a script that references that field. How do you know which repetition of the field to reference? Unlike an array in other programming languages, a repeating field cannot be manipulated as a whole. You can reference only one specific repetition at a time.
FileMaker 8 extended the usefulness of repeating fields somewhat in allowing the script step Set Field to programmatically reference a repeating instance. You can now open a Specify Calculation dialog to point a script to a specific cell within a repeating field. (Note that the same is true for setting variables.)
Repeating fields do have their place, however. Imagine a spreadsheet. Even though an entire row may be blank, the cells are there, ready and waiting for input. If your users are familiar with Microsoft Excel or have been using a paper form for years, it may make sense for you to duplicate the look-and-feel in question, using repeating fields.
In addition to facilitating data entry, you could simulate a related child table with repeating fields.
For a detailed discussion of multiple-table solutions, see Chapter 6, "Working with Multiple Tables," p. 157.
Knowing when to use repeating fields can be somewhat tricky. If you ever have multiple bits of information that belong together—say, a product name and a price—it's always best to create a table and define fields for those items. They are attributes of the same item—a product. You might be able to grasp that Price corresponded to Product, but only anecdotally. If another developer followed behind you in your work, that developer wouldn't necessarily make that same assumption.
The Excel spreadsheet example we gave previously could actually be exactly the wrong way to go: A spreadsheet assumes that a single row relates to all the information within that row. Repeating fields set next to each other on a layout do not share that programmatic logic.
We tend to use only repeating fields that genuinely need multiple instances of the same field: a graphics library, for example, or a simple place to store default values.
Databases store data by definition, of course, but they are also required to perform functions such as searches and sorts with that data. FileMaker Pro, like many databases, can index some of the data in a file to increase the speed at which it performs some of these functions and to enable it to relate data across tables.
An index is somewhat like a database within a database. FileMaker Pro can store, along with a specific value in a given field, a list of all the records in which that exact data is used. This then enables FileMaker to recall those records quickly without having to resort to a linear scan of your file. Aptly named, these indexes work just as a book index works: They facilitate finding all the locations in which a given item is used, without searching page by page through the entire book.
To familiarize yourself with the concept, take a look at a given field's index. Click into a field and select Insert, From Index. If the field is indexable, and has already been indexed, you are presented with a dialog box showing all the discrete values indexed for a given field. Just as with selecting from a value list, you may opt to choose from this list rather than type.
Allowing a user to select from an index is only one of the reasons indexes are used in FileMaker. Indexes enable FileMaker Pro to perform find requests, sort records, and establish relationships.
There are two kinds of indexes in FileMaker: value indexes and word indexes. Value indexes apply to all field types, with the exception of container or summary fields; word indexes apply only to text fields and are based on a given language or character set. The difference between the two, and when either is specifically enabled, lies in their applications.
FileMaker Pro's default indexing setting (found on the Storage tab of the Field Options dialog, displayed in Figure 3.10) is None, with the check box for Automatically Create Indexes As Needed enabled. Most developers, even the more advanced, should find that this setting serves most of their needs.
Figure 3.10 FileMaker creates either one type of index or both, depending on how a field is defined and used by users.
Value indexes are established by a database's schema definition—as a developer defines fields and builds relationships—and allow for relationship matches and value lists. If a developer creates a serial ID and joins a relationship via such a field, a value index is created for the serial ID field.
Unless a developer explicitly sets a field to generate an index, word indexes are created as users are interacting with and using a given database. They are utilized in text fields for find requests, or created when a user explicitly chooses Insert, From Index. If a user enters data in a find request for a field that lacks a word index, FileMaker Pro enables indexing for that field and builds one (unless it's explicitly unindexed, or an unindexable calculation).
At this point you may be wondering what all the fuss is about. Why not index every field in a database and be done with it? The downside to indexes is increased file size and the time it takes FileMaker to maintain the indexes. Creating new records, and deleting, importing, and modifying them, all take more time, in addition to the fact that the indexes themselves take up more file space.
Notice that FileMaker doesn't allow you to explicitly control word and value indices. Value indices are possible for all field types; word indices apply only to text fields. The Minimal setting will be an available option only for text fields, and when you see it marked, this indicates that at least one of the two indices exists for the field. There's no straightforward way of determining which. By explicitly setting the field to minimal, FileMaker will create, on demand, either of the two indices based on how the field is used: When a user creates a find request including that field, a word index will be created. If a developer uses the field in a relationship, a value index will be created.
Only a subset of the fields in your database will ever need to be indexed, and FileMaker's "on demand" approach makes things fairly simple for developers. Generally speaking, it's best if a field is indexed only when necessary.
To explore the vagaries of storage and indexing considerations for calculation fields, see "Other Options," p. 224.
An important point to remember is that some fields are not indexable. This means that they will be slow when used in sorts and find requests, but, most important, they cannot be used to establish relationships.
A field is unindexable if it is a calculation based on a related field, a summary field, or a global field, or if it references another unindexed, unstored calculation field.
You can also explicitly make a field unindexable by turning indexing options to None and unchecking the Automatically Create Indexes As Needed setting. In the case of a calculation field, an additional radio button option is available: Do Not Store Calculation Results—Recalculate When Needed. These settings are important to remember; they allow you to force FileMaker to reevaluate and display dynamic information. The Get (CurrentDate) function, for example, displays the current date if you have indexing turned off, but displays whatever date was last stored with the record if you leave indexing (and storage) turned on.
The fourth tab in the Field Options dialog is one that many English-speaking developers will have trouble properly pronouncing, let alone using. Because of the adoption of Unicode support in FileMaker Pro 7, it is now possible to offer Asian-language double-byte language support. As a result, you can now manage Japanese.
Japanese has four alphabets. One is based on glyphs from Chinese, known as Kanji, two are based on phonetic syllables known as Hiragana and Katakana, and the last is our own Roman alphabet, adopted in the nineteenth century for foreign words. When you're working in Japanese, it is possible to render the phonetic equivalents to a Kanji-based block of text. Quite useful when one doesn't know how to read one of the 20,000-plus characters in Kanji.
Suffice it to say that unless you're a student of Japanese (native or otherwise), this tab will likely not attract much of your attention.