Link to home
Start Free TrialLog in
Avatar of jl2001
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
Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

it's interesting to note that you need to enclose strings in a single quote ' instead of a double quote " when using TADOQuery components with Access databases (dunno, never tried ADO with other databases, I usually use native access for other databases)

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.
Avatar of kretzschmar
do not open, do execute your query
ASKER CERTIFIED SOLUTION
Avatar of CrazyOne
CrazyOne
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jaymol
Jaymol

Actually, DragonSlayers comment will fix the first error message and Kretzschmar's comment will fix the second error.

John.
Avatar of jl2001

ASKER

Thanks all. ExecSQL is the best way to fix my problem.