Link to home
Start Free TrialLog in
Avatar of tdiamond
tdiamond

asked on

How to check if record is locked (again)

I submitted this question a couple of days ago:

How can you check to see whether a record that you are about to write to is locked by another user? If I have to check for an exception, then which one should I check for (EConvertError, E....)? What I want to do is rollback a Transaction if my write to a table fails.

and got this response from Waldek:

You must check for exception EDBEngineError and then
test ErrorCode like this
  case Errors[0].ErrorCode of
    DBIERR_LOCKED:{ Action , if lock failed}
    DBIERR_UNLOCKFAILED: { Action , if unlock failed}
  end;

The code doesn't compile and I was wondering if Waldek, or anybody who gets this, could help me out again?
       
The following code works but will report the same error when ANY kind of EDBEngineError Exception occurs. Can you tell me (or even better, show me) how to modify it to test specifically for a locked record? It would help me out a lot.

procedure TFRMMain.Button2Click(Sender: TObject);
begin
  try
    ItemsTable.edit;
    ItemsTable['Test']:=1;
    except on EDBEngineError do
      showmessage('Record is locked !');
  end;
end;

I want to know how I can extract the specific error condition that has occured.
ASKER CERTIFIED SOLUTION
Avatar of Waldek
Waldek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tdiamond
tdiamond

ASKER

I found it but I'm having a little trouble implementing it correctly.  Could you possibly give me an example of how it might be called and with what parameters?  For instance, here is what my code lookes like:

procedure TFRMMain.ButtonClick(Sender: TObject);
var
  b: bool;
begin
  DbiIsRecordLocked(ItemsTable.handle, b);
  if (b=true) then
    showmessage('Record IS locked')
  else
    showmessage('Record is NOT locked');
end;

It compiles, but when it runs, it always reports the record NOT being locked even when it is!  Can you tell if I'm doing something wrong in my code.  Am I passing the correct parameters?
Check this
var b: bool;
begin
  Check(DbiIsRecordLocked(ItemsTable.handle, b));
     if b then
       showmessage('Record IS locked')
         else
       showmessage('Record is NOT locked');
     end;