In the last chapter, you created a custom database for storing lease documents. You learned how to create fields of the appropriate type to store your data and then you customized two layouts—one for viewing the data as a form and a second for viewing the data in a list.
If you never went any further than that, you’d still have a database that tracks documents. But now that your business is more efficient, it’s time to teach the database some new tricks. You’re already storing the signing date and lease duration data for each lease. In this chapter, you’ll create a field that lets you know when each lease is ending. Then you’ll create a new table and all the supporting mechanisms for recording monthly payments.
On Managing Field Types, you learned about field types as you created them. You chose a field type based on the kind of data you wanted to enter into each field. But calculation fields can create data under their own power. For instance, your database already stores monthly Rental Fee and Lease Duration data. If you wanted to figure out the value of that lease over its life, you could punch the numbers into a calculator and then manually enter the result into your database. But a calculation field calculates and enters data automatically and can even update if the data in either field changes. All you have to do is write a formula that tells the calculation field how to get the answer you want.
Note
This tutorial picks up where Chapter 3 left off. If you didn’t complete those tutorials, or you want to start with a clean file that’s already got some data in it, download the samples from this book’s Missing CD page at www.missingmanuals.com/cds/fmp13mm.
In the last chapter, you created fields with the Field Picker (Creating Fields). But for creating fields with more control, call up the Manage Database window (Figure 4-1). That’s how you’ll create your first calculation field:
Figure 4-1. The Manage Database window is the most efficient way to create and manage fields. You can also Duplicate or Delete fields here. Rename a field by selecting it from the list, typing the new name and then clicking the Change button.
Choose File→Manage→Database and then click the Fields tab (if it isn’t already active).
The Fields tab is where most of your field creation takes place. (See the box on Use the Keyboard for more information about using this window.)
In the Field Name field, type Lease Value.
As with other database elements, it’s best to use simple, descriptive names for your new calculation field.
From the Type pop-up menu, choose Calculation and then click Create.
The Specify Calculation window appears. Here’s where you’ll write the formula that gives you the Lease Value. It’s based on data in the Lease Duration and Rental Fee fields.
In the Current Table list, double-click the Lease Duration field.
The field’s name appears in the calculation area of the window. You could type the field’s name, but if you make a typing error, FileMaker will bark at you, so it’s usually easier to point and double-click.
In the Operators section of the window, click the * sign.
The asterisk means multiplication.
In the Current Table list, double-click the Rental Fee field.
The formula now reads Lease Duration * Rental Fee. Your calculation should look like Figure 4-2.
Figure 4-2. The Specify Calculation window lets you write a formula that’s attached to a calculation field. You can perform calculations on text, number, date, time, timestamp, or container fields and get results of each of those types, too. The option “Do not evaluate if all referenced fields are empty” can speed up your database, because FileMaker doesn’t have to try to calculate a value if data is missing from all the fields that make up the calculation.
Note
Number is automatically selected for the “Calculation result.” But if you create several calculation fields in one session, the default result type changes to match the last type you created. You should always check this setting when you’re creating calculations, since some calculations don’t work as you intend if the calculation result type isn’t set properly. You’ll see one such example in the next tutorial.
Click OK and then OK again to close both windows.
Depending on the settings in your Preferences window, you may not see the new calculation field appear immediately on your layout.
If the new calculation field appears on your layout, switch to Layout mode and then move the field into place below your existing fields. (If the new field doesn’t appear, see the next page to learn why. The next section shows you how to create the field manually.) Switch back to Browse mode to see the calculation in action. Change the data in the Lease Duration and Rental Fee fields to see the calculation work dynamically.
Tip
Now that you’ve created fields using the Manage Database window, you may never want to go back to creating them in Table view. Change FileMaker’s Preferences by choosing Edit→Preferences (Windows) or FileMaker Pro→Preferences (Mac). Then turn on the “Use Manage Database dialog to create files” option. The next time you create a new database, you’ll get the Manage Database dialog box instead of a table. Note, though, that once you set Manage Database as your preferred method for creating fields, new fields no longer automatically appear on your layouts when you create them in Table view unless the “Add newly defined fields to current layout” option is also selected in Preferences.
You determine whether or not newly created fields appear automatically on your layout when they’re created using the Manage Database dialog box. In Windows, choose Edit→Preferences; on a Mac, choose FileMaker Pro→Preferences and then select the Layout tab to see the “Add newly defined fields to current layout” option. When this option is turned on, FileMaker just plops the field down at the bottom of your layout and, if necessary, increases the Body part to accommodate the new field. Since you’re going to invest a lot of time making layouts look just so, you’ll usually work with this option turned off. That means you have to place any newly created fields on your layout manually.
FileMaker provides many ways to add a field to a layout, all of which require you to be in Layout mode. First of all, you can just choose Insert→Field. When you do, FileMaker asks which field you want and then drops it on the layout for you to move into place. The Field/Control tool (Figure 4-3) gives you more control, so it’s used more often than the menu command. This tool lets you create an edit box, drop-down list, pop-up menu, checkbox set, radio button set, or drop-down calendar instead of a plain vanilla edit box. Another version of the field tool (third tool from the right) lets you drag a field onto the layout. This tool doesn’t give you the option of choosing a control type, though.
The Field Picker is the best method when you need to add several fields at once, because you can use your favorite selection technique to select all of the fields you need and then drag them onto the layout where you can start arranging them. This method also lets you choose where the fields’ labels appear.
Finally, you can copy an existing field. Use the Edit menu’s Copy and Paste commands (or their keyboard shortcuts). But to really work like the pros, Shift-drag (Option-drag) an existing field to copy it as you move it. Fields you create this way have all the formatting of the original, so you can save lots of trips to the Inspector.
Figure 4-3. In Windows, an arrow to the right of the tool lets you view the pop-up menu. On a Mac, press and hold the Field/Control tool to reveal its pop-up menu. The Field/Control tool icon changes to reflect your choice and then you use the crosshair cursor to drag on the layout and create the new field. When you’re creating fields, turn on dynamic guides, so you can see the size, shape, and location of the new fields. The dotted horizontal line represents the field’s baseline.
To add the Lease Value field to the layout, follow these steps:
Click the Field/Control tool.
The Field/Control tool will darken to indicate it is the active tool. When you move your cursor down to the work area, it changes to crosshairs.
Click in the layout and drag to create a new field.
As you drag, FileMaker shows dynamic guides to help you size and line up the field.
When the field is the size you want, release the mouse button.
The Specify Field window appears (Figure 4-4), showing a list of the fields in your current table.
Choose the Lease Value field and then turn on the “Create label” checkbox (if it’s not already turned on).
Figure 4-4. When you add a field to the layout, FileMaker asks which field you want by showing you the Specify Field dialog box. Another way to quickly create a field with the formatting you want already on it is to copy and paste an existing field and its label. Double-click the new field to show the Specify Field dialog box. Select the field you want, turn off the “Create label” checkbox before you click OK. Finally, manually change the field’s label.
The field and its label appear on the layout. If either the field or the label doesn’t appear in the format you want, make sure you’ve saved the changes to the proper style (Customizing Styles) and then apply the proper style to the object.
The new field isn’t quite as wide as the original, so now is a good time to notice that your other currency field, Rental Fee, is much wider than it needs to be. Make the Rental Fee and Lease Value fields the same width as the Lease Duration field. Don’t forget to use the Inspector’s Data tab to apply Currency formatting. Most people prefer to see currency data aligned to the right. You can do that in one of three ways:
Choose Format→Align Text→Right.
Click the Right Align button in the Formatting bar (Text Formatting).
Go to the Inspector’s Appearance tab, Paragraph section, and click the Right Align button.
Now you’re ready to switch to Browse mode to view the data in your new calculation field. The Date Signed field is also wider than it needs to be, so switch back to Layout mode and make it a little smaller as well, but not quite as narrow as your currency fields.
In the Manage Database window, you can delete a field (and its contents in all records of the current table) by selecting it and then clicking Delete. You’ll see a dialog box asking you to confirm that you really do want to delete the selected field. There’s a caveat, though: Calculation fields (like Lease Value) make reference to other fields. That is, the formula for Lease Value is Lease Duration * Rental Fee. If you try to delete either Lease Duration or Rental Fee, FileMaker shows you a warning dialog box and refuses to delete the field. If you absolutely, positively have to delete either field, you have a choice: Either change Lease Value’s formula so it doesn’t refer to the field you want to delete, or delete Lease Value first. Then when you try to delete the field again, FileMaker still asks you to confirm that you want to delete the field, but it doesn’t need to warn you about the other ones that refer to it—because there aren’t any.
Note
You’ll also get a warning if you try to delete a field that’s used in a script. But in that case, you get the choice to proceed anyway or cancel the delete. If you delete a field used in a script, be prepared to do some repair work. The script can’t do its work properly once a field it needs is gone from your database.
There’s a fallback position if you delete or change fields and then change your mind or realize you’ve made a mistake. Click the Manage Database dialog box’s Cancel button. FileMaker asks whether you want to “Discard ALL changes made in this dialog to tables, fields, and relationships.” Click Discard, and FileMaker performs none of the changes you’ve made since you opened the Manage Database dialog box. You may lose work you would have preferred to keep, but that’s often a better option than losing a field with a tricky calculation that you can’t quite remember.
Now that you’ve got the basic information stored for your leases, you realize that you can centralize your data if you record rent payments in the same database as the lease documents. Your first thought might be to create a field or two (Date Paid and Amount Paid) to record each payment, but you quickly realize that since some leases last 36 months, you’ll have to create 72 fields and then place them on your layout. Worse yet, for your 12- and 24-month leases, most of those fields will be empty.
If you start signing 48-month leases, you’ll have to create a whole slew of new fields and start rearranging your layout again. And what if your tenants make more than one payment each month? It’d be so much more efficient if each record could have only the fields it needed, and no more.
You’re on the right track—instead of adding fields in the Lease Agreement table, what you need is a set of new records in a related table (see the box on These Terms Are Relational for a definition of table and other terms you’ll need to know for this section). That’s where a relational database comes in. You need separate tables to store each type of information. Because when you think about it, monthly payments aren’t really part of a Lease Agreement table. Date Paid and Amount Paid data pertains to a specific lease agreement, but it doesn’t belong with the name of the tenant or the PDF of the lease agreement itself. What you need is a new Payment table, where you can add 12 records (one per month, of course) to the 12-month leases, or 24 records to the 24-month leases. With a separate table, even if you start offering 5-year leases, you’ll never have to add more fields or stretch a layout to accommodate a change in the way you do business.
And you don’t even have to create a new file for your new table. FileMaker lets you put dozens, even hundreds, of tables into the same file. There’s an art and science surrounding how to figure out which tables you need and how to relate them to one another. You’ll learn that in Chapter 5. For now, you’ll learn about the tools you need to create a related table and enter monthly payment records on the Lease Agreement layout.
Now that you’ve decided to store lease information in the Lease Agreement table and payment information in a new Payment table, you need to make sure payments match the right Lease Agreement record. First, you start with a unique identifier called a key field, which uses the auto-enter field option to create a serial number that’s unique for each record in the table. Then you use the Relationships graph to match the two tables’ key fields. Finally you create a special layout object, called a portal, that lets you view, create, and edit records from a related table. In this case, you’ll use a portal to show payment records on the same layout where you store data about each lease agreement.
To ensure that Lease Agreements and Payments records match properly, you need a unique identifier in the Lease Agreement table. One of FileMaker’s field options, called Serial Number, automatically assigns a unique number to each record when it’s created. Here’s how to create a key field and then apply an Auto-Enter Serial number option to it:
Choose File→Manage→Database and then if it’s not active, click the Fields tab.
The Manage Database window appears, with the Lease Agreement table’s fields in a list.
In the Field Name field, type agreementID.
This field name may seem odd (no spaces and a mix of upper- and lowercase letters), but it’s one of many naming conventions used by developers to help them quickly identify fields that they’ve created to make the database work. In this naming convention, the field name starts with the name (or one-word abbreviation) of the table for which it is the key field. The “ID” at the end confirms that the field is used as a key.
From the Type pop-up menu, choose Number.
Key fields are most often number fields. (See Keys for more information on choosing and creating a good key field.)
Click the Create button and then click the Options button. See Figure 4-5.
The “Options for Field ‘agreementID’” window appears.
Turn on the “Serial number” checkbox and then click OK until you’re back on the Lease Agreement layout.
A serial number is created in the agreementID field for each new record you make.
It’s important to know that even if you can’t see the agreementID field, the serial number is added to it when you create each new record. And if you still haven’t turned on the “Add newly created fields to current layout option” in the Preferences dialog box, do that now. Create a few new records to watch serial numbers being created. Notice though, that the records that were already in your database don’t have serial numbers yet.
Figure 4-5. When you first set up an auto-enter serial number field, the next value is always “1” unless you change it. Auto-enter serial numbers are most often assigned as a record is created (“On creation”), but if you choose “On commit” instead, FileMaker doesn’t generate the number until the first time the user commits the record. Usually, you use the “On commit” option when your database has multiple users (as you’ll learn on page 232). For instance, a user might create a record but then delete it instead of filling out its data. In that case, there would be a gap in the serial numbers, because the numbers assigned to deleted records are never reused. If you’re importing data from another source, you should find the highest serial number in the imported data and then set your field’s “next value” to one number higher than the imported data. And while the most common “increment by” value is 1, you can make the numbers increase by any other number you choose instead.
When you create a key field after data has been entered, you don’t have to go to each record and manually enter a serial number. You can use the Replace Field Contents command (Using the Replace Command) to add in the missing serial numbers and reset the field option’s “next value” counter at the same time. Since you have to have a field on a layout in order to use the Replace Field Contents command, you may have to put the agreementID field on the Lease Agreement layout if it isn’t already there.
In Browse mode, choose Records→Show All Records.
Every record has to have data in its key field in order to relate to another table, so make sure you aren’t looking at a found set of just some of your records.
Note
If you’re using this tutorial on a database where some records have serial numbers and some don’t, make sure you don’t replace the value in the records that already have numbers. First, find just the records without serial numbers. Then, make sure you set the “next value” higher than the largest number in the record with serial numbers.
Click in the agreementID field.
If you don’t click into a field first, the Replace Field Contents command will be dimmed.
Choose Records→Replace Field Contents.
The Replace Field Contents window appears. See Figure 4-6.
Figure 4-6. In the Replace Field Contents dialog box, the default button is not the one that does the action you’ve just set up. The Replace command can’t be undone, so FileMaker is saving you from unintentionally destroying good data if you hit Enter too soon. Instead, pressing Enter cancels the replacement and leaves everything as it was. Replace Field Contents is a lifesaver when you have to retrofit a table with a key field after you’ve created records. However, replacing data after you’re created relationships between tables is risky—if the value in the key field in either table changes, the child record gets disconnected from its parent.
Select “Replace with serial numbers,” and then turn on “Update serial number in Entry Options,” if it isn’t already turned on.
Replace with serial numbers tells FileMaker how you want the numbers created in your existing records. Update serial number in Entry Options ensures that records you created after the Replace don’t reuse any of the numbers entered in the current found set. If you didn’t choose this option, you’d have to look through your records to find the highest number and then return to the Manage Database dialog box to change the “next value” setting for the agreementID field manually after the replace is done.
Click Replace.
Serial numbers are created in all the records of your database.
Flip through the records to see the serial numbers. You can go back to the Manage Database dialog box and check the auto-enter options for the agreementID field to see that its “next value” has been updated. While you’re there, select the “Prohibit modification of value during data entry” option at the bottom of the window. You don’t want people changing your carefully crafted serial numbers. Create a new record to see the next value appear automatically in the agreementID field.
Tip
Because it can be cumbersome to create a key field and populate it with data months or even years later, it makes sense to create a key field in every table you create even if you have no immediate plans to relate the table to any other table. That way, you don’t have to deal with all that rigmarole if your needs change. Plus, a unique serial number is very helpful if you’re trying to troubleshoot missing or duplicated records.
Your Payment table needs to store information about each monthly payment for a specific Lease Agreement. The Payment table’s attributes are the date the rent was paid and the amount paid. You also need a key field for hooking up Payments to the Lease Agreement table. And since it’s good practice to create a key field in every table, just in case, you’ll also add a paymentID field.
Two key fields in one table? It may sound crazy, but it’s not uncommon for a table to have 10 or more key fields that let it relate to that many other tables. The first key field you’ll create (called paymentID) uniquely identifies each Payment record and could be used when you figure out a reason to link the Payments table (as a parent) to another table. (See Primary and Foreign Keys for information on primary and foreign keys.) The second key field (agreementID) will hold the value that matches the value in the key field of a specific record in the Lease Agreement table. That’s how a Payment record (the child) matches up with the proper Lease Agreement record (the parent).
Choose File→Manage→Database and then click the Tables tab.
This tab is where you create, edit, and manage your tables.
In the Table Name field, type Payment and then click Create.
Remember: When you created the Lease Agreement table, you decided to use the singular case for your database elements, so stay consistent. The Payment table appears in the Tables list.
Click the Fields tab.
You’re viewing the field list for the new Payment table. FileMaker is smart like that and switched to the selected table for you. But if you need to, you can switch tables using the Table pop-up menu above the list of fields.
In the Field Name field, type paymentID and then select Number from the Type pop-up menu. Click Create.
It’s a good habit to create a table’s key field first thing. That way, you won’t forget to do it.
Click the Options button and make the paymentID field an auto-enter serial number.
All your Payment records will have serial numbers because you created a key field right at the beginning, before you created any records. And since you have no records yet, the next value should be “1.”
At the bottom of the Options window, turn on “Prohibit modification of value during data entry.”
This prevents users (even you, the developer) from changing the data in a field. Protecting the data in a key field is critical to keeping your records properly related to one another. But if you ever do need to change this data, just head back to the field’s options and switch off this option.
Click OK.
You’re ready to finish creating the rest of the fields for the Payment table.
Use the skills you learned earlier in this chapter to create these fields (their types are in parentheses):
Date Paid (Date)
Payment Amount (Number)
agreementID (Number)
Click OK when you’re done. Because you’re still viewing the Lease Agreement layout, you don’t see evidence of your new table yet. But it’s there.
If you click the Layout pop-up menu (in either Browse or Layout mode), you’ll see a new layout called Payment. FileMaker created it for you when it created your new table. And much like the boring layout you got when you created the Lease Agreement table, there’s nothing there except the standard lineup of layout parts and your newly created fields. In Browse mode you can see that this new table doesn’t have any records yet. If you click in a field as if to enter data, you’ll see the warning message in Figure 4-7.
Figure 4-7. When you see this message, just click OK. But don’t bother creating a record. You’ll do that later—from the Lease Agreement layout.
You could dress this layout up any way you want to. For instance, you might turn it into a list layout so you can report on your payments. But since you’ll be creating Payment records and entering data from the Lease Agreement layout, for now just note that the Payment layout is here, and go back to the Lease Agreement layout.
You just saw that when you create a table and then add fields to it, FileMaker makes a bare-bones layout for that table. It also makes a Table Occurrence for the new table on the Relationships graph. That graph is found on the only tab of the Manage Database window that you haven’t seen yet. And true to its name, that’s where you create the relationship between the Lease Agreement and Payment tables.
Note
Your Relationships graph can have more than one instance of any table, and each instance is a different view into the table. Each instance of a table is called a table occurrence. It’s important to know whether you’re referring to the table itself or an occurrence of the table. A word of caution, though: FileMaker isn’t all that consistent about using the term in its own windows and help files, so it’s not your fault if you’re confused.
Also true to its name, the Relationships graph is a visual representation of your file’s tables and how they relate to one another. And you create relationships in perhaps the easiest way possible: You drag from one table to another to create a line. Here’s how to create a relationship between two tables, using their key fields:
Choose File→Manage→Database or use the shortcut Shift+Ctrl+D (Shift-⌘-D) and then click the Relationships tab..
You’ll see two table occurrences: one for the Lease Agreement table and the other for the Payment table. You need to draw a line between the agreementID fields in each table, but that field is not visible in the Lease Agreement table occurrence.
Click and hold the tiny triangle at the bottom of the Lease Agreement table occurrence.
That scrolls the field names so that you can see the one you need. Or you can drag the bottom border of the table occurrence to make it tall enough to show all its fields.
In the Lease Agreement table occurrence, click the agreementID field and then drag to the agreementID field in the Payment table occurrence. Release the mouse when it’s pointing to the proper field.
As you drag, you’ll see a line with a box in the middle, and the key field is highlighted to help you stay oriented. The pointer changes shape to show that you’re creating a relationship. As you hover over any field, it highlights. When you release the mouse, the two fields you’ve connected jump above a new divider at the top of each table occurrence. They still appear in the list below and are italicized to show that they’re used as keys (Figure 4-8).
Double-click the box in the middle of the relationship line.
The Edit Relationship window appears (Figure 4-9). The window is divided into halves, showing Lease Agreement on the left and Payment on the right.
On the Payment side of the relationship, turn on “Allow creation of records in this table via this relationship” and “Delete related records in this table when a record is deleted in the other table.”
This setup is typical of the child side of a relationship. Here’s how to think about it: You’ll be creating Payment records from the Lease Agreement layout, so the relationship’s options need to allow record creation. And if you delete a Lease Agreement record, there probably isn’t much use for the Payment records, so that second option deletes Payment records that would otherwise be “orphaned” when the parent record is deleted.
Click OK until you return to the Lease Agreement layout.
Or the Payment layout, if that’s the one you were viewing at the start of this tutorial.
Figure 4-8. Notice that the line between the two Table Occurrences is straight on the Lease Agreement end and has a “fork” at the Payment end. This crow’s foot helps you know which is the parent and which is the child side of the relationship. But how does FileMaker know which is which? Lease Agreement’s agreementID field has the Auto-Enter Serial number option turned on, which is standard operating procedure for a field that’s meant to be used as a parent table key field. Payment’s agreementID isn’t set as a serial number, so FileMaker knows that it’s pointing to the child table’s key field.
If naming fields in both tables with the same name seemed confusing as you were doing it, you’ve just seen why it’s a good idea. In a large or complex database, some tables can have many key fields for relating to other tables. But if you use the same name in the child table as the key field in the parent table, it’s very easy to find the proper key field and then drag a line between the two tables.
Note
Purists will say that the “tables” you just connected by their fields are really table occurrences. Turn to Understanding Table Occurrences to find out why.
Figure 4-9. The Edit Relationship window lets you define how a relationship works. It’s divided in half vertically, with one table’s information on the left and the other table’s information on the right. For most people, it’s easiest to visualize a relationship when the parent table is on the left and the child table is on the right. If your window is flipped (Payment on the left and Lease Agreement on the right), it’s because your table occurrences are flipped on the Relationships graph. To change the display, close the Edit Relationship dialog box and then drag the table occurrences to rearrange them. In practice, it doesn’t matter which side the tables appear on because relationships are bidirectional (you’ll learn what that means on page 217).
Once your tables are related to one another, you can freely display related fields on layouts. For example, say you want the Date Paid and Amount Paid fields from the Payment table to appear on your Lease Agreement layout. You could simply add those fields to the layout, but you’d quickly find a big problem. There’s only a single instance of each field, and the point of this related Payment table is to have multiple records from the child table related to the parent table.
The problem is solved with a portal, which is a layout object that displays multiple records from a related table. Not only can the portal display related records, but you can also use a portal to create, edit, and delete related records.
A portal can display as many related records as you want, limited primarily by the size of your layout and the height of the portal. As with other layout objects, you can format portals to match your database, using fills and lines the way you would with other drawn objects. Here’s how to create a portal on the Lease Agreement layout:
In Layout mode, drag the bottom edge of your Body part to make room for your portal. Or use the Inspector to make the Body 500 points high.
Figure 4-10 shows you how much space you’ll need.
Figure 4-10. In the upper right of the Status toolbar, you see the Portal tool selected. On the layout you can see an outline where the portal will be when you finish creating it. The settings shown in the Portal Setup dialog box above, along with the relationship’s setting to allow record creation, let you create, edit, and delete Payment records without ever having to visit a Payment layout.
Click the Portal tool to select it and then drag on the layout to create a portal.
As with all drawn objects, it’s usually easiest to start in the upper-left corner and drag to the lower-right corner where you want the portal to appear. When you release the mouse, the Portal Setup window appears.
From the “Show related records from” pop-up menu, choose Payment.
This is the Payment table occurrence you saw when you hooked up the Lease Agreement and Payment tables in the Relationships graph (Creating a Relationship Between Two Table Occurrences).
Turn on “Show vertical scroll bar.”
A scrollbar lets you see more related records in a smaller space.
Turn on “Allow deletion of portal records” and then click OK.
The “Add Fields to Portal” window appears. When the Allow deletion option is turned off, you can’t delete Payment records using the portal and the regular Delete command.
From the list of available fields on the left, select and move the Date Paid, Payment Amount, and agreementID fields.
Each field appears in the list on the right as you move it. It isn’t necessary to add a related table’s key field to a portal. In the real world, you don’t usually want to see that data. But while you’re learning about portals and relationships, it’s very helpful to display the key field so you can see how portals work.
Tip
Pay attention to the way the field name appears in the available fields list. You’re seeing the parts of a fully qualified field name. That is, the field is represented by its table name, followed by a pair of colons and then the field name itself. It’s kind of like you being called by both your first and last names. This nomenclature helps you and FileMaker keep the Lease Agreement and the Payment tables’ agreementID fields straight.
Click OK.
FileMaker adjusts the portal object to show the number of rows you specified, and the related fields you selected in the previous step appear in the portal (Figure 4-11).
Figure 4-11. In Layout mode, the portal displays information about the data it contains along its lower-left edge. The related table’s name (Payment) appears, followed by “[1…6+].” That means that the portal is tall enough to display 6 rows of related records, starting with the first record. The “+” sign means that the portal will show an active scrollbar when it contains more than six child records. Notice that the Lease Agreement::agreementID field is moved out into the gray space beyond the edge of your layout. Objects in that space don’t show up in Browse mode, so it’s a handy place to put things you might need for troubleshooting but you don’t want to see while you’re entering data.
No matter how many rows the portal is set to show, you see only one row of fields in Layout mode, and they’re always in the portal’s first row. Notice that the fields fit precisely in that top row. In fact, FileMaker used the height of your fields to figure out how many rows it could fit in the space you drew with the portal tool.
The Payment fields are evenly divided within the width of your portal. You may need to adjust the fields’ widths, change their text alignment, and create field labels.
Just like any other layout object, a portal shows selection handles when you click to select it. But the location of its bottom selection handles may surprise you. They’re at the bottom of the portal’s first row, not at the bottom edge of the portal. If you drag the bottom middle selection handle downward, you change the row height and not the number of rows the portal will display. That’s useful where you want to show lots of data from each related record and a single row of fields won’t get the job done. You can change the width of a portal by dragging one of the middle handles on either side or by using the Width box on the Inspector’s Position tab.
To move a portal, click to select it and then drag it to the new location. When you do, all the fields move along with it. So it’s safest to click in the gray area below the fields, because if you click a field and then drag, only the field you click will move. You may want to move fields within their portal—say to rearrange their order or to scoot them around to make room for new fields—but make sure they stay inside the border of the portal’s top row. If they’re off by even a point, two problems can occur. First, only one row of data from the related table will appear, no matter how many related records there are. Second, if you try to move the portal, the fields that stick their necks up get left behind when you drag the portal.
Delete a portal by clicking it and then pressing the Delete key (or choosing Edit→Clear). The portal and its fields are deleted together. You can copy a portal and its fields with the Copy and Paste commands or keyboard shortcuts. But the Shift-drag (Option-drag) shortcut is quickest. See the box on Power to the Portal to learn why you might want to copy a portal.
Making a portal taller doesn’t give you more rows. If you want to add more rows, change its options. Do that by double-clicking on the portal to make the Portal Setup dialog box appear. That’s the same one you saw when you created the portal. To add or remove rows, just type the number you want in the “Number of rows” field. When you click OK, the portal will resize to accommodate its new settings.
Note
If the layout isn’t tall enough to fit the adjusted portal, you’ll see a warning message that tells you that the layout size needs to be increased. If you don’t want FileMaker deciding how tall your layout should be, click No in that warning dialog box, and the portal will stay the height you drew it. Then you’ll have to increase the layout size manually and then try to change the row display again.
Back in Browse mode, you can see the portal, but there’s no data in it. That’s because the Payment table doesn’t have any records yet. If you’re thinking about choosing the New Record command, don’t act on that thought, because it won’t work. At least it won’t create a new Payment record. It will continue to work as it always has, by creating a new Lease Agreement record. To understand why, backtrack just a bit to take a look at your layout’s setup. Switch to Layout mode and then choose Layouts→Layout Setup (Figure 4-12).
Figure 4-12. The Layout Setup dialog box tells you that the Lease Agreement layout shows records from the Lease Agreement table occurrence. The “Include in layout menus” option lets you determine which of your layouts show up in the Status toolbar’s Layout pop-up menu. Deselecting this option is a good way to keep users off a layout you don’t want them to see, like your Payments layout or layouts you create for printing envelopes or mailing labels.
Each layout is tied to one, and only one, table occurrence. That table occurrence tells the layout which table’s records it can show. Another way of saying the same thing is that the table occurrence gives the layout its context. Context is fundamental to many aspects of your database, because without the proper context, the layout can’t show you the records you want to see. Layouts have context, and so do portals. The portal you added to the Lease Agreement layout has the context of the Payment table. Keep context in mind as you’re reading the next section, which covers the things you can do with related records by using a portal.
This database has only two tables and only one occurrence of each table, so context seems obvious. But real-world databases often have dozens (even hundreds) of tables, and they may have several occurrences of each table. So if you get used to thinking about context now, you’re laying a good foundation for working with more complicated databases later. No matter how complicated a database gets, though, you can thread your way through the most tangled mess by answering two simple questions: Where am I, and what records do I need to see? Right now, you’re on the Lease Agreement layout, and you need to see Payment records. The relationship you created on Creating a Relationship Between Two Table Occurrences shows you the records you need.
Once you’ve set up a portal on a layout, you can create records without going all the way back to the Payment layout. On a Lease Agreement record, in Browse mode, click in any field. You can now tab into the Payment portal’s fields, just as if they were a part of the Lease Agreement table. The fields are active even though there aren’t any related records, because when you defined the relationship between the Lease Agreement and Payment tables, you chose the option that lets related records be created. View your related table by choosing Window→New Window. A new window appears that’s a duplicate of the original. Move the new window over to the side so you can see both windows and then switch the new window to the Payment layout (Figure 4-13).
On the Lease Agreement layout, go to Browse mode, click in the Payment portal’s Date Paid field, type a date and then press Tab.
As soon as the insertion point moves to the next field, you’ll see the record appear in the Payment window. Even though you didn’t enter it, the new record’s agreementID field now has a value, and it matches the value in the Lease Agreement::agreementID field.
Type a number in the Payment Amount field and then press Tab again.
The agreementID field in the portal is active. Because it’s a child record key (sometimes called a foreign key), it’s not set up to prohibit data entry. Try this experiment: Change the number in the Payment::agreementID field (it’s in the portal). Click outside the portal to commit the change and the Payment record disappears. After you commit the change, the Payment record is no longer related to the Lease Agreement record. It might even be related to the wrong parent record now. Luckily you have the Payment window open, and you can just change the Payment::agreementID information back to match the value in the parent record you’re viewing. When you commit the Payment record, it will show back up in the portal.
Use the portal to create a few more payment records just to watch how the process works behind the scenes. Notice how the Tab key travels through the rows in the portal, much as in a spreadsheet.
Figure 4-13. In the window on the left, the Omar Little record is active, and you can see that the portal shows field boundaries as if there’s a new record. But in the window on the right, you can see that the Payment table doesn’t have any records yet. Keep your screen set up like this for the next tutorials, so you can watch how and when related records are created, edited, and deleted. This is also a good troubleshooting tip when something is wrong with a relationship and you’re trying to figure out how to fix it.
Remember you wouldn’t usually put a foreign key field in a portal because as you just saw, the child record disappears from the portal when you changed the field’s value; it’s no longer related to the parent record you’re viewing. Plus it’s confusing to users, since the value in every child record is, as it should be, exactly the same. Still, viewing an ID field in a portal, along with a second window opened to a layout that has the context of the related records, is a good way to learn how relationships work. Plus it’s a great way to troubleshoot when something is not working the way it should.
You can edit any related record by clicking in the field you want to change and then typing the new info. Tab to the next row, or if you’re on the last row in the portal, click into a blank space on the Lease Agreement record to commit the changes.
Unlike the New Record command, the Delete Record command can work on a portal record—if you’ve formatted the portal to allow related record deletion (luckily, you did just that back on Creating a Relationship Between Two Table Occurrences). But you have to click in a portal row first to set up the context so FileMaker knows which related record you want to delete.
Click in any field of the portal row you want to delete.
You’ll see the insertion point blinking in the field you clicked in.
The delete master or related record warning appears (Figure 4-14).
A warning dialog box appears, giving you a chance to change your mind and keep the record instead of deleting it.
Click the Delete button.
The related record is deleted.
The terms master and related are FileMaker-speak for parent and child. If you’re not on a portal row at all—neither an active field nor a portal row highlight—the Delete Record command works exactly as it does on a layout that has no portals. That is, you’ll delete the Master (or parent) record.
There’s a way to avoid dealing with two different warning dialog boxes when you want to delete a related record. You have to select the whole portal row instead of just clicking in a field (Figure 4-15). Click between the fields in a portal row instead of in one of its fields. When the portal is highlighted, choose Records→Delete Record. FileMaker now knows that you want the related record deleted and it skips right to a warning dialog box that asks if you want to permanently delete the related record. Click Delete, and the payment record is gone.
Figure 4-15. The Payment record for December 15, 2014, is highlighted. No field in the portal row is active, but a highlight shows on the whole row. Selecting a row this way sets the context for a portal row delete. However, you can’t enter or edit data in a portal row that’s highlighted this way, because the insertion point isn’t in a specific field.
You can search in related fields just as easily as you can search in the “local” table’s fields. So if you wanted to find all Lease Agreements with April 2014 Payment records, just switch to Find mode, click in the Payment portal’s Date Paid field, type 4/2014 and then click Perform Find. You get a found set of all the Lease Agreement records that have related Payment records dated April 2014. Each Lease Agreement record will still show all its related records in the portal, and not just the ones that match your search criteria. As you flip through your found set of Lease Agreement records, you might think that some records shouldn’t be in the found set because you can’t see April 2014 dates in the portal. But remember, if there are a lot of Payment records for a Lease Agreement, you may need to scroll to see more records. So if you think a record showed up when it shouldn’t have, scroll down in the portal. You’ll find an April 2014 Payment record in there somewhere.
But if what you wanted was a list showing only the April 2014 payment records, you have to search using the Payment layout, which is set to show records from the Payment table occurrence (because it has the context of the Payment table occurrence). That way, your found set will contain just the records with April 2014 payments.
Adding a portal to the Lease Agreement layout may have streamlined your workflow, but it required you to increase the size of your layout. And if you want to add a large Notes field, you’ll have to increase the size even more. Plus, the Lease Document container field would be more useful if it were larger. But if you make it too big, that field will dwarf all the other data on the screen, making it hard to focus on the text data.
That’s where Tab Controls come in. The name makes it seem as though Tab Controls have something to do with the Tab key on your keyboard, but they don’t. (The “Tab” part of Tab Control is a tribute to the antique database called a file cabinet. Our forefathers used a paper device with a protruding tab, called a folder, to organize their documents.) Tab Controls provide a way to organize data on a layout so you can focus on a few chunks of data at a time. Tab Controls also let you put far more information on the same layout without making it gigantic or cramming it with data. You’ve seen similar objects in other software programs, even other places in FileMaker itself. For example, the Inspector has four tabs that are used for organizing all the information it holds. A layout Tab Control works much the same way (although without the collapsible sections). Figure 4-16 shows the Lease Agreement layout reorganized using a Tab Control.
Tab Controls are easy to draw, but since it can be tricky dividing the objects on an existing layout among the new tabs, you’ll need to do a little prep work. The process is easier on a larger monitor but you can do it even on a small one. First, expand the database’s window as large as your monitor will allow. If it’s not big enough to show you about double the width of space as you currently have showing, try Zooming out. Take a look at Figure 4-16 to see the new design you’re aiming for.
Figure 4-16. This version of the Lease Agreement layout has a large Tab Control covering most of its area. The Tab Control has two tabs, with the existing fields divided among them. The Agreement tab has the basic data, plus a Popover button for the Lease Document (more on popover buttons on page 148). The Payment portal has been moved to the Payments tab. Check the finished sample file to see what those tabs look like.
Next, follow these steps:
In Layout mode, drag all of your fields and field labels over in the gray space to the right so they’re past the visible edge of the Lease Agreement layout.
If you don’t move your fields, the new Tab Control will be on top of them in the layout’s stacking order, and it won’t be easy to select them afterward. Although you’d think that the “Send to Back” command would fix things, it doesn’t always, so it’s just better to empty out the space first.
In the Status toolbar, click the Tab Control tool. (It looks like a tiny tab control, with just one panel.) Then, draw a large tab control on the layout.
It should be as wide as the Body part and nearly the height of the Body. Put the left edge of the Tab Control on the left edge of the layout, and its bottom edge on the bottom edge of the Body part. When you finish drawing, the Tab Control Setup dialog box (Figure 4-17) appears.
Note
You can also create a Tab Control by selecting Insert→Tab Control. The Tab Control tool lets you predefine the size and placement.
Figure 4-17. These are the settings for the Tab Control shown in Figure 4-16. Tab widths are automatically determined by the length of their names. But you can use the Tab Width pop-up menu to add extra space, set a minimum or fixed width, or make all tabs the width of the widest label. All Tab Width options are overridden if you select Full justification, though.
In the Tab Name field, type Agreement and then click Create. Repeat this step and name your second tab Payments.
FileMaker adds the two tab names to the Tabs list. The first tab you create is set as the Default Front Tab. That means the Agreement tab will be the active one whenever you switch to the Lease Agreement layout—no matter which tab was active the last time you left it.
From the Tab Justification pop-up menu, select Full.
The Full option will make the tabs appear all the way across the top of the Tab Control itself.
When you’re done, click OK to close the Tab Control Setup dialog box.
Your new tab panel, complete with two tabs, sits highlighted in place on your layout.
Notice the selection handles and a dark box around each of the tabs. Any formatting changes you make with this selection will affect both tabs. It’s easiest to make formatting changes to the Tab Control right after you set it up, because it’s a bit of a pain to reselect all your tabs later. Try it now: Click outside the Tab Control to deselect it. Then click the Agreement tab. To select the others, you have to press Shift and then click twice on the other tabs. Pause slightly between clicks—if you click too fast, your computer will think you’re double-clicking and open the Tab Control Setup dialog box).
Switch to Browse mode, where you’ll see that your Tab Control is already working. You can click tabs, and each one comes to the front, just as you’d expect. But a Tab Control without objects is pretty useless.
Switch back to Layout mode to divide your objects and move them onto their proper tabs. First, select the fields and field labels that belong on the Agreement tab (refer back to Figure 4-16, if you need a refresher). Then drag those fields onto the Agreement tab. Choose File→Manage→Database to create a Notes field (Text type) in the Lease Agreement table and then put it on the Agreement tab, too. Use the Inspector to add a vertical scroll bar to the Notes field.
Click the Payments tab and move the Payment portal and its fields to the Payments tab. Double-click the portal to view the Portal Setup dialog box and then increase the number of rows to nine. Finally, now that the tabs are filled, put copies of the Name fields above the Tab Control (you may have to reduce the height of the Tab Control to make room for the Name fields above it). That way, you know whose record you’re looking at, no matter which tab is selected.
As you just saw, you can add a Tab Control any time you need to fit more stuff on a layout. And once it’s there, you can add or delete panels or change the control’s appearance. Edit a Tab Control by double-clicking it in Layout mode to summon the Tab Control Setup dialog box.
In the Tab Control Setup dialog box, you can add new tabs by typing a name and then clicking Create. The new tab appears at the end of the list of tabs and to the right of the existing tabs in the control. You can also rename an existing tab: Select it in the list, enter a new name and then click Rename.
To delete a tab, select it in the list and then click Delete. When you delete a tab from a layout, you also delete all the objects on that tab, so be sure to move any objects you want to keep to another part of the layout first. (If the tab you select for deletion has objects on it, FileMaker warns you first, and asks whether you’re sure you know what you’re doing.)
Finally, you can control the tab panels’ order. FileMaker draws the tabs in the order they appear in the Tabs list. The leftmost tab panel is the one that appears at the top of the list, and so on. Rearrange the list using the arrows to the left of each name. FileMaker is smart enough to move the objects on each tab along with the tabs themselves when you reorder.
When you first switch to a layout but before you’ve clicked a tab, FileMaker needs to decide which tab to show automatically. You tell it which one by choosing the appropriate tab name from the Default Front Tab pop-up menu in the Tab Control Setup dialog box. While it’s possible to choose any of the tab panels, be aware that most places where tabs appear, the leftmost tab is usually the front tab. If there’s a compelling reason for a tab to always be in front when you first see a layout, it’s pretty likely that that tab should be on the left, too.
If the total width of all your tabs is less than the width of the Tab Control itself and you haven’t chosen Full justification, then FileMaker lets you choose where the grouping of tabs should be positioned. It’s a lot like aligning a paragraph of text: Choose from Left, Center, or Right, and the tabs will bunch up according to your selection.
The Tab Width pop-up menu has several choices to influence the width of the tabs:
The standard setting, Label Width, makes each tab just wide enough to hold its label, so each tab could be a different width.
Label Width Margin of adds the amount of additional space you specify around the label text. You can choose your favorite measurement system (as long as it’s either inches, centimeters, or points). The label’s text will be centered within the tab.
If you prefer all your tabs to be the same width, choose Width of Widest Label. FileMaker figures out which label is biggest, sizes that tab appropriately and then makes the others match. This setting may push some tabs out of view if they won’t all fit with the new width.
If you’d like all your tabs to be a nice consistent width, but with the ability to accommodate the odd long label, choose Minimum of. Enter a minimum width (75 points, say), and every tab will be that width, unless the label is too big to fit, in which case that one tab will widen enough so the label fits.
If you want the utmost in control and uniformity, choose Fixed Width of and then enter a width in the box. Every tab is exactly that width. If the label’s text is too big, then FileMaker cuts it off at the edges.
Out of the box, Tab Controls are matched to the theme you chose when you created the layout. But as with other objects on a layout, you can change their formatting. Use the Line and Fill tools on the Inspector’s Appearance tab (or use the Formatting bar, if you prefer). Change the setting in the Corner Radius tool to make the tabs squared off or rounded. You’ll usually format all the tabs on a single Tab Control the same way, but you can select each tab panel individually; the choices you make apply only to the currently selected tab. To select the whole Tab Control (all its tabs and all the objects on each tab), use the selection rectangle or Shift-click each tab panel.
If you don’t want a Tab Control after all, just select it and then choose Edit→Clear, or press Delete or Backspace. FileMaker warns you that it’s about to delete all objects on the tab panel as well. If that’s all right with you, click OK. If you need to keep fields or objects on the tab panels, though, click Cancel and then move the keepers off the panel for safekeeping.
When you created the Lease Document container field (see Managing Field Types), you made a simple container that stores a copy of an external file and shows an icon to let you know what type of file has been stored in the field. But it might be handy to view the contents of that file from your database. You might think you’d need to make a giant field that uses massive screen real estate. But with a popover button, you can create a temporary overlay that only appears when a user clicks that button. The rest of the time, the overlay is hidden, letting users focus on the stuff they use most frequently.
To create a popover button, switch to Layout mode and follow these steps:
Click the Agreement tab to select it. Drag the Lease Document field into the gray area to the right of the layout.
In this example, you’re going to put a popover button for the Lease Document onto the Agreement tab.
In Windows, click the arrow to the right of the Button tool. On a Mac, press and hold the Button tool in the Status bar.
A pop-up menu appears, giving you the option to create a button or popover button.
Choose “Popover button” from the menu and draw a button on the upper right of the Agreement tab.
A popover window appears, along with the Popover Setup dialog box. In the dialog box, the title field is selected (Figure 4-18). In the background, the button you created has a blinking text insertion point, which lets you add text to your button later on.
Figure 4-18. The Popover Setup dialog box automatically appears when you first create a popover button, letting you give the popover window a title and determine where it opens in relation to its button. To access the Popover Setup dialog box again later, double-click the popover window’s title bar.
In the dialog box’s title field, type Lease Document.
A row of icons in the middle of the Popover Setup dialog box lets you choose where the popover window appears in relation to the button when the user clicks the popover button.
Select the far left icon.
The popover window’s title appears at the top, and it scoots over to reflect the position you chose.
Click the button you created in the step 2 and type Lease Document. Change the text style to bold so it stands out.
If the insertion point disappears, select the Text tool and then double-click the button to get the insertion point back.
Drag the Lease Document field into the popover window. Resize it to fill the window.
If you didn’t draw your popover window large enough back in step 3, you may need to make it bigger and then resize the Lease Document field. Your popover window is complete.
Switch to Browse mode to see the popover in action. Find the record for Antoine Batiste, whose record contains a lease document file. When you click the popover button, the popover window opens, displaying a file icon—just like the Lease Document field did before you made the popover. You need to change the Lease Document field to display the file’s contents instead. Here’s how:
Switch back to Layout mode and double-click the popover button you just created.
The popover window appears with the Lease Document field inside.
Click the Lease Document field to select it and then choose the Inspector’s Data tab. In the Data Formatting section, select “Optimize for Interactive content.”
You’ve changed the field’s behavior to display the contents of the PDF file, but you must reimport the file for it to work.
Note
For the next step, you need a PDF file to insert. If you didn’t download the samples from this book’s Missing CD page, you can find them at www.missingmanuals.com/cds/fmp13mm.
Switch back to Browse mode, click the button to open the popover window and then select the Lease Document field. Choose→Insert PDF and find the file you want to import (the Missing CD folder gives you sample PDFs for the first six records in the Lease Agreement database).
The PDF document appears in the popover window. You may need to expand the popover window and the lease document field to see the PDF controls. Figure 4-19 shows how it should look.
Note
If you can’t see the PDF in your new popover window, you can try a few fixes. Make sure you’ve installed Adobe Reader (available at http://get.adobe.com/reader) and launched the program at least once, where you’ll be asked to accept the license terms. (Just installing Reader isn’t enough.) Also, make sure your web browser is set up to allow plug-ins.
Figure 4-19. Because the PDF you’ve inserted has multiple pages, the container field gives you a scroll bar to view all of them. The field’s controls also include up and down arrows for scrolling and + and – buttons for zooming. Alternatively, you can view the PDF in Adobe Reader by clicking its icon.
The Lease Agreement table’s First and Last Name fields are up above the new Tab Control so you can tell which record you’re dealing with as you switch tabs. But all the rest of your fields are on the Tab Control, and if you’re chunking data, it’s not the best idea to have some of your data entry fields up there above the Tab Control. That’s one place Merge Fields come in. You can create a text object that contains merge fields to display the data from each record’s First Name and Last Name field. A merge field is a text block containing the field’s name, surrounded by a pair of double angle brackets like this:
<<First Name>>
Besides being useful for displaying data on layouts, merge fields are often used for things like form letters, labels, or envelopes. Either way, merge fields expand and contract to use only the actual amount of space required by data inside the fields they represent. You can’t enter or edit data using a merge field, nor can they be searched in Find mode (but the Quick Find in Browse mode will search merge fields). But that’s no problem here—you have the normal First Name and Last Name fields down on the Agreement tab where you can edit and search them. In this case, a pair of merge fields help orient you as you switch tabs and are a more attractive way to display data than using normal fields would be. Start by deleting the copies of the name fields you placed above the Tab Control.
In Layout mode, select the Text tool, click in the blank space above the Tab Control and then change the formatting to 18-point bold.
The click tells FileMaker where you want the merge field to land. Setting the format here saves you a trip back to the toolbar.
Choose Insert→Merge Field, Or usethe shortcut Ctrl+M (Windows) or Option-⌘-M (Mac).
The Specify Field dialog box appears.
Double-click the First Name field and then press the space bar.
The First Name merge field, “<<First Name>>,” appears inside your text block. The space is so the Last Name doesn’t run onto the First Name.
Tip
If you know the exact name of the field(s) you want, you can type it instead of using the Insert→Merge Field command. Just be careful to get the angle brackets and name of the field exactly right. If you make a mistake, all you’ll see in Browse mode is what you typed, and not the data you expect.
Make sure that the insertion point is still blinking after the First Name and space in the text block and then Choose Insert→Merge Field again. Double-click the Last Name field.
The text “<<First Name>> <<Last Name>>” now appears inside the text block.
Click outside the text block to switch back to the pointer tool and then move the text block if necessary. Switch to Browse mode to see that the field’s contents appear inside the merge fields there.
The agreementID field is another good candidate for a merge field, since it can be useful to see the agreement number. Make a text block near the top of the layout and use Agreement ID <<agreementID>> for its contents. Align the text to the right, put it at the right side of the header, and make sure it lines up with the merge field showing the name.
You can format merge fields just like any other text block. But since they also contain data, if you apply formatting from the Inspector’s Data tab to text blocks that contain merge fields, FileMaker displays the data according to your formatting. So if you put a merge version of the Rental Fee field on the Payment tab (so you can see what the Rental Fee is supposed to be as you record each payment), don’t forget to format the text block as currency.
You can use merge fields to create a form letter (you’ll get one copy for each record in the found set, with appropriate data for each record). Just create a new layout, select your context and then type the text of the letter inside a large text block, and insert merge fields within the text as appropriate. Because your new layout shows you records from a specific table occurrence, you’ll never have to run another “merge” operation. Just find the records for the folks who should get a letter and then print all the records in your found set (Creating Layouts for Printing). You’ll also see heavy use of merge fields if you use the Layout/Report Assistant to create label or envelope layouts for sending out your merged letters.
Merge fields aren’t the only things FileMaker can use to show dynamic information. You use one of a handful of special symbols—stand-in characters that are replaced with info when you view your database in Browse, Layout, or Preview mode. For example, see Figure 4-20, where each record on the Lease Agreement List layout is numbered. The record number symbol displays an automatic number for each record.
Note
FileMaker offers a host of other symbols besides the record number symbol. See the box on Other Symbols for details.
To add a record number to your Lease Agreement List layout, you’ll need to slide your fields and their labels to the right to make space. Then click the Text tool to select it and click in the space you’ve just made. Choose Insert→Record Number Symbol. You now have a text object that contains “{{RecordNumber}}”. It takes up way more space than it needs to in Layout mode, but it’ll be fine in Browse mode. It’s annoying having it cover part of the First Name field, so select the text block and then choose Arrange→“Send to Back” to place it behind the name field. It’ll work just fine there without being distracting. If you want to format the record number, just select it and apply the changes you want. You don’t need to bring it forward first. Switch to Browse mode, where you see that FileMaker puts the current record number in place of the symbol.
As with Merge Fields, you can insert symbols into existing text objects. Just click in the text object first, as if you’re going to type. Then, when you choose Insert→Record Number Symbol, FileMaker adds the record number symbol to the existing text.
Figure 4-20. The number to the left of each record on the Lease Agreement List layout comes from a special symbol placed on the layout. Sort the list, and notice that the records change order, but the record numbers themselves stay in sequence. The record number is meant to help you figure out where the record is ordered in a list, but not to identify any specific record. See page 126 for a way to assign a permanent ID number, or key, to a record.
Now that you have a record number on the Lease Agreement List layout, and have sorted the list, you can start to see how useful the layout really is. The Lease Agreement layout is great for revealing detail, but when the Lease Agreement List layout is sorted by Last Name, it’s a cinch to scroll to the record you need without entering Find mode, typing in search criteria and then performing a find. But it could be easier to sort the records on the layout. As it is, you have to choose Records→Sort Records, select the field you want to sort by and then click Sort. Doing this routine is easy, but not a good use of your time. The solution is to write a script to do these things automatically.
If you’re familiar with macros in other programs, then you already have the idea of scripts in FileMaker—you set up scripts to perform tasks for you. The task at hand—sorting—is just one command, but it has several steps. They’re all quick steps, but when you have to repeat them several times a day and so does everyone else in your office, all that wasted time adds up to real inefficiency. Also, any manual process leaves room for human error. When you make a mistake, no matter how harmless, you have to undo or redo what you just did. A script that handles your sort is more efficient and less susceptible to error.
Here’s how to write a script that sorts the records on your list layout alphabetically by Last Name and then First Name:
Choose Records→Sort Records. Set up the window to sort by Last Name and then First Name (Sorting Records) and then click Sort.
Every time you open the Sort window, your most recent sort order is already in the window. So save yourself some time by performing the sort before you start creating your script. That way, the order will already be in the Sort window.
Choose Scripts→Manage Scripts.
The Manage Scripts dialog box appears.
Click New.
The Edit Script dialog box appears (Figure 4-21).
Figure 4-21. The Edit Script window contains everything you need to write a script. Here the list of available script steps is filtered by View: Found Sets, but you can also sort all the script steps alphabetically if you prefer. Many of the script steps available are the same as the commands in FileMaker’s menus. If you know how to use those commands manually, you know how they’ll behave as script steps. But there are some commands that you can access only through scripting, and the subject is so deep and wide that this book has three Chapters—11, 12, and 17—devoted to the subject.
In the Script Name field, type Sort by Last Name.
Always use descriptive names in FileMaker. Mature databases can have hundreds of scripts, so good naming is the first step in keeping things organized.
In the View pop-up menu, choose Found Sets.
The list of script steps is now filtered so you can easily pick the one you need.
Double-click the Sort Records script step.
The Sort Records script step appears in the window’s Script pane on the right.
In the Options section below the Script pane, turn on “Perform without dialog.”
Without this option selected, you’d see the regular Sort window every time you run the script. Don’t turn on this option when you’re writing a script that lets the user choose a custom sort as the script runs.
Turn on “Specify sort order.”
The regular Sort window appears, with Last Name and then First Name set up already. As you know, FileMaker remembers your most recent sort order, but it’s good practice to verify everything when you’re scripting. And if you want a different sort order, you can change it and the script will remember your changes.
Click OK to close the Sort window and then, in the Edit Script window, click the Close button.
Click Save.
FileMaker saves the script and closes the Edit Script window.
Make sure “Include in Menu” is turned on for the “Sort by Last Name” script and then close the Manage Scripts window.
Now that the Manage Scripts window is closed, you need some way to run the script you just wrote. Look in the Scripts menu. It appears there, along with a shortcut. You can run the script by choosing Scripts→“Sort by Last Name” or by using the shortcut.
Tip
Windows users can save scripts before closing the Edit Script window by choosing the Edit Script window’s File→Save Script command. Both PC and Mac fans can use the Save Script shortcut Ctrl+S (⌘-S).
You’ve finished writing the script and it’s time to test it to see if it works as intended. Unsort your records (choose Records→Unsort) and then run the script.
Running the script from the menu saved you a few steps, but you can make it even more convenient by attaching the script to a layout object, which then becomes a button. Then whenever you click the button in Browse mode, the script runs automatically. Here’s how:
In Layout mode, click the Last Name Field’s label to select it.
It’s a common convention to click a column label to sort a column, so help your users out by adopting that principle.
Choose Format→Button Setup.
The Button Setup window appears (Figure 4-22).
In the script step list, click Perform Script.
This “controller” script step lets you run any script you’ve written by attaching it to a button.
In the Options section of the window, click Specify.
The Specify Script window appears, showing a list of all the scripts in your database. You’ve only got one, but it’s not uncommon to have hundreds.
Click the “Sort by Last Name” script and then click OK until you’re back on your layout.
Your button is ready to use.
Figure 4-22. The Button Setup window lets you choose from most of the same steps you see in the Edit Script window. The difference is that you can only choose a single script step when you define a button this way. Any time you need a process that requires two or more script steps, create a script and then attach it to the button. But even if the process is a single step, you may still want a script, so you can set it to appear in the Scripts menu. Even better, if you apply a script to several buttons throughout your database, you can change the script, and all the buttons will run the edited script automatically. But if you had attached a single action to each of those buttons instead of a script, you’d have to change each one manually.
Switch to Browse mode and then Unsort your records, if they’re sorted. Finally, click the Last Name field label to see the script run. You haven’t put anything on your layout to indicate to your users that the field label does anything useful. FileMaker changes the pointer to a hand icon when it’s positioned over any button, but you have to give users a visual clue that there’s something useful besides text. So change the label’s formatting (make it a contrasting color, or put a border around it so that it looks like a button) to help users out. (Learn more about buttons on Buttons.)
The script was nice, and the button improved things, but you’re still not done learning how useful and intuitive scripts can be. Since the point of going to the List layout is to quickly scan a list so you can find a particular Lease Agreement record, it’d be even more convenient if the list just knew to sort itself every time you switch to the layout. And that kind of thing is what Script Triggers are for.
You’ve just seen that you can run a script from the Scripts menu or from a button. But you can also tell a script to run when you do other things, like enter data in a field or go to a specific layout. Here’s how to make the “Sort by Last Name” script run every time you go to the Lease Agreement List layout:
On the Lease Agreement List layout, switch to Layout mode and then choose Layouts→Layout Setup.
You’ll learn about this dialog box’s other options in Chapter 7. For now, you’re interested in the Script Triggers tab.
Click the Script Triggers tab.
The Script Triggers tab appears (Figure 4-23).
Select the OnLayoutEnter option in the Event list. Windows users may need to scroll the list to see that option.
Selecting an Event tells FileMaker when to run a script. Once you make a selection, the Specify Script window appears.
Click the “Sort by Last Name” script to select it and then click OK.
The Script Trigger tab is now set up. Notice that you’re letting the script run only while you’re viewing the layout in Browse mode.
Click OK.
The script will run each time you switch to the Lease Agreement List layout.
To test the script trigger, switch to Browse mode and unsort your list and then switch to the Lease Agreement layout. Then switch back to the Lease Agreement List layout. The script runs and sorts your list for you.
Figure 4-23. Script triggers give you a more automated way to run a script than by using the menu or creating a button. This script trigger will run a script called “Sort by Last Name” every time you view the layout in Browse mode. When you apply a script trigger with the Layout Setup dialog box, it affects only the layout you apply it to. Script triggers are enormously powerful, but they can be tricky. Learn more about them on page 425.
Your database is getting pretty smart now. It can do math and perform some housekeeping duties on its own. But one of the main purposes of storing data is to be able to analyze it. You’re storing information about lease agreements, but so far, there’s no way to take a look at any trends that might show up. For instance, you offer leases of 12, 24, or 36 months. If you sort and count your leases by duration, you may be able to spot interesting trends, like people who are willing to sign longer leases are also willing to lease your more expensive properties, for example. Or, maybe the opposite is true and they’re less willing. If so, you’ll want to come up with some incentives to get the high rollers to sign longer leases. But you’ll never know until you create a report.
You’ve already seen how the Layout/Report Assistant makes it a breeze to create a new layout (Creating a New Layout). Many of the assistant’s panels are already familiar to you. But the assistant can also build some special layout parts and create fields for you that summarize your data. Better still, the report you’ll build is dynamic. If you add a new record to the list while you’re viewing the onscreen report, the record is automatically sorted into place, and your summary data updates immediately. Here’s how to create a dynamic report:
In Layout mode, choose Layouts→New Layout/Report.
The New Layout/Report dialog box appears.
In the “Show records from” pop-up menu, make sure the Lease Agreement table is selected. In the Layout Name box, type Lease Agreement Report. Click the Computer icon and then choose Report from the layout type list in the lower half of the screen. Click Continue.
After you click Continue, the “Include Subtotals and Grand Totals” panel appears.
If they’re not already selected, choose “Include subtotals” and “Include grand totals,” and then click Next.
After you click Next, the Specify Fields panel appears.
Move the First Name, Last Name, Rental Fee, and Lease Duration fields to the “Fields shown on layout/report” box and then click Next.
This should be familiar territory by now. Remember that you can use the arrow to the left of each field to move them up and down in the list on the right. After you click Next, the “Organize Records by Category” panel appears.
Move the Lease Duration field into the Report categories list and then click Next.
The sample report icon changes as you move fields into the Report categories list. After you click Next, the Sort Records panel appears.
Move the Last Name and First Name fields into the Sort order list and then click Next.
Lease Duration is already in the list, because that’s how the report will categorize the list. But you want records with the same Lease Duration value to be sorted alphabetically by Last Name and then by First Name. After you click Next, the Specify Subtotals panel appears (Figure 4-24).
From the Subtotal Placement pop-up menu, choose “Above record group” and then click Specify under “Summary field.”
The Specify Field window appears.
Click Add.
The “Options for Summary Field” window appears (Figure 4-25). You’ll create a special field that counts the records in each category. The field will appear above the record group it summarizes because of the choice you made in step 7.
Figure 4-24. Choices you make in this window determine how many subtotals your report will have. You can also place the subtotal above or below the records it’s summarizing. You need to have at least one Subtotal line item in the Subtotals box at the bottom for the summary to work, though, so make sure you click Add Subtotal after you create or choose a summary field (you’ll learn about them later in this tutorial).
In the Summary Field Name box, type Count Leases and then select “Count of.” Now choose Lease Duration from the “Choose field to summarize” list and then click OK until you’re back on the Specify Subtotals panel. Now click Add Subtotal. Click Next.
FileMaker creates a new Count Leases field (Summary type) and adds it to the Subtotals list. At the end of this process, you’ll see a Subsummary part (you’ll learn how to create them manually on Summary Fields and Subsummary Parts) based on the options you just chose. The summary field counts each record in the sorted category group you selected and displays a count of records for each group in its own Subsummary part. Make sure the Lease Agreement::Count Leases field appears in the Subtotals list at the bottom of the window. After you click Next, the Specify Grand Totals window appears.
Click Specify.
The Specify Field window appears. The Count Leases field you just created appears in the list.
Click the Count Leases field to select it and then click OK. Leave the “Grand total placement” pop-up menu set to “End of report,” and then click Add Grand Total. Make sure the Lease Agreement::Count Leases field appears in the Grand Totals list at the bottom of the window. Finally, click Next.
Summary fields are smart enough to display different data depending on the layout part they’re placed in. So you can use the same summary field in a Subsummary part and a Grand Total part, and it will display appropriate data in each part. This version of the Count Leases field will appear at the end of the report and will give you a grand total count of all the records you’re viewing. After you click Next, the “Header and Footer Information” window appears.
Figure 4-25. You’d see a nearly identical window if you used the Manage Database window to create a summary field. Summary fields do just what their name implies: They summarize groups of data. You can apply one of several mathematical operations to the fields, including Totals, Averages, and Counts. You can count any field, but you can’t apply math to text fields. So if you’re trying to select a field, but it’s grayed out, check the operation you’re trying to perform. It may not be the right option for the field you want to summarize (or the field’s definition may be set to the wrong type).
From the Header’s “Top right” pop-up menu, choose Layout Name. From the Footer’s “Bottom right” pop-up menu, choose Current Date. Then click Next.
The “Create a Script for this Report” window appears.
Click the “Create a script” option, and Lease Agreement Report appears in the “Script name” box. Select the “Run script automatically” option and then click Finish. FileMaker writes a script for you that goes to the report layout and sorts your records properly (the data in Subsummary parts doesn’t show up unless the records are sorted by the field specified in their definition). The “Run script automatically” option attaches an OnLayoutEnter Script Trigger (explained on OnLayoutKeystroke) to the new report layout so that you don’t have to remember to run the script every time you switch to the report layout.
Click Finish and then switch to Browse mode.
Your report appears, but the subtotals don’t show up. The script wasn’t triggered because you weren’t in the Lease Agreement Report layout, even though you just created it.
Switch to the Lease Agreement layout, then switch back to the Lease Agreement Report layout.
Your sorted report appears (Figure 4-26)
Figure 4-26. Graphically speaking, the report as created is nothing to write home about, so you’ll need to put your layout design skills to use on this layout. However, the Layout Assistant gave you some nice tools to help you start analyzing your data. You can see that your current group of tenants trend toward longer leases. And it looks like your higher-priced properties are leased out longer, too. Except for that one guy with the $1,850 lease who’s only signing on for 12 months. (While you’re learning FileMaker, have your assistant find out what it’ll take to get that guy signed up for another couple of years.)
There were a lot of steps and a lot of selections to make. But the hard work’s been done for you. In Chapter 15, you’ll learn how to create a sorted subsummary report completely by hand. Once you’ve done that, you’ll appreciate how much easier it was to make selections in an assistant. However, you need to format your fields and generally beautify the layout so that it matches the rest of your database.
When you’ve got the layout looking fine, switch to Browse mode so you can see how the report updates dynamically. For example, click the Toni Bernette record to select it and then click the New Record button in your Status toolbar. Finally enter the following data in the new record:
First Name: Janette
Last Name: Desautel
Rental Fee: 1295
Lease Duration: 12
Your new record is created right below your active record. When you commit the new record, it’s sorted into the 12-month group, and the Count Leases field is updated immediately in both the Subsummary and Grand Total parts. The same thing is true if you edit data in the field on which the sort is based (remember that this report is always sorted by the Lease Duration field and then by Last Name and First Name). Change some data in a Lease Duration field and then commit the record. It will sort into the proper group. If you add data that’s not in an existing group (say you type 48 in the Lease Duration field), a new group will be formed with a Count of “1.”
Subsummary layouts show their summary data only when your records are sorted by the field that’s attached to the Subsummary part (explained on Summary Fields and Subsummary Parts). So if you unsort your records, or do a sort that doesn’t include the Subsummary part’s field (say you sorted by Last Name and First Name only), the groups and subtotals don’t show on the layout. To get them back, sort the records again, and this time, make sure you include the proper field (in this case, it’s Lease Duration) in the sort order.
Note
Sorted subsummary reports are great for looking at trends in your data, but you probably wouldn’t usually use them as the primary way to add new records or edit existing ones. But on those occasions when it’s suitable (say the sorted report makes it clear that some data wasn’t entered correctly), it’s convenient that you don’t have to leave the report layout to make corrections.
The dynamic report you just created is perfect for printing out when you want to analyze your data. Even if they don’t want to print the report, your users can just switch to the Lease Agreement Report layout and get an up-to-the-minute categorized report on your properties. But what if they need a quick analysis of the data using a category that you haven’t set up for them? Do they have to wait until you have time to add a new report? Or maybe you need a last-minute, one-time report that won’t be printed (you don’t even have time—there’s a meeting in 5 minutes and you’ve been told to get the data), so you don’t want to bother creating a new layout and spending time making it match the rest of your database. Either way, FileMaker’s Trailing Group reporting feature is the solution.
A Trailing Group report requires a Table view on a layout that shows records from the table you want to report on. It accomplishes the same thing as a dynamic subsummary (sorts records automatically by the category you choose, with an optional summary field), but it’s temporary and doesn’t actually add a Subsummary part to the layout. To start this tutorial, switch to the Lease Agreement layout and then click the Table View button.
Click the triangle to the right of the Lease Duration field’s column heading. (The triangle appears when you place your mouse over the heading.) Choose “Add Trailing Group by Lease Duration” (Figure 4-27).
The Lease Duration column’s pop-up menu appears when you click the triangle and then when you choose the Trailing Group command, a gray summary row appears on the table and the records are automatically sorted by Lease Duration.
Figure 4-27. The contextual menu for column heads in table view lets you create quick Trailing Group reports, but it also lets you change the field’s type or options, delete the field, add new summary fields, or sort the records you’re viewing. You can even change the way the Table view behaves by adding or deleting fields from the layout or changing a column’s width. In this case, the “Trailing Group by Lease Duration” field would disappear from Table View. But if you’ve created a summary field for the Trailing Group, the field isn’t deleted from the table.
From the Lease Duration column’s pop-up menu, choose Trailing Subtotals→Count (Count Leases).
The count appears in the summary row below each Trailing Group.
FileMaker sorts and groups the records for you, so you can get the information you need from the ad hoc report without fuss. As with the dynamic report in the previous section, the records must be sorted by the Trailing Group you chose for the summaries to show up. So if you switch back to Form View and sort your records another way, you won’t see the Trailing Group Report when you return to Table View. Select Sort Ascending from the Lease Duration column pop-up menu to sort the records, and the trailing groups reappear.
If you switch to Layout mode, you’ll see that the layout doesn’t have a new part added to it. Nor does the Count field actually appear on the layout. That’s why this type of report is temporary. It’s meant to let you get a quick, bird’s-eye view of your data and then get right back into your other tasks (or to the meeting on time and with the data you were told to have at your fingertips).
You can get creative with your ad hoc reports by adding multiple trailing groups at the same time. In a small database like the one you’re working on, you may find that nearly every record gets its own group. But in a database with lots of records, you can use this technique to get fine-grained reports very quickly. Remove a Trailing Group by clicking the column head menu associated with that group. Choose “Remove Trailing Group by Lease Duration,” and the group no longer shows up.
Text-based reports, whether they’re meant for print or screen, are not the be-all and end-all of data analysis. Most people find information easier to read in the form of a chart over a bunch of numbers. So FileMaker gives you a quick method for creating charts from Table view. To see it in action, on the Lease Agreement layout, start in Table view.
From the Lease Value column head’s pop-up menu, choose Chart→“Chart by Lease Value.”
The Chart Setup dialog box appears showing a column chart of the data in the Lease Value column.
Click “Save as Layout,” and then accept the name suggested by the “Save as Layout” dialog box. Click OK.
FileMaker creates a new layout showing a chart of all your lease agreement values and switches to the new layout so you can see the chart.
FileMaker creates a column chart by using the context of Lease Agreement and the Lease Value field. If FileMaker created it too large to see all at once, you can resize it like any other object. Currently, the chart displays all the records from the table, so if you had hundreds of records, this chart wouldn’t be very useful. But there’s a quick trick for making it show you just some of your records. As you might expect, the solution is found in Layout Mode. All you need to do is put a field on the layout so you can do a search to limit the number of records in the found set.
Switch to Layout mode, click the Field tool to select it and then drag to create a field down in the gray area to the right of the chart.
If you’ve resized your chart and didn’t resize the layout to match, there will be a light gray area (the original dimensions of the chart) and a darker gray area. You need to drag the field into the darker gray area.
Anything in the gray area doesn’t appear when you’re in Browse mode. When you release the mouse, the Specify Field window appears.
From the Specify Field window, choose Lease Duration, click OK and then switch to Browse Mode.
The chart looks just like it did before, but now you have a way to change the records you’re viewing.
In the Quick Find field (Figure 4-28), type 36 and then press Enter.
FileMaker searches the only field on the layout (Lease Duration — even though it’s hidden in the grey area, the Quick Find features still knows it’s there and can search its contents) and finds the records with 36-month leases. The chart changes dynamically with the found set.
You know that FileMaker creates a lot of elements for you when you first create a new database. One thing you may not know is that it even created a login account for you and it assigned that account name to be entered automatically each time you open the file. You can see that setting and then turn it off, by choosing File→File Options (Figure 4-29).
Figure 4-29. It’s not industrial-strength security, but FileMaker created an Admin account for you and has been secretly using it every time you open the Lease Agreement database. Really it’s not that much of a secret, because you have to figure that at least half of FileMaker’s millions of users know about this account and the hackers among them would try the “Admin” account name (it doesn’t even have a password), if they wanted to break into your database. One of the first things you should do to make your database more secure is to turn off the “Log in using” option of this window. Then change the default account to a custom name and password.
This default account is created in every database FileMaker creates, so to protect your new database from prying eyes, turn off the option to log in using the automatic account and then change the default account’s name and password. Here’s how to change the default account:
Choose File→Manage→Security.
The Manage Security window appears. It shows two built-in accounts, but only the Admin account is active, as shown by the checkmark in the Active column.
Click the Admin account and then click Edit.
The Edit Account window appears (Figure 4-30).
In the account box, type your first initial and last name.
This first initial/last name scheme is commonly used for creating account names. But you can use whatever scheme you like, so long as you can remember it.
In the password box, type a password.
You know the drill by now: Create a password that has a mix of letters and cases. Use some numbers and, for extra strength, even a symbol or two. Just make sure you remember exactly what you typed,
Figure 4-30. Account Names appear in the Account Name box just as you type them, but characters in the password box are obscured by a password font. That keeps your typing safe from someone who may be looking at your computer screen over your shoulder, but it does mean that you have to be very careful as you type. Because once you add a password to an account, even you can never see what was entered in the box. (Go to Chapter 18 to read all about security.)
Click OK to close the Edit Account dialog box.
You’ve just changed the Full Access account for the file.
Click OK to close the Manage Security dialog box.
Type the Account Name and Password to confirm that you want to make change to the file’s security.
You’ll have to type this information every time you make a change to your file’s security. Chapter 18 gives you the full scoop on this important topic. If you can’t quite get the combination right, go back into the account you’ve just edited and then retype your password. Once you get the combo correct, the window closes, and you’re back on your layout.
You know that you’ve got the account name and password right, because you were able to close the window. But you should test the new login account by closing the file (if you still have multiple windows open, make sure you close them all) and then opening the file again. This time, you’ll be asked to enter the new account name and password.
FileMaker guesses that the account name used for each file it opens is the same as the name you entered when you first installed FileMaker. But you can change this name in the program’s Preferences window. Choose Edit→Preferences (Windows) or FileMaker Pro→Preferences (Mac) to get the window shown in Figure 4-31.
Figure 4-31. Type the account name you want to appear in the User Name box, and FileMaker will use that as the automatic account name from now on. On the Mac these options are a little different. You can choose from the System’s Admin Account name or choose Other and then type a custom name. Either way, it saves you a few keystrokes every time you open the database.
Over the last two chapters, you’ve created a database from scratch. In that process, you’ve learned the basics of FileMaker’s major features. You know how to create tables and fields, and you can create relationships between them. You can create layouts and layout objects, and you can change the format of the most common layout objects. You’ve even tried your hand at writing a script, creating some data analysis reports, and adding security to your file. Before you delve deeper into these topics, it’s time to switch gears and learn more about planning a database. In the next part, you’ll learn how to think like a database developer.
Get FileMaker Pro 13: The Missing Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.