Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Field and Record validation with DBC Rules
Message
From
22/11/2001 07:46:48
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
To
22/11/2001 07:24:03
Sam Trenchard
System Support Services
London, United Kingdom
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00584837
Message ID:
00584878
Views:
30
My solution: All rules are stored in a table, validation.dbf (Table C(30), Order I, Rule M, Spanish M, Comments M, Warning L)

Order specifies the optional order of evaluation. Spanish is the error message in Spansih. Comments are comments for the programmer only. Warning = .T. means the user will see a warning only (record is still saved).

Each Form has a method Form.Valid(), called from Form.OnSave() (both part of the framework, Visual Extend). From Form.Valid(), I call a function I created, RecordValid() (stored in the DB stored procedures):
**********************************************************************
FUNCTION RecordValid(tcTableName, llEvaluateAllRules)
	* Record validation.
	* Meant to be invoked from form.valid((tablename)).
	* Can also be invoked from other places, for instance, triggers.

	tcTableName = lower(tcTableName)
	llEvaluateAllRules = .T.  && Currently, I override second parameter.

	* save and change settings
	local lnOldAlias, lcOldExact
	lnOldAlias = select()
	lcOldExact = set("exact")
	set exact off

	* open validation table
	if used("validation")
		select validation
	else
		select 0
		use validation again
	endif
	set order to "mainorder"

	* evaluate all errors defined in rules table
	local lcErrorText, llError
	lcErrorText = ""
	tcTableName = padr(tcTableName, len(Table))
	seek tcTableName
	scan while table = tcTableName
		select (lnOldAlias)
		if not evaluate(validation.rule)
			lcErrorText = lcErrorText + iif(validation.warning, "Warning: ", "Error: ") + validation.spanish
			if not validation.warning
				llError = .T.
			endif
			if llEvaluateAllRules
				lcErrorText = lcErrorText + chr(13)
			else
				exit
			endif
		endif
		select validation
	endscan

	* show error message
	if not empty(lcErrorText)
		* wait window nowait "Can't save changes:" + chr(13) + chr(13) + lcErrorText
		MessageBox(iif(llError, "Can't save:", "Warning on save:") + chr(13) + chr(13);
			+ lcErrorText + iif(llError, "", chr(13) + "Changes saved."),;
			16, iif(llError, "Error saving", "Warning on save"))
	endif

	* restore environment and return success status
	use in validation
	select (lnOldAlias)
	set exact &lcOldExact
	return not llError
ENDFUNC
With this validation I intercept most error conditions from Visual FoxPro, including (with specially designed functions) "trigger failed" when the user tries to save a record in a child table before saving the record in the parent table, and duplicate records.

HTH, Hilmar.
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)
Previous
Reply
Map
View

Click here to load this message in the networking platform