Solved

How to insert records to database

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
Intraweb download file link ? 1 154
Correct Component for Shopping Cart. 2 112
Tvertscrollbox like a whatsapp layout 5 34
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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