Control access to databound form controls with the FormDataObject

I often see code where the developer wants to restrict access to certain fields on a form and does that by flipping the allowEdit property of the form control.

Often the field in question is represented on both the Overview and General tab, so the code will often something like this:


This is all very good unless the fields is added once more to the form. If you add it yourself you must add a line more to your code controlling the access. If the user adds the field again through user setup on the form your logic is however circumvented. 

Instead you should retreive a FormDataObject for your field. This objects works on the field level of the datasouce. Now you can save a line of code by simply writing:

 custTable_ds.object(fieldNum(CustTable, Name)).allowEdit(false);

And as a bonus the logic is now secure, the user can add the field a zillion times without getting access to it.

The FormDataSource object also support the following properties: allowAdd, enabled, mandatory, skip and visible.

If you pass the record buffer from a datasource to a class, you can get the DataSource object from the record buffer and thus get to the FormDataObject. This gives you the option to do some amount of form manipulation from, for example, a class without actually knowing anything about the form. A method to accomplish that could look something like this:

 public void controlStuff(CustTable _custTable)
    formDatasource form_ds;
    if (_custTable.isFormDataSource())
        form_ds = _custTable.datasource();
        if (form_ds)
            form_ds.object(fieldNum(CustTable, Name)).allowEdit(false);

This posting is provided "AS IS" with no warranties, and confers no rights.