Solved

How to insert records to database

Posted on 2001-08-29
5
216 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now