home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!usenet.ins.cwru.edu!agate!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!cruzio!rhaven
- From: rhaven@cruzio.santa-cruz.ca.us
- Newsgroups: comp.databases
- Subject: Re: PARADOX HELP NEEDED re: auto fill of key field.
- Summary: AutoIncriment fields in Paradox
- Message-ID: <4649@cruzio.santa-cruz.ca.us>
- Date: Sat, 14 Nov 92 19:52:57 PST
- References: <92319.010417U15310@uicvm.uic.edu>
- Sender: rhaven@cruzio.santa-cruz.ca.us
- Reply-To: rhaven@cruzio.santa-cruz.ca.us
- Lines: 50
-
-
-
- There are several ways to impliment autoincriment fields in Paradox, all
- of which involve PAL programming. They fall in to two broad catagories:
- keeping a seperate table with the last id used and incrimenting it; and
- finding the "highest" value in the table itself and incriment that.
-
- In a multi-user situation, things get more complicated because each user
- need to make sure that when they try to use the "next" key, another user
- isn't trying to do the exact same thing. But it's not that hard to do.
-
- As a general note, I use alphanumeric fields for ID rather than numeric fields
- because they are easier to manipulate. Even if a key value is numeric, it
- has no intrinsic numeric value; it is merely a range of possible values.
- Therefor, I usually start my id sequence at 100000 for A6 fields, 1000000
- for A7 fields, etc. Starting with all digits used avoids any leading
- zero confusion.
-
- Technique 1: Keep the last number used in a seperate table. This table can
- either be imbedded into the master form as an unlinked form with the
- attributes of black on black so it is invisible, or positioned off the
- desktop (Paradox 4.0 Standard interface). In either case, you use PAL
- to detect when the user enters a new record (the function RecordStatus("New")
- is good for that). You can either check after certain keys (3.5 style) or
- trap on the ARRIVEROW event (4.0 style). When the new record is detected,
- move to that table (WINDOE SELECT in 4.0) and lock the single record there.
- If you get the lock, that means no onle else is trying to add a record; if not,
- wait a few seconds and try again. Then incriment the value in the single
- field in that table and post the record. YOu now have a key value that you
- know no other session will try to use, Move back to the main data form and
- insert the key value into the key field,
-
- Technique 2: calculate the key from the table itself. You could try to use
- CMAX() or ImageCMax() to get the highest value (on a numeric field), but
- that requires a WL, and if any other session has a record lock, the WL
- will fail. A better approach is to move to the end of the table (which is in
- key sequence), get the value, incriment it to get a new value, add the new record
- (which will become the new "last" record), and use it. TO avoid the multi-
- user danger described above, you need to set a signal (or semaphore) to
- warn others away for as long as it takes to add the new record. You can do
- this by using the LOCK command. You can LOCK a non-existant table. If you
- use a LOCK "Adding" FL, it will fail if anyone else is LOCKing the same
- imaginary table.
-
- Anothe way is a rather dangerous technique using SETBATCH (available only in 4.0)
- Ask me if you are really interested.
-
- I hope this helps
-
- RCH
-