home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Press 1997 July
/
Sezamfile97_1.iso
/
msdos
/
database
/
pdx_ti2.arj
/
TI662.ASC
< prev
next >
Wrap
Text File
|
1993-01-05
|
8KB
|
265 lines
PRODUCT : Paradox NUMBER : 662
VERSION : 2.0 & up
OS : DOS
DATE : January 5, 1993 PAGE : 1/4
TITLE : Using PAL to Generate Unique Values (Auto-increment)
┌───────────────────────────────────────────────────────────────┐
│ │
│ This Technical Information Sheet is intended only for users │
│ who are familiar with PAL. It contains PAL code to demon- │
│ strate a model solution to a specific problem and is not │
│ intended to represent a complete programming solution. │
│ Programmers who use this code must take responsibility for │
│ debugging and developing their application. Assistance │
│ in debugging and developing applications is considered │
│ consulting and is beyond the scope of technical support. │
│ │
└───────────────────────────────────────────────────────────────┘
In many cases, you will want to generate unique values for each new
record in a table. For example, each new customer might need a new ID
number. Here are three different methods for creating an auto-
increment capability.
The code samples here are generic examples only, and should be modified
to fit your specific application. Note: the following methods work
only in Edit and Coedit modes.
For a single user in interactive mode, the following method works.
SETKEY -23 [] = IMAGECMAX() + 1
Whenever the user presses <ALT-I>, the current field becomes equal to
one more than the current maximum in the current column. To use this
command, insert the above command into the script INIT using the menu
choices: Scripts | Editor | New (for versions earlier than 4.0,
substitute Write for the menu choice New). At the prompt, type in
"Init" and press <ENTER>. Type in the above command exactly as it
appears. The script INIT is played every time you start Paradox, thus
activating <ALT-I>. The keycode -23 corresponds to the key <ALT-I>.
<ALT-I> is just one possible code that you can assign to the PAL
script. See Appendix G of the PAL Reference Guide for a list of
Keycodes Recognized by Paradox (for versions earlier than 4.0, refer to
Appendix B of the PAL User's Guide). In order for the SETKEY macro to
function properly, the cursor must be in the field to be incremented,
which must be a numeric data type.
PRODUCT : Paradox NUMBER : 662
VERSION : 2.0 & up
OS : DOS
DATE : January 5, 1993 PAGE : 2/4
TITLE : Using PAL to Generate Unique Values (Auto-increment)
The following is an example of a PAL program that increments a field:
IF ISEMPTY("CUST") THEN
[CUST ID] = 1
ENDIF
WHILE TRUE
IF ISBLANK([CUST ID]) THEN
[Cust ID] = CMAX("Cust","Cust ID") + 1
ENDIF
WAIT RECORD UNTIL "F2", "INS", ...
KEYPRESS RETVAL
SWITCH
CASE RETVAL = "F2":
QUITLOOP
CASE ...
.
.
.
ENDSWITCH
ENDWHILE
(For more information on the PAL command WAIT RECORD see Technical
Information Sheets 538 and 697)
Whenever the user moves to a record where the CUST ID is blank, the
application enters in a new ID number. The new ID number is derived
using the CMAX function which returns the highest number. The
application then increments this number by one.
The previous two methods work fine in a single-user environment,
however if there is a possibility the application might be used on a
network, you should use the following method. Note: the following
method works only in Coedit mode.
First, create a table with a single Numeric field. For example:
ID ═╦═══ ID ═══╗
║ 10001 ║
This table contains the next number to be used when the application
increments the field in the table you are entering data.
PRODUCT : Paradox NUMBER : 662
VERSION : 2.0 & up
OS : DOS
DATE : January 5, 1993 PAGE : 3/4
TITLE : Using PAL to Generate Unique Values (Auto-increment)
Whenever the user moves to a record where the CUST ID is blank, a new
record is created and filled in with a new ID number. The main data
entry routine calls a procedure that moves to the ID table, and
attempts to lock the record. If it fails the first time, it keeps
attempting to lock the record until it succeeds. During these
attempts, no one else can access the number there. This method
guarantees that each user obtains a unique number.
When the application succeeds in locking the number, it increments it
by one so the next time the application accesses a the table a new
number is available. Next, the application moves back to the CUST
table to continue the editing.
The ID table must be on the workspace in order to access it. If data
entry occurs in table view or with a single table form, you must place
the ID table on the workspace before editing your data table.
A multi-table form complicates the process of accessing the ID table.
(In a multi-table form it is not possible to toggle to table view
before posting the current record.) The simplest solution is to place
the ID table into your master form as an unlinked embedded form, with
the foreground color the same color as the background to hide it from
the users. With the ID table embedded on the master table, the
application can access the ID table while remaining in form view.
The following procedure is an example of how to access the ID table to
obtain the next ID number.
PROC GetCustID()
PRIVATE id
CurrTab = TABLE()
MOVETO "ID"
LOCKRECORD ;Give only one user access
WHILE NOT RETVAL ;Keep trying if first attempt
SLEEP 10 ;fails
LOCKRECORD
ENDWHILE
id = [ID]
[ID] = [ID] + 1 ;Increment ID
UNLOCKRECORD ;Allow other users access
MOVETO CurrTab
RETURN id
ENDPROC
PRODUCT : Paradox NUMBER : 662
VERSION : 2.0 & up
OS : DOS
DATE : January 5, 1993 PAGE : 4/4
TITLE : Using PAL to Generate Unique Values (Auto-increment)
The following PAL program is an example of the main data entry routine.
WHILE TRUE
IF ISBLANK([Cust ID]) THEN
[Cust ID] = GetCustID()
ENDIF
WAIT RECORD
UNTIL "F2", "INS" ...
SWITCH
CASE RETVAL = "F2" :
.
.
.
ENDSWITCH
ENDWHILE
Paradox 4.0 Application Workshop
Instead of writing the whole data entry routine yourself, you could
attach GetCustID() to the ARRIVEROW trigger in the Event Procs for an
Edit session. This works best if the ID table is an embedded unlinked
table. You will need to add additional code to make sure that it only
updates the ID field:
IF TABLE() = "Master" and ISBLANK([Cust ID]) THEN
[Cust ID] = GetCustID()
ENDIF
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that you
received with the Borland product to which this information pertains.