I’m currently using the following method to add a field to a paradox table at run time.
procedure TfrmMain.AddField(UpdTable, FieldName, FieldType: string);
begin
with qryUpdate do
begin
Close;
ParamByName('UPDTABLE').AsString := UpdTable;
ParamByName('FLDNAME').AsString := FieldName;
ParamByName('FLDTYPE').AsString := FieldType;
ExecSQL;
Open;
end;
end;
procedure TfrmMain.FormShow(Sender: TObject);
begin
AddField('Test','newfield', 'VARCHAR(30)');
end;
In the component called ‘qryUpdate’ I have the following query:
ALTER TABLE :UPDTABLE
ADD :FLDNAME :FLDTYPE
However when the query is executed the following message is displayed:
Invalid use of keyword.
Token: ?
Line Number: 1.
What am I doing wrong?
Another (related) question:
Will adding fields like this to an existing database harm the existing fields/data at all?
You can only add the field once to the table! Your code, as written, will try to add the field every time you run the program. You should first check to see how many fields your table contains prior to adding the new field.
I used to use the following code to add fields at runtime (this is for BDE/Paradox tables)
Note that my syntax of the ‘alter table’ statement is different from yours – the tablename is enclosed in quotation marks and includes the file’s extension.