Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Only using distinct number in table
Message
De
26/03/2002 08:56:46
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00637190
Message ID:
00637191
Vues:
26
>Good day everyone,
>I have a form that a user inputs data into and on a particular text box that I have the user is supposed to input a number and the number is supposed to be original so I don't want any duplicates of that same number. Is there any way to place code on the lost focus event of the text box that will check in the table (that the information will be inserted into) to see if that number already exists and if it does then display a message box stating that the number is already used and to find another number?
>
>Thank you!
>Max

Personally, I prefer to do validation only when the user saves (i.e., tries to save) the record. This is much simpler in many aspects.

Wherever you choose to do the validation, the following function should help you to check for duplicates.

HTH, Hilmar.
**********************************************************************
FUNCTION DuplicateKey(tcTag, tcTable, txPkExpression)
	* Check for duplicate, according to a certain index tag.

	* Parameters:
	* tcTag: name of index tag.
	* tcTable (optional): name of table. Required if working with views.
	* txPkExpression (optional): primary key expression. Required when working with views.
	*   This allows to omit current record from checking of duplicate.
	*   Can't be used to check uniqueness of primary key (but I use non-business-value keys,
	*   automatically generated, anyway).

	* Returns: true if another record exists with the same value(s)
	* (according to the specified index tag).

	* Additional comments:

	* By intercepting for this duplicate value before Visual FoxPro does (through Primary or
	* Candidate Indices), the user can messages that are clearer than "Trigger failed"
	* (at least with Visual Extend, "Trigger failed" doesn't give more details).

	* The function can also be used to check duplicates in FoxPro 2.x;
	* just change "local" to "private".

	* Of course, depending on the index, this function may check whether the combination of
	* two or more fields is unique - however, only the index tag is passed as a parameter.

	local lnSelect, lcDbf, lnRecno, txEvalExpr, llDuplicate, lcExpression, lcOrder, llWorkOnView
	llWorkOnView = not empty(tcTable)
	lnSelect = select()
	lcDbf = iif(empty(tcTable), dbf(), tcTable)
	lnRecno = recno()
	lcOrder = order()
	if not llWorkOnView
		set order to (tcTag)
		lcExpression = key()
	else
		select 0
		use (lcDbf) order (tcTag) again
		lcExpression = key()
		use
		select (lnSelect)
	endif
	txEvalExpr = eval(lcExpression)
	select 0
	use (lcDbf) order (tcTag) again
	seek txEvalExpr
	if found() and (llWorkOnView and eval(tag(GetPkNum())) = txPkExpression;
			or not llWorkOnView and recno() = lnRecno)
		skip
	endif
	llDuplicate = (eval(lcExpression) = txEvalExpr)
	use
	select (lnSelect)
	set order to (lcOrder)
	return llDuplicate
ENDFUNC && DuplicateKey
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform