Solved

Putting a Table in Edit Mode

Posted on 1997-06-06
7
163 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 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