Quote
On Fri, 9 Mar 2001 09:47:28 +0100, "Arjan" <n...@spam.com> wrote:
>Dan,
>Thanks for you answer. I can not use the search max method, because it is a
>multi-user program. Therefore I would ask you to give me some more
>information on using the separate table method you mentioned.
>Thanks in advance
>Best regards,
> Arjan
Here is the general method: When one user needs the next counter
value: lock the table, read the current value, increment it, then
return the new value to the calling procedure, then unlock the table.
Each user should call for a new number in a try-except block to allow
for simultaneous calls.
Note: the following is for Paradox tables, which are locked
automatically by the BDE for a Live Query. For other DBs, be sure to
understand how the Counter table will be locked. You may have to set
an explicit lock in your code, and then release it later.
1. create a table called 'Counter.db' [I'm assuming Paradox for
illustration only]. Give it an integer field for each different
counter you need, for example 'OrderNo' and 'CustNo'. Open the table
and set each field to your starting values; e.g. 1000. Use Database
Desktop, or Paradox, or ?? to make this table.
2. drop a query on a datamodule, say dm1, named qCounter. Set the SQL
property to SELECT * FROM "Counter.db"
RequestLive = true
3. put the following function in dm1:
function Tdm1.NextNum(typ: string): integer;
var
nn: integer;
begin
with qNumber do
begin
close;
try
open; // this locks the record to other users
edit;
nn := FieldByName(typ).AsInteger; // read current max value
inc(nn); // add one to it
FieldByName(typ).AsInteger := nn; // write the new value
post; // store the new value in the database
result := nn; // send back new value
except
result := -1; // send back error code
end; // end of try-except block
close; // release the lock on the table
end;
end;
4. Typical calling procedure:
procedure TfrmOrders.cmdNewClick(Sender: TObject);
var newnum, N: integer; { if newnum is needed elsewhere, make it a
form variable}
begin
N := 1;
repeat
begin
newnum := dm1.NextNum('OrderNo'); // gets next order number
inc(N);
end;
until (newnum <> -1) or (N = 5); // tries 5 times to get a number
if newnum = -1 then showmessage('Could not get new Order Number');
qcvp.AppendRecord([newnum, date]); { add a new record to the
dataset in use by the order entry form. This example fills in the 1st
two field values, including the just generated order number}
{<do other things if necessary>}
end;
--
Dan Brennand
CMDC systems, inc.
Configuration Management and Document Control:
visit us at www.cmdcsystems.com
[SPAM block: change at to @ in my e-mail address]