<< Click to Display Table of Contents >> Navigation: Programming > Mail Merge (replace fields with data) and data forms > Forms & Edit Fields (data forms) > Read and Write Data |
Edit fields can be read out using procedure MailMerge and event OnMailMergeGetText:
// Read the data which is currently displayed in the editor
procedure TWPEdTest.ReadData;
begin
FReadingData := TRUE; // global boolean to change behaviour
DataEdit.MergeText;
end;
// Write back the data
procedure TWPEdTest.WriteData;
begin
FReadingData := FALSE;
DataEdit.MergeText;
end;
// This reads and writes the data fro the database 'Table1'
procedure TWPEdTest.DataEditMailMergeGetText(Sender: TObject;
const inspname: String; Contents: TWPMMInsertTextContents);
begin
if FReadingData then
Table1.FieldByName(inspname).AsString := Contents.OldText
else Contents.StringValue := Table1.FieldByName(inspname).AsString;
end;
end;
The code above automatically deals with checkbox fields since the TWPMMInsertTextContents detects if there is a FORMCHECKBOX field inside of the merge field and updates its "Params" property automatically. If the field is empty, the logic has no way to detect that a checkbox is required to display the value of a field. You can use the option mmHandleFORMCHECKBOX to fix this, a checkbox will be automatically created. Of course it is also possible to remove the flag to disable the automatic object handling.
if Table1.FieldByName(inspname) is TBooleanField then
Contents.Options := Contents.Options + [mmHandleFORMCHECKBOX];
If you rather update the field in your code, you can use Contents.SetBoolean(b, wpcvFormCheckbox) instead of updating the Contents.StringValue.