Global Fields
Global fields are quite different from the others, mostly because they remain the same for, and are accessible to all records. They can be used to store things which are static, such as the owner's name or logo, or to temporarily hold values that are changing quickly, such as during script steps.
One great advantage of using global fields for holding temporary info is that it doesn't use the clipboard. This allows multiple values to be stored in separate globals (or in one with repeats). It's also faster in Scripts to Set a field to a global than it is to copy/paste data into one.
One thing globals are not, however, is indexable. This isn't too big a problem. If you only have one value, and it's available to all records, there's little need for indexing. It does limit their use, however. Any field that references a global can't be indexed, therefore can't be used as the destination of a relationship (though a global can be used as the originating field, and is not slow in this case). A calculation using a global will have to recalculate whenever it appears on a layout, as will any calculations that depend on it. Everything can slow to a crawl (as in above paragraph on kinky indexing).
Globals are indispensable in Scripts, to store values which would otherwise become unavailable during operations, such as data, layout location, found records, etc.. They are not slow here either. They can be also be used to enter data for operations, while in Browse mode, to use for Find or Replace operations, without changing data in the records.
Search and Replace
Here's how to make your own Search and Replace operation using globals. There isn't a regular one, like in a word processor, or even a spreadsheet. That's just as well, as you can imagine the havoc an undoable Replace affecting the entire database could cause.
We'll make one that works on one field at a time, on one or multiple records, but only on the found set. Most operations (even Delete All) only take place on the found records.
First you need to make a blank layout to work on and create the following global fields:
Search_g
Replace_g
Put them on the layout, labelled Find and Replace. Your field should also be there, clearly labelled.
[Some people put the "g" in front, so they can sort fields alphabetically and separate the types of fields. I put it in back, so I can sort and see them next to the regular fields (which they often "mimic"). I can sort by Field Type if I want to separate them, and/or create my own Custom sort. Either naming convention is fine; but you should use one consistently.]
Script
Now you've got to make a script. Go up to ScriptMaker and create a new script. Delete all the steps. Choose the following steps:
Go to Layout ["SearchandReplace"]
Set Field ["Search_g", """"]
Set Field ["Replace_g", """"]
Pause/Resume Script
Set Field ["Field Name,"Substitute(Field Name, Search_g, Replace_g)"]
The first two Set Field steps just clear out whatever was in the boxes from the last time. The Pause waits for the user to type something in the boxes, then hit the Enter key. You could also have a button to run the script (which would be safer), and check that they'd typed something before running. Especially if you use the Replace step, which works on the entire found set. It is not undoable. They could wipe out all values in a field in all the found records.
Substitute is a calculation function, available under Text functions in the Specify Calculation dialog box, which appears after hitting Specify. Select the generic text, then double-click the field names to fill them into the formula instead. It's easier than it sounds.
Now make a button to run the script.
If you want it to work on the same field in multiple found records you would use the Replace step instead, with the "by calculation" radio button, using the same substitute formula as above. The whole thing would then look like:
Replace ["Field Name", "Substitute(Field Name, Search_g, Replace_g)"]
You can check the "No dialog" after running once to test it. Remember it's not undoable, and that the field to be Replace into must be on the layout (not necessary for Set Field, but it is for Replace).
If you want to use it on other fields, just duplicate the script, substituting the new field name in both the Specify field places. You can use the same global fields and layout; just make new labelled buttons for each script, next to the regular field that is being Replaced into.
To do all the fields at once just put duplicate Replace steps, each specifying a different field on the layout, one after the other in only the one script. Then you only need one button (obviously).
As you can see, Set Field and Replace are similar. The first operates on one record at a time. It can be scripted to act on several records with the Loop step (later), but it still only operates on one record at a time. Replace does all the found records at once and is very much faster.
The above script could also include the Go To Layout [original layout] as the last step.That way you can call the same script from different layouts and return; just like a word processor.
Undo
Another great use of a global field, which is otherwise unavailable, is to create an "undo" script. I use it to store the text of a record while running a more complex text calculation (which searches for and deletes characters, such as ">" marks and unnecessary hard returns, from imported email messages). Since it sometimes makes things worse rather than better, I wanted to add my own undo step.
So I have the script set the text into a global field first (only one record, one field). Then I have a script (an undo button) to set it back if things get messed up. Even if you do other copying in the interval, the data is still available. You do have to be on that same record, but it doesn't have to the only found record.
I use Set Field in my script, but if you want to keep text formatting, you could use Copy/Paste. That requires the global field to be on the layout (but it can be hidden behind the real field). That brings up a point. Only Copy/Paste and Paste (now Insert) Result will preserve formatting. Other steps, including calculations in Field Definitions, will not.
Well, that should be enough to keep you busy for a while. See you next time with the first of many Relationships.
Send comments, questions, toys to Fenton Jones