Database Reference
In-Depth Information
Every once in a while, you need an extra jolt of flexibility. Suppose your script sets one of
any number of possible fields, and which one varies from one running of the script to the
next. For instance, imagine you have a script that puts a measurement into one of seven dif-
ferent fields in a database of statistical recordings, one for each day of the week (with a new
record for each week of the year). When your script goes to put the data in the right place,
you might feel like you're going to need a giant If/Else If block with six cases, and six nearly
identical Set Field steps.
Use Set Field by Name instead of writing a hairy If/Else test. Instead of selecting the tar-
get field from a list, you use a calculation to produce the target field name. For instance, ima-
gine your six fields are called “Measurement 1” through “Measurement 7.” You could use a
formula like this for your target field:
"Measurement " & DayofWeek ( Get ( CurrentDate ) )
This formula produces the name of the field for each given day, so a single Set Field by
Name step can replace all those If conditions.
Set Field by Name has a downside, though: If you rename a field, the step stops working
because the calculation uses a character string (static text) and not a reference to the field. To
combat this problem, use a special function called GetFieldName() that does let you refer to
a field, and so will update if you need to rename the field. So if you're inclined to rename ob-
jects without checking first to see if they're in use somewhere, use a formula like this to be
safe:
Choose (
DayofWeek ( Get ( CurrentDate ) ) - 1;
GetFieldName ( Measurements::Measurement 1 )
GetFieldName ( Measurements::Measurement 2 )
GetFieldName ( Measurements::Measurement 3 )
GetFieldName ( Measurements::Measurement 4 )
GetFieldName ( Measurements::Measurement 5 )
GetFieldName ( Measurements::Measurement 6 )
GetFieldName ( Measurements::Measurement 7 )
)
NOTE
See The Choose() Function to learn how the Choose() function works.
This formula checks the current date and then returns the correct field name for that day. And
since it uses GetFieldName() , it returns the right name even if you rename the field it refers
Search WWH ::




Custom Search