Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Checking some field for uniqueness
Message
 
 
To
12/06/2001 15:49:14
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00518556
Message ID:
00518572
Views:
15
Hi Hilmar,

I was thinking along these lines, but your program has one problem. It openes a table again, therefore it doesn't check buffered changes.

Here is the scenario, I concern about:

Table is opened in table-buffering mode. User can add records or can change the existing records. While he/she is on this textbox, I want to perform a check. I was thinking about using COUNT FOR.

I have to also worry about duplicates, when record is saved. I can either set this tag to be candidate and catch the error on tableupdate (I believe, this is done in our class already) or do it some other way.


>You can check a duplicate key with the function I am including at the end. You should also change the index to Candidate or Primary, since this provides greates safety against violation of rules.
>
>Regards, 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
>
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform