Board index » delphi » Help! Record locked - Not in Edit Mode

Help! Record locked - Not in Edit Mode

I am writing my first "real" application in Delphi 1.0 and have run into a problem
that I absolutely can not figure out.

The application I am writing is a simple club-membership type database.  It has a form
(FrmMembers) for viewing/entering/changing data about members.  This information is
kept in the table TblMembers which has the following fields:

Name
Spouses Name
Address
Zip Code
Home Phone
Work Phone
Spouses work phone

Plus a few boolean fields indicating who gets the newsletter, whether membership has
been payd, etc.

There is also a phonebook (TblPhonebook) whith four fields

Name
Address
Home Phone
Work Phone

FrmMembers has the following components (that have anything to do with the problem)

TblMembers : TTable
TblPhonebook : TTable
DsMembers : TDataSource
DsPhonebook : TDataSource
BtnPhonebook : Tbutton

Data-aware components which have as their datasource DsMembers:
DbEditName : TDBEdit
DbEditSpouse : TDBEdit
DbEditAddress : TDBEdit
DbEditHomePhone : TDBEdit
DbEditWorkPhone : TDBEdit
DbEditSpousesWorkPhone : TDBEdit

Data-aware components which have as their datasource DsPhonebook:
DbEditPhonebookName : TDBEdit
DbEditPhonebookAddress : TDBEdit
DbEditPhonebookHomePhone : TDBEdit
DbEditPhonebookWorkPhone : TDBEdit

These last four components all have their Visible property set to False.

BtnPhonebook is suposed to take the information in the membership database and put it
in the phonebook.  Here is the code that is supposed to do it:

procedure TFrmFelagar.BtnPhoneClick(Sender: TObject);
var
   alreadyinphonebook : boolean;
begin     {procedure}
     alreadyinphonebook:=false;
     if not (TblPhonebook.state=DsEdit) then
             TblPhonebook.edit;
     try
           tblPhonebook.insert;
           DbEditPhonebookName.text:=DbEditName.text;
           DbEditPhonebookAddress.text:=DbEditAddress.text;
           DbEditPhonebookHomePhone.text:=DbEditHomePhone.text;
           DbEditPhonebookWorkPhone.text:=DbEditWorkPhone.text;
           tblPhonebook.post;
           TblPhonebook.refresh;
     except
           on Exception do
           begin
                alreadyinphonebook:=true;
                errorpanel.caption:=dbeditName.text+' is already in
                the phonebook.'
                {TblPhonebook.close;
                TblPhonebook.open;}
           end;  {except}
     end; {try-Except block}
     if not (DbEditSpouse.text='')then
     begin
           if not (TblPhonebook.state=DsEdit) then
                   TblPhonebook.edit;
     Try
           DbEditPhonebookName.text:=DbEditMaki.text;
           DbEditPhonebookWorkPhone.text:=DbEditVsMaka.text;
           DbEditPhonebookAddress.text:=DbEditAddress.text;
           DbEditPhonebookHomePhone.text:=DbEditHomePhone.text;
           DbEditPhonebookWorkPhone.Text:=
              DbEditSpousesWorkPhone.text;
           TblPhonebook.post;
           TblPhonebook.refresh;
     except
           on Exception do
              begin
                       if alreadyinphonebook then
                       errorpanel.caption:=dbeditName.text+' and '+
                       dbeditSpouse.text+' are already in the
                          phonebook'
                   else
                       errorpanel.caption:=dbeditmaki.text+
                       ' is already in the phonebook.';
                       exit;
              end; {except}
     end; {try-Except block}
     end;  {if-then block}

end; {procedure}

If the user accidentally cliks the BtnPhonebook button twice for the same person the
try-except blocks work like they're supposed to and inform the user that this member
is already in the phonebook.  But if I then try to add a new name to the phonebook
(not an error) there is a problem.

The statement

if not (TblPhonebook.state=DsEdit) then
                   TblPhonebook.edit;

precedes each Try-Except block to ensure that the table is in edit mode.  If I include
these statements I get an exception - EDBEngineError with the message "Record already
locked by this session".  But if I leave out these statements I get the message "Table
not in Edit or Insert Mode". This happens in the second Try-Except block.  I also
sometimes get the message Key Violation, when there is no error.  This happens when
the program gets to the line tblPhonebook.insert;.

Can anyone help me with this.  Please reply by email so I'll be sure to see it.

Kristinn Eysteinsson
<k...@treknet.is>

************************
* Keyboard not found   *
* Press F1 to continue *
************************

 

Re:Help! Record locked - Not in Edit Mode


Quote
Kristinn Eysteinsson wrote:

> procedure TFrmFelagar.BtnPhoneClick(Sender: TObject);
> var
>    alreadyinphonebook : boolean;
> begin     {procedure}
>      alreadyinphonebook:=false;
>      if not (TblPhonebook.state=DsEdit) then
>              TblPhonebook.edit;
>      try
>            tblPhonebook.insert;
>            DbEditPhonebookName.text:=DbEditName.text;
>            DbEditPhonebookAddress.text:=DbEditAddress.text;
>            DbEditPhonebookHomePhone.text:=DbEditHomePhone.text;
>            DbEditPhonebookWorkPhone.text:=DbEditWorkPhone.text;
>            tblPhonebook.post;
>            TblPhonebook.refresh;
>      except
>            on Exception do
>            begin
>                 alreadyinphonebook:=true;
>                 errorpanel.caption:=dbeditName.text+' is already in
>                 the phonebook.'
>                 {TblPhonebook.close;
>                 TblPhonebook.open;}
>            end;  {except}
>      end; {try-Except block}
>      if not (DbEditSpouse.text='')then
>      begin
>            if not (TblPhonebook.state=DsEdit) then
>                    TblPhonebook.edit;
>      Try
>            DbEditPhonebookName.text:=DbEditMaki.text;
>            DbEditPhonebookWorkPhone.text:=DbEditVsMaka.text;
>            DbEditPhonebookAddress.text:=DbEditAddress.text;
>            DbEditPhonebookHomePhone.text:=DbEditHomePhone.text;
>            DbEditPhonebookWorkPhone.Text:=
>               DbEditSpousesWorkPhone.text;
>            TblPhonebook.post;
>            TblPhonebook.refresh;
>      except
>            on Exception do
>               begin
>                        if alreadyinphonebook then
>                        errorpanel.caption:=dbeditName.text+' and '+
>                        dbeditSpouse.text+' are already in the
>                           phonebook'
>                    else
>                        errorpanel.caption:=dbeditmaki.text+
>                        ' is already in the phonebook.';
>                        exit;
>               end; {except}
>      end; {try-Except block}
>      end;  {if-then block}

> end; {procedure}

> If the user accidentally cliks the BtnPhonebook button twice for the same person the
> try-except blocks work like they're supposed to and inform the user that this member
> is already in the phonebook.  But if I then try to add a new name to the phonebook
> (not an error) there is a problem.

> The statement

> if not (TblPhonebook.state=DsEdit) then
>                    TblPhonebook.edit;

> precedes each Try-Except block to ensure that the table is in edit mode.  If I include
> these statements I get an exception - EDBEngineError with the message "Record already
> locked by this session".  But if I leave out these statements I get the message "Table
> not in Edit or Insert Mode". This happens in the second Try-Except block.  I also
> sometimes get the message Key Violation, when there is no error.  This happens when
> the program gets to the line tblPhonebook.insert;.

> Can anyone help me with this.  Please reply by email so I'll be sure to see it.

Shouldn't you be cancelling the edit in the Exception blocks. For example:

            on Exception do
               begin
                    if alreadyinphonebook then
                       errorpanel.caption:=dbeditName.text+' and '+
                       dbeditSpouse.text+' are already in the
                           phonebook'
                    else
                       errorpanel.caption:=dbeditmaki.text+
                       ' is already in the phonebook.';
--------------->       tblPhonebook.Cancel;
                       exit;
               end; {except}
      end; {try-Except block}

That way your dataset would be taken out of Edit and the record would be unlocked.

Hope this helps.

Dean Millam.

Other Threads