Mega Code Archive

 
Categories / Delphi / Examples
 

How to set a null value through the vcl

Question: I need to set a field in a record back to NULL (not an empty string or 0 for a numeric field). I know I could run a SQL statement and do something like 'update customers set pid = NULL where cust_id=1234' How can I do this within a TQuery using .Edit and .Post methods and TField instances to assign values? Answer: To set a field to NULL, you need to use the TField.Clear method. See the code below for an example. var Query1 : TQuery; begin //.. Query1.Edit; Query1.FieldByName('FIRST_NAME').AsString = 'noname'; Query1.FieldByName('P_ID').Clear; // set this field to NULL Query1.Post; end