Solved

Insert record using TADOQuery

Posted on 2007-11-21
9
8,117 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
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:ciuly
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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

21 Experts available now in Live!

Get 1:1 Help Now