?
Solved

Insert record using TADOQuery

Posted on 2007-11-21
9
Medium Priority
?
8,227 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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