?
Solved

Insert record using TADOQuery

Posted on 2007-11-21
9
Medium Priority
?
8,252 Views
Last Modified: 2013-11-23
I am having a problem inserting a record using TADOQuery in Delphi 5.   The query is open and active (the associated datasource state is in dsBrowse) when I try to perform the insert:

  with DM.CustPartQuery do begin
    Insert;
    FieldByName('PKEY').AsString := IntToStr(nNextPKey);
    FieldByName('MCI_NBR').AsString := IntToStr(nNextMCINbr);
    FieldByName('FILM_STATUS').AsString := 'Active';
    FieldByName('SUBCONTRACTED').AsBoolean := (Response=mrYes);
  end;

During debug I can see that after the insert statement, the datasource is still in dsBrowse mode.  Then when it tries to assign the first field, I get an error "Dataset not in insert or edit mode"

What am I doing wrong?
0
Comment
Question by:mthiel3333
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 21

Expert Comment

by:developmentguru
ID: 20327593
 It may help to see the query that is active at the time you are trying to do the insert.  Certain queries can produce result sets that are not easily updatable.  Are all of the fields in the same table?

  You should check the values of the CursorLocation and CursorType properties (both at design time and ruin time).  If the cursor location is clUseClient then the only valid CursorType is stStatic.  I would set the cursor location to ctServer and the CursorType to ctKeySet or ctDynamic and see if it works.

  Personally I use the ADOQuery to populate a TClientDataset and browse off of that.  This will allow you to edit everything in memory and discard changes if you wish.  This works best with a relatively small set of records although I have done it with roughly 20,000).  Once the user decides to accept their changes you can cycle through the dataset looking for records whos fields that have been modified or added.  In general added records will have no auto populated ID (one way to test for an inserted record).  Generally I use insert and update query statements to update the database (using transactions if appropriate), then repopulate my ClientDataset from the database.

Let me know if you need more.
0
 
LVL 28

Expert Comment

by:2266180
ID: 20327599
there can be a number of problems:
if your query is a join for example. or any other such "complex" sql.
or you have some master-detail or similar setup probably not configured correctly
or etc.

so .. what is your query looking like? (the sql) what otehr modifications have you made to the query and connection?
0
 

Author Comment

by:mthiel3333
ID: 20332242
The query that I am using is pretty straightforward:

CustPartQuery.close;
with CustPartQuery.SQL do begin
    Clear;
    Text := 'SELECT * FROM CustomerParts WHERE MCINBR=''1%'''
end;
CustPartQuery.open;

I tried the various CursorLocation/CursorType per your response and all of the variations provided the same error (dataset not in edit or insert mode).
0
Independent Software Vendors: 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!

 
LVL 13

Expert Comment

by:rfwoolf
ID: 20332967
Hmm... try this:

 with DM.CustPartQuery do begin
    Insert;
    FieldByName('PKEY').value:= IntToStr(nNextPKey);
    FieldByName('MCI_NBR').value:= IntToStr(nNextMCINbr);
    FieldByName('FILM_STATUS').value:= 'Active';
    FieldByName('SUBCONTRACTED').value:= (Response=mrYes);
  end;

I was always taught that those AsString and AsBoolean are readonly values.
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20332977
Just another thought, is your Query empty when you call insert?
If you're still stuck and looking for something to try, see if you can make sure your dataset isn't empty.
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20332979
Also note that you don't call Post.
0
 

Author Comment

by:mthiel3333
ID: 20335683
I did check to make sure the query was not empty prior to the insert.  As far as not using AsString, etc., the problem is during the Insert statement.  When I debug, the dataset is still in Browse mode immediately after the Insert statement before it does the replacement statements.

I ended up using an INSERT INTO query instead to add the record and that seems to work fine.

This question can be closed.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 20657794
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

650 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