Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to reset a Paradox AutoInc field Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
28-Aug-02
Category
DB-General
Language
Delphi 2.x
Views
104
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Aside from using the Database Desktop to copy the structure of a Paradox table to a 
new one, is there a way or a utility to reset a Paradox AutoInc to one (for any 
empty table) or to the next number after the maximum value for the field?

Answer:

You would have to restructure the table and change the field type to long integer 
then restructure the table and change the field type back to autoinc. An 
alternative is to generate your own autoinc value. Create a single field single 
record table to hold the last number used then use the following code to get the 
next value.

1   
2   function dgGetUniqueNumber(LastNumberTbl: TTable): LongInt;
3   {Gets the next value from a one field one record table which stores the last used 
4   value in its first field. The parameter LastNumberTbl is the table that contains 
5   the last used number.}
6   const
7     ntMaxTries = 100;
8   var
9     I, WaitCount, Tries: Integer;
10    RecordLocked: Boolean;
11    ErrorMsg: string;
12  begin
13    Result := 0;
14    Tries := 0;
15    with LastNumberTbl do
16    begin
17      {Make sure the table contains a record.  If not, add one and set the first 
18  field to zero.}
19      if RecordCount = 0 then
20      begin
21        Insert;
22        Fields[0].AsInteger := 0;
23        Post;
24      end;
25      {Try to put the table that holds the last used number into edit mode. If 
26  calling Edit raises an
27      exception wait a random period and try again}
28      Randomize;
29      while Tries < ntMaxTries do
30      try
31        Inc(Tries);
32        Edit;
33        Break;
34      except
35        on E: EDBEngineError do
36          {The call to Edit failed because the record could not be locked.}
37        begin
38          {See if the lock failed because the record is locked by another user}
39          RecordLocked := False;
40          for I := 0 to Pred(E.ErrorCount) do
41            if E: Errors[I].ErrorCode = 10241 then
42              RecordLocked := True;
43          if RecordLocked then
44          begin
45            {Wait for a random period and try again}
46            WaitCount := Random(20);
47            for I := 1 to WaitCount do
48              Application.ProcessMessages;
49            Continue;
50          end
51          else
52          begin
53            {The record lock failed for some reason other than another user has the 
54  record locked.
55            Display the BDE error stack and exit}
56            ErrorMsg := '';
57            for I := 0 to Pred(E.ErrorCount) do
58              ErrorMsg := ErrorMsg + E.Errors[I].message + ' (' + 
59  IntToStr(E.Errors[I].ErrorCode) + '). ';
60            MessageDlg(ErrorMsg, mtError, [mbOK], 0);
61            Exit;
62          end;
63        end;
64      end;
65      if State = dsEdit then
66      begin
67        Result := Fields[0].AsInteger + 1;
68        Fields[0].AsInteger := Result;
69        Post;
70      end
71      else
72        {If the record could not be locked after the specified number of tries raise 
73  an exception}
74        raise Exception.Create('Cannot get next unique number. (dgGetUniqueNumber)');
75    end;
76  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC