This example shows how easy it is to add a field to a table with CNS Query.
Download Example File
To see the calculation that tells the plug-in what to do, open ScriptMaker/Manage Scripts by going to the “Scripts” menu, and choosing ScriptMaker/Manage Scripts. Once in ScriptMaker/Manage Scripts, double click on the ‘Add Field’ script. Note that in the first script step, we are using the “Set Field” script step which consists of two settings. The “Specify target field” button allows you to choose which field you want to set. In our case we want to set the “Result” field. The second specify button is where the CNS Query calculation exists. Click this button to see the calculation used to tell the plug-in what to do. The following image shows the first “Set Field” script step calculation. You can view the other script step calculations by following the same procedure.
The second script step is a pause of 0 seconds. If you have a script that modifies the schema of your database and then immediately needs to do something with that new schema you need this pause in FileMaker 8.5 and above to tell FileMaker to temporarily give up control to the plug-in. In our script, we are adding a field and then we want to list all the fields that are in the table. Since adding a field is changing the schema, we have to insert that 0 second pause to let the plug-in do its work before we can get a list of the fields that includes the field we just added.
Add Field Function
The “Add Field” function is very easy to use. In our script, it looks like this:
CNSQuery_AddField( “” ; Add Field::Field Name ; Add Field::Field Type )
The first parameter is where you specify the name of the Table you want to Add the Field to. Since we are adding fields to the current table, we can just specify “” here, otherwise this would be the name of a Table.
The second parameter is the new Name for the Field. We are using the value we typed into the “Field Name” field.
The third parameter is the Type of Field we want to add. We are using the value we selected in the “Field Type” field. This Field Type parameter is optional and if left off, a Text field would be added.
The “CNSQuery_AddField” function has two other parameters, one is the “AsGlobal” parameter which allows you to define the new field as a Global field (this will only work in FileMaker 8.5 and above), and the last parameter is the “SkipTableNameCheck” parameter. The last parameter simply makes the plug-in work a little faster by not double-checking that the Table exists if you already know that it does.