Solved

Putting a Table in Edit Mode

Posted on 1997-06-06
7
166 Views
Last Modified: 2010-05-18
When I run the code below, I get an error stating 'Datset not in Insert or Edit Mode'.  As you can see, I place the table in edit mode several times. What am I doing wrong? (By the way, I'm trying to change a field's value to False if it equals True)

procedure TfrmGList.BitBtn1Click(Sender: TObject);
begin
if table1.state = dsInactive then
   table1.Active := True;
table1.edit;
table1.disablecontrols;
table1.first;
while not table1.EOF do
  begin
    if table1.FieldByName('Select').asBoolean <> False then
       table1.edit;
       table1.FieldByName('Select').asBoolean := False;
       table1.next;
  end;
  table1.first;
  table1.enablecontrols;
end;
0
Comment
Question by:d4jaj1
[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
  • 4
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
ygolan earned 20 total points
ID: 1336856
There should a "begin" following the "if" statement in the while loop. Because it is missing, the line where you modify the Select field will be activated even when the table is not in edit mode.  Here is the fixed code, note the comments :

procedure TfrmGList.BitBtn1Click(Sender: TObject);
begin
  if table1.state = dsInactive then
    table1.Active := True;
  table1.edit; // Not needed, you can remove this!
  table1.disablecontrols;
  table1.first;
  while not table1.EOF do
  begin
    if table1.FieldByName('Select').asBoolean <> False then
    begin // This was missing !!
      table1.edit;
      table1.FieldByName('Select').asBoolean := False;
      // it's safer to add table1.post here.
      table1.next;
    end; // And you need to add this as well
  end;
  table1.first;
  table1.enablecontrols;
end;
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1336857
Thanks for your quick response.  I think there is a problem though, when I run the code, nothing happens!  My system locks and the values aren't changed.  When I added the Showmessage when it finds False and after it makes the changes, it does so correctly - but goes on a continuous loop, like it never reached the EOF.   Some of the values are blank instead of false/true, could this be the problem. If I try to use ..."AsString" I get an error because the field is a logical Paradox field.  Where did I go wrong?

if table1.state = dsInactive then
   table1.Active := True;
   table1.disablecontrols;
   table1.first;
   while not table1.EOF do
     begin
       if table1.FieldByName('Select').asboolean <> False then
       Showmessage('Initial Found');  //It worked but never ended
       begin // This was missing !!
         table1.edit;
         table1.FieldByName('Select').asboolean := False;
         Showmessage('Changed');  //It worked but never ended
         //table1.post;
         table1.next;
       end; // And you need to add this as well
     end;
table1.refresh;
table1.first;
table1.enablecontrols;

0
 
LVL 1

Expert Comment

by:ygolan
ID: 1336858
Move the table1.next out of the "if". The way it is now, it will be an infinite loop indeed.


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 3

Author Comment

by:d4jaj1
ID: 1336859
Still doesn't work.  I commented the Disablecontrols and added the Showmessage methods so I couldl see it work.  here are still two problems, 1) The table changes values in every record, even if already equals False (although the Value Found only appears if True is found, not Blank) and 2) if never ends!  I moved the table1.next out of the If.. statement, but it still goes to the top of the grid and starts over.  Is the EOF in the wrong place?
************************************************************************
begin
table1.first;
showmessage('went first');
while not table1.EOF do
  begin
  if table1.FieldByName('Select').asBoolean <> False then
  showmessage('found value');
    begin // This was missing !!
    table1.edit;
    table1.FieldByName('Select').asBoolean := False;
    showmessage('changed');
    table1.post;
    end; // And you need to add this as well
  table1.next;
end;
end;
0
 
LVL 1

Expert Comment

by:ygolan
ID: 1336860
Remove the ShowMessage('found value') line and everything will work.

If you can't figure out why, up the points please.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1336861
It worked!  I guess because the ShowMessage was the only statement being evaluated by the IF statement, thus the Begin statement execute regardless if teh IF statemenet returned a true or false value.

Great Job!!!
0
 
LVL 1

Expert Comment

by:ygolan
ID: 1336862
> It worked! I guess because the ShowMessage was the only
> statement being evaluated by the IF statement, thus the Begin
> statement execute regardless if teh IF statemenet returned a > true or false value.

Exactly.

I am glad to hear that it works.
0

Featured Post

Technology Partners: 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…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month6 days, 11 hours left to enroll

623 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