Xbasic

KEY_UNIQUE Function

Syntax

Result_Flag as L = KEY_UNIQUE(C tagname)

Arguments

tagname

The name of an index tag that may contain the Key_Value. The Index_Tag parameter can include a full drive and path specification. If you omit the drive and path, Alpha Anywhere searches the current directory.

Description

Determines if the keyvalue for the specified tag is unique.

Discussion

KEY_UNIQUE() returns .T. (TRUE) if the key value for the current record already exists in index Index_Tag ; otherwise, it returns .F. (FALSE). This function is useful for checking if a record with a certain key value already exists in the current table. Contrast KEY_UNIQUE() with KEY_EXIST(). KEY_EXIST() will report that the current record's key is a duplicate key when you are in change mode and you save the record. This makes KEY_EXIST() unsuitable for use in a Validation Field Rule. On the other hand, KEY_UNIQUE() does not include the current record's key value in the search for duplicate keys.

Examples

Assume that the following table is indexed on the NAME field, and the index is stored under the tag name "NAMEORD".

CUSTOMER

CUST_ID

NAME

C100

InSync

C101

ACE Software

C102

Friendlies

C103

Sunstar

C104

Progress Inc.

C105

Adventure Holidays

' assuming that the current record's key value does not exist in the NAMEORD index
? key_unique("NAMEORD")
= .T.

Assume that you are entering names and addresses into a contact table ( CONTACTS ), and want to prevent the entry of duplicate records. You define duplicate records as those with the same values in the FIRSTNAME, LASTNAME, and COMPANY fields. During data entry, the prompt order forces you to enter data into the FIRSTNAME field first, then the LASTNAME and COMPANY fields. After you enter a value into the COMPANY field, check to see if the record is a duplicate. To do this:

1. Index the CONTACTS table using the following index expression: trim(FIRSTNAME) + trim(LASTNAME) + COMPANY
2. Name the index " DUP_KEY "
3. Define the following expression as the Validation Field Rule for the COMPANY field: KEY_UNIQUE("DUP_KEY")

To count the unique values in a field in a table.

1. Create an index named myindex on the field that field you want to count. Make sure that the index requires unique values.
2. The value of unique will be the count of unique values in the table's field.
unique = total(if(KEY_UNIQUE("myindex"), 1, 0),  grp->grand)

See Also