Solved

How to insert records to database

Posted on 2001-08-29
5
226 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:jl2001
5 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6439149
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.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6439382
do not open, do execute your query
0
 
LVL 44

Accepted Solution

by:
CrazyOne earned 50 total points
ID: 6439398
Here is what kretzschmar is referring to, change the Active := True to ExecSQL

procedure TForm1.Button1Click(Sender: TObject);
begin
   with dm.ADOQuery1 do
   begin
       Active := false;
       SQL.Clear;
       SQL.Add('insert into category (description) values (''category1'')');
       ExecSQL;
   end;
end;
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 6439486
Actually, DragonSlayers comment will fix the first error message and Kretzschmar's comment will fix the second error.

John.
0
 

Author Comment

by:jl2001
ID: 6442695
Thanks all. ExecSQL is the best way to fix my problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question