Solved

Putting a Table in Edit Mode

Posted on 1997-06-06
7
159 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
What Security Threats Are You Missing?

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 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

19 Experts available now in Live!

Get 1:1 Help Now