I have to create a program in Delphi using Access 2003 .mdb file as data repository.
The Access database has a table with a boolean (Yes/No in Access) field called "original".
I have mapped this field to a TDBCheckBox which shows checked for true and unchecked for false, and shows a half greyed check is the field has not been set.
What I want开发者_高级运维 is oncreation of the field for the field to be set to false (checkbox unchecked) and save the field value as false IF the user has not explicitly set the field.
I have tried if (DVDQuery.FieldByName('Original').AsBoolean <> True) and (DVDQuery.FieldByName('Original').AsBoolean <> False ) then DVDQuery.FieldByName('Original').AsBoolean := False;
But this does not work for new records. I use a query to access the dataset as there a large number of dynamically created where statements to filter the dataset.
ANy help guidance is greatly appreciated.
Rob
Can you change the structure in the database? The proper place for default values is in the column definition. If you can update the structure, change the field to have a default value of "No". You will then never need to do any coding around this issue, and your data will be guaranteed correct even if entered directly through Access.
If you need to check the value in code, use if DVDQuery.FieldByName('Original').IsNull
to determine whether the field is empty or not.
Finally, if you must change the value in code rather than as a database default, do it in the appropriate TDataset event (AfterInsert, AfterScroll, etc).
Check if the field is set a value or not in the BeforePost
event of the DataSet:
procedure TForm1.DVDQueryBeforePost(DataSet: TDataSet);
begin
if DVDQuery.FieldByName('Original').IsNull then
DVDQuery.FieldByName('Original').AsBoolean := False;
if still relevant, check the status field of the field. It should be cbChecked, cbUnchecked or cbGray, which is what you are looking for.
精彩评论