edhasted
asked on
Delphi - How to test if a Table is locked?
I'm trying to get to grips with how to overhaul some Delphi code to become multi-user.
The program has a sequence of forms that need to be posted to a Paradox Table controlled by the BDE.
In essence I want to check if the table is locked and if not lock the table or record and then post and then remove my locks.
The Borland BDE API examples seem to be working with grids and rely on working out where the cursor is.
They don't appear to be appropriate and do I need anything that complex.
I have three issues that I will separate out into distinct EE questions.
How do I see if the table is locked?
If I use the crude code below I can get the BDE intercepting it first telling me that the table is locked by another user.
Also it always gives me the same answer. I am forcing the table into Edit mode to notionally lock it.
function IsLocked(TheDataSet: TDataSet): Boolean;
begin
Result := False;
try
TheDataSet.Edit;
TheDataSet.Cancel;
Except
Result := True;
end;
if Result = True then ShowMessage('Database Locked');
if Result = False then ShowMessage('Database UnLocked');
end;
Questions:
1. How do I improve the code so it works and
2. Trap the error calls so they are handled more elegantly.
With thanks,
Ed
The program has a sequence of forms that need to be posted to a Paradox Table controlled by the BDE.
In essence I want to check if the table is locked and if not lock the table or record and then post and then remove my locks.
The Borland BDE API examples seem to be working with grids and rely on working out where the cursor is.
They don't appear to be appropriate and do I need anything that complex.
I have three issues that I will separate out into distinct EE questions.
How do I see if the table is locked?
If I use the crude code below I can get the BDE intercepting it first telling me that the table is locked by another user.
Also it always gives me the same answer. I am forcing the table into Edit mode to notionally lock it.
function IsLocked(TheDataSet: TDataSet): Boolean;
begin
Result := False;
try
TheDataSet.Edit;
TheDataSet.Cancel;
Except
Result := True;
end;
if Result = True then ShowMessage('Database Locked');
if Result = False then ShowMessage('Database UnLocked');
end;
Questions:
1. How do I improve the code so it works and
2. Trap the error calls so they are handled more elegantly.
With thanks,
Ed
If you need to determine if the table itself is locked, you can check the number of write locks against the dataset, eg:
function GetTableLockCount(DataSet:
begin
// Set default result
result:=0;
// Check active state
if DataSet.Active then
begin
// Get number of write locks on the table
Bde.DbiIsTableLocked(DataS
end;
end;
Russell
ASKER
Russell - if I use the function GetTableLockCount(DataSet: TBDEDataSet): I'm always getting a a result of 0 even after I have put the table inot "edit" state - from what I have read on EE previously that should lock the table?
I was trying to lock it anotherwas but looking at the BDE calls like the one to release the TableLocks they almost always mention a cursor which throws me. I'm not using a grid so the concept of the cursor is superfluos. Can you elaborate?
DbiRelTableLock (hCursor: hDBICur; bAll: Bool; eLockType: DBILockType): DBIResult stdcall;
many thanks,
Ed
I was trying to lock it anotherwas but looking at the BDE calls like the one to release the TableLocks they almost always mention a cursor which throws me. I'm not using a grid so the concept of the cursor is superfluos. Can you elaborate?
DbiRelTableLock (hCursor: hDBICur; bAll: Bool; eLockType: DBILockType): DBIResult stdcall;
many thanks,
Ed
ASKER
Another variant maybe of the same question in the function IsRecordLocked(DataSet: TBDEDataSet): LongBool;
How does it know which record to lock?
Ed
How does it know which record to lock?
Ed
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Russell - thank you for such an expnasive answer which explained in longhand what was written in shorthand in the BDE help.
As always examples explain all.
With many thanks,
Ed
As always examples explain all.
With many thanks,
Ed
My pleasure, glad to have helped.
Regards,
Russell
Regards,
Russell
When working with BDE, you will find that this is a little cleaner as you don't have to enter/exit the edit state (you only test the state). The plus side is that you get to handle the errors without raising/trapping an exception to do it.
Requires DB, DBTable, BDE in the uses clause.
function IsRecordLocked(DataSet: TBDEDataSet): LongBool;
begin
// Check for active dataset
if DataSet.Active then
begin
// Check if record is locked, if an error occurs return false
if (Bde.DbiIsRecordLocked(Dat
end
else
// Not active
result:=False;
end;
Regards,
Russell