jl2001
asked on
How to insert records to database
Hi, all Experts
I am using ADO to connect to MS Access database. I created the following codes to insert a record:
procedure TForm1.Button1Click(Sender : TObject);
begin
with dm.ADOQuery1 do
begin
Active := false;
SQL.Clear;
SQL.Add('insert into category (description) values (''category1'')');
Active := true;
end;
end;
But when I run the program and click on button1, it shows some errors:
1. The operation requested by the application is not supported by the provider;
2. CommandText does not return a result set.
Please help me to fix it.
Thanks
I am using ADO to connect to MS Access database. I created the following codes to insert a record:
procedure TForm1.Button1Click(Sender
begin
with dm.ADOQuery1 do
begin
Active := false;
SQL.Clear;
SQL.Add('insert into category (description) values (''category1'')');
Active := true;
end;
end;
But when I run the program and click on button1, it shows some errors:
1. The operation requested by the application is not supported by the provider;
2. CommandText does not return a result set.
Please help me to fix it.
Thanks
do not open, do execute your query
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, DragonSlayers comment will fix the first error message and Kretzschmar's comment will fix the second error.
John.
John.
ASKER
Thanks all. ExecSQL is the best way to fix my problem.
so let's say you want to insert something that the user has typed in an edit box, try this:
with ADOQuery1 do
begin
Close;
SQL.Text := Format('INSERT INTO category (description) VALUES (%s)', [QuotedStr(Edit1.Text)]);
Open;
end;
using SQL.Text eliminates the need to call Clear and Add.
Hope that helped!
DragonSlayer.