Integrating A Mini Calendar
By Matt Petrowsky (matt@filemakermagazine.com)

BONUS FILE: CALDATE.FP3 & CALENDAR.FP3
PLATFORM:
Macintosh & Windows
RATING:
Intermediate

After receiving great feedback, I've decided to continue offering the "Copy & Paste Scripts" feature. These are scripts ready to copy and paste into your own solution. This month's offering will help navigate you through the trials of dealing with FileMaker Pro and creating dates and calendars.

Calendar Solution Objective

One of the most often requested feature for FileMaker Pro is a built in calendar. As you can imagine, since the early days of FileMaker, there have been many calendars created. Most all of them used the older Today function or required a reference to a stored value which made them quite slow. I've seen many solutions using a calendar and remember dredging through the whole process of wondering how the whole thing worked.

After days of work, I finally produced a calendar. It was nothing compared to the calendars that can be created today. With FileMaker Pro v3 the calendar became an easy function to offer. This solution offers the user a button in the interface, which when clicked, will bring up a calendar window so the user can select a certain date. Once the date is selected then a script is run which moves the selected date to the field from which the button was clicked.

    Editorial note:

Solution Fields Needed

The fields and scripts contained here can be copied and pasted (in whole or part) directly into your own solution. When duplicating scripts you will need to add the steps indicated in the scripts. The following are the minimum fields needed for the solution. You will need your primary database file for which a date field exists.

Field Name (field type and options)

Date Field    (Date)
Record ID    (Calculation, Number) = Status(CurrentRecordID)
_Constant    (Calculation, Number) = 1

Solution Relationships Needed

You will need a total of two relationships to accomplish this solution. First, you need a data path to the Calendar.FP3 file. This happens through the use of a Constant field. A constant field is a calculation that results in a perpetual value of 1. This means that on both sides of the relationship 1 will always equal 1 so you will have access to all fields, global or otherwise. It is important to note that you only want to adjust values in global fields using a constant based data path. The objective is to send the Record ID of the current record over to the Calendar.FP3 file so it will know which record needs to receive the date when it is selected in the Calendar file.

    Quick TIP: It should be noted that this solution allows the Calendar.FP3 to serve a single function. This is know as File Specialization, wherein a file is designed to do just one thing. That one file should handle most all of the tasks specific to what it does and other files should simply call it and ask for information.

Another important thing to know is that if you are going to set the Record ID of the current record in your main file over to the Calendar.FP3, you need to allow the Calendar.FP3 to be able to send the selected date back to your primary file. This happens through a relationship based on the Record ID of the record from which the Choose Date button was clicked.

Relationship 1: From Your File to Calendar.FP3

Name: CALENDAR_Constant
Fields: _Constant = ::_Constant
Target: CALENDAR.FP3

Relationship 2: From Calendar.FP3 to Your File

Name: MY FILE_Target Record
Fields: Target Record = ::Record ID
Target: MY_FILE.FP3


Script Names for Your File

Highlight each of the following, then copy and paste the script names into your own solution. There is only one script so this should be easy.

Choose Date

Script Steps for the Choose Date script
Information about the script below...

Comment ["Using a constant path to the calendar file we set the Record ID so the calendar file, when a day is selected, knows which record to target with its Target Record field.
Set Field ["CALENDAR_Constant::Target Record", "Record ID"]
Perform Script [Sub-scripts, External: "CALENDAR.FP3"]

This is the script attached to the button in your interface which has the calendar icon on it and reads something to the effect of Choose Date...

When this script is run then the current Record ID is set over into the Calendar.FP3 file. The field name used is the Target Record field in the Calendar.FP3 file. The second step in the script simply performs an External script over in the Calendar.FP3 and this script is the step that causes the Calendar window to come forward and go to the Month layout.

Script Names for Calendar.FP3

You don't need to create these scripts because they already exist in the Calendar.FP3 file which you should use to hook up to your own solutions. There is no need to reinvent the wheel by creating your own calendar. Use the one provided and enjoy the time you save!

Selecting a date in Calendar.FP3
Information about script below...

Set Field ["Selected Date", "Case( not IsEmpty(Sunday 1), Date(Month Number, Sunday 1, Year), "" )"]
Perform Script [Sub-scripts, "My Action Script"]

The first part of the Select date script simply sets a field named Selected Date to a date of the month, day and year being selected. Once the date is set into this field another script is called. The script which is performed is named My Action Script. The reason this is done in this manner is so that changes are universal to each Select script in the Calendar.FP3 file. To change what each script does would require a repetition of efforts. By simply calling another script you can change anything you wish in the My Action Script and the changes will be universal in all of the individual Select date scripts.

In the BONUS file you see the following steps in the script My Action Script.

If ["not IsEmpty(Selected Date)"]
Set Field ["CALDATE_Target Record::Date Fie…", "Selected Date"]
Toggle Window [Hide]
End If

What the script is doing is determining if a value date is in the Selected Date field in order to do anything. If the user clicks on a date in which there is no day value then nothing should happen. However, if there is a valid date in the Selected Datefield then the Calendar.FP3 sets this selected date into the field in the primary file and hides the Calendar.FP3 window.

Enhancing this solution and closing

Using this solution of integrating a calendar has been simplified for the BONUS file. The true implementation which I use for my own files utilizes a bit more complexity. The ultimate objective with a calendar button is so it can be used on any field which is a date or those that are desired to have the function. The problem with attaching the My Action Script to a certain field is that only that field would be able to use this cool feature. However, with some slight modification you can use conditional to watch for which field should receive the Selected Date value. Above and beyond this you can use the Calendar file as a feature across all of your files. For adept developers the following suggestion will make sense.

To implement across multiple files and multiple fields, all that's needed is to know which file called the Calendar.FP3 file and which field needs to receive the selected date. To do this you can use the Status(CurrentFileName) function and the name of the field by setting them into a Variables field which you would need to create in the Calendar.FP3 file. You would then modify the My Action Script in the Calendar.FP3 to use conditional IF statements to determine which file the Calendar.FP3 file would send the date to. It's quite simple and very powerful!

If you need further assistance, we recommend starting with the FileMaker Pro manual or a third party book which instructs you on ScriptMaker. (If you haven't already, check out "Scriptology: FileMaker Pro Demystified" which is a great resource book. Visit www.scriptology.com for more information!)

Editor's Note: With the introduction of this new article format, we would greatly appreciate your feedback. If you enjoy this format or have comments or suggestions, please email them to editor@isoproductions.com.

Happy FileMaking!
Matt Petrowsky

## END ##