Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Putting a Table in Edit Mode

Posted on 1997-06-06
7
Medium Priority
?
167 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 80 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

722 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