Solved

Insert record using TADOQuery

Posted on 2007-11-21
9
8,151 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: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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

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