Solved

Insert record using TADOQuery

Posted on 2007-11-21
9
8,183 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

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!

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
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…

734 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