The information in this article applies to:
SUMMARY
While other database applications have a native field type (known as a
Counter type) that is defined to automatically increment a counter, the
implementation of such field behavior in FoxPro is left to the application
developer.
MORE INFORMATION
In FoxPro, the developer has control over the treatment of key values in
records. The developer can decide at the design stage how keys will be
generated and handled in the application.
Creating a Unique KeyIn most cases, you can create a unique key from the data itself by using a Social Security number or a combination of a phone number and name. If this type of data does not exist in the record, a combination of system functions may be used to create a unique key, such as SYS(1), which produces the Julian day number concatenated with SYS(2), which returns the number of seconds elapsed since midnight. Obviously, this method will work only if it can be guaranteed that no more than one record will be created per second, a consideration that may not be valid in a multiuser application. For more information about this, please see the following article in the Microsoft Knowledge Base:Q106708 How to Generate Unique Key Field Values Using a Counter Field as the Unique KeyIn many cases, a simple counter field, incremented when a new record is added, is adequate to ensure a unique key for each record in the database.Adding Counter Field to Screen Previously Generated by Screen WizardThe example given in this article is intended for a single-user system only. As the next available key is stored in memory prior to actually creating the record, the original last key is not changed. In a multiuser environment any number of sessions could receive the same last key, causing records which should be unique to share the same key.You'd need to create a different scheme for multiuser applications. For example, a function could handle requests for obtaining the next key and deny any other requests until the new record is written. The application would then need to increment the key immediately (before the record is written), which means it would not be visible on the screen during data entry. In situations where deleted records are purged to archive files, the 'last record' method would not be appropriate. If the last record is deleted, the next record added is given the same key as the deleted one. To ensure that current records will not have duplicate archived keys, use a separate table to hold the highest key value. In a multiuser environment, you could lock the key table and the data table, update both tables using the next value, add the new record to the data table, and then unlock the key table for use by the next new record. As previously mentioned, adding records should be handled by a single procedure to avoid collisions. The procedure should check a flag field to see if it is set, which would indicate that the procedure is currently in use. If the procedure is not in use, set the flag and execute the procedure. If the procedure is called from another session while the flag is set, the application should ignore the request and try again later. Once the procedure has added the record and updated the key, it should reset the flag to allow more records to be added. Step-by-Step Example for Single-User ApplicationsIn this example, the key is of character type, and no records are in the database that is associated with the screen.The modifications made in this example can be applied to existing screens, and will still work if there are already records in the database. However, if a key field does not yet exist, add one by following this procedure:
To follow this example exactly as written, copy the INVOICES.DBF file from the Tutorial directory to the FoxPro home directory. Once copied, Choose File, Open... and select the new INVOICES.DBF in the FoxPro home directory. From the Command window, issue the command:
Then choose Yes to remove all the records from INVOICES.DBF.
NOTE: The comments contained in the code below are optional, and as such may be omitted for brevity.
Additional query words: VFoxWin FoxWin FoxDos FoxMac
Keywords : FxprgGeneral |
Last Reviewed: August 18, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |