February FileMaker Functions - Char and Code
Monday, February 1st, 2010The Char and Code functions were introduced in FileMaker Pro 10. Char converts a given number into a Unicode character and return the resulting text. Code converts text and keystrokes into a Unicode value.
-
Char ( number )
- Input: number
- Output: text
- Input: text
- Output: number
Code ( text )
Examples of Use
1. White Space Characters
The Char function allows you to use 'white space' characters such as spaces, tabs and returns more easily in your calculation expressions. While FileMaker provides a symbol (ΒΆ) for a carriage return, no such symbols are available for a tab or a space.
We can now construct a calculation that includes tab characters. The Unicode value for a (horizontal) tab character is 9. So we can write:
first_name & Char(9) & last_name & Char(9) & suburb
This will return a tab separated string of text from a series of fields.
We can also substitute out unwanted characters from a field like this:
Substitute ( last name ; [Char(9) ; Char(0)]; [Char(13) ; Char(0)]; [Char(44) ; Char(0)] )
This will substitute Null for a tab character (9), return character (13) or a comma (44).
2. Keystroke Detection
FileMaker Pro 10 brought us script triggers. Two of these involve keystroke detection where a script can be triggered by a keystroke. Depending on what key was pressed, we can script an action. For example, if the user presses one of the arrow keys, we might script record navigation to the next or previous record. In our script, we need to use the Char function to detect which arrow key has been pressed - left, up, right or down. Each of these keys has a Unicode decimal number - 28, 29, 30 and 31 respectively.
If we want to script an action for pressing an arrow key, we could write:
If [ Get (ActiveFieldName) = Char (0) // No active field ]
If [ Code(Get(TriggerKeystroke))=28 or
Code (Get(TriggerKeystroke))=29 // left or up ]
Go to Record/Request/Page [ Previous ]
Exit Script [ Result: False ]
Else If [ Code(Get(TriggerKeystroke))=30 or
Code(Get(TriggerKeystroke))=31 // right or down]
Go to Record/Request/Page [ Next ]
Exit Script [ Result: False ]
End If
End If
This script will be set to trigger OnLayoutKeystroke. When it does, it first tests if there is an active field (if the cursor is in a field). If so, nothing happens because we want the arrow keys to obey their usual behaviour in moving within the field. If no field is active, the trigger keystroke is tested. If the converted code is between 28 and 31 (an arrow key), record navigation occurs.
For each case of arrow key detection, the script exits with a result of False. Since OnLayoutKeystroke is a pre-event trigger, this means that the keystroke is cancelled therefore having no subsequent effect after the script.
3. Generate a Random Character
Since characters can be referenced by a decimal number, we can use the Random function to generate random characters within a range. If we write:
Char ( Int (Random * 26) + 65 )
this will return a random character in the range 65-90 which defines all the capital letters of the alphabet.




