Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why Trigger failed, more details...
Message
From
21/01/2003 08:06:23
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00743645
Message ID:
00743715
Views:
18
This message has been marked as the solution to the initial question of the thread.
>Hi All
>
>I have an app running and fine. I have set up relational integrity and records get updated, deleted, added according to the rules.
>
>Now when I delete a record becuase there is a restriction I get the message whatever trigger failed.
>
>How do I get to give more details of the trigger failure to the user, eg. "Entries are made in the journal voucher thus this account cannot be deleted" etc.?

You can get some error information with AERROR().

The usual cause for "trigger failed", of course, is RI.

IIRC, AERROR() won't show you the table which didn't allow the delete - but you can get a list of relations with ADBOBJECTS().

I check this BEFORE issuing the TableUpdate(), and inform the user with the functions I am including.

HTH,

Hilmar.
FUNCTION CanDelete()
	local lcHasChildRecords
	lcHasChildRecords = HasChildRecords()
	if not empty(lcHasChildRecords)
		* Can't delete. Records in table ...
		ErrorMsg("No se puede borrar. Hay datos en " + lcHasChildRecords + ".")
		return .F.
	endif
ENDFUNC



FUNCTION HasChildRecords()

	if CursorGetProp("SourceType") # 3	&& not a table
		return ""
	endif

	*!*	lParameters tlShowErrors
	*!*	if parameters() < 1
	*!*		tlShowErrors = .T.
	*!*	endif

	local lnSelect
	lnSelect = select()

	if not used("RelationsForDelete")
		open database database
		adbobjects(arelations, "relation")
		create cursor RelationsForDelete (child C(30), parent C(30), childtag C(10), parenttag C(10), ri C(10))
		append from array arelations
		select *;
			from RelationsForDelete;
			into cursor RelationsForDelete;
			where substr(ri, 2, 1) = "R"
		index on Parent tag Parent
	endif
	local lcError, laFields(1), lcTableName, lcChildTable, lcChildAlias, lcParentTag, lcChildTag,;
		lcSeekExpr, lcParentKey
	lcError = ""
	select (lnSelect)
	afields(laFields)
	lcTableName = upper(laFields(12))
	if empty(lcTableName)	&& probably a free table
		return ""
	endif
	use (dbf()) again alias "CanDelete_ParentTable" in 0
	select RelationsForDelete
	scan for parent = lcTableName
		lcParentTag = alltrim(ParentTag)
		lcChildTag = alltrim(ChildTag)
		lcChildTable = alltrim(Child)
		lcChildAlias = "CanDelete_" + lcChildTable
		select CanDelete_ParentTable
		set order to (lcParentTag)
		lcParentKey = key()
		select (lnSelect)
		lcSeekExpr = eval(lcParentKey)
		if used(lcChildAlias)
			select (lcChildAlias)
		else
			select 0
			use (lcChildTable) again alias (lcChildAlias)
		endif
		set order to (lcChildTag)
		if seek(lcSeekExpr)
			lcError = lcChildTable
			exit
		endif
	endscan

	select (lnSelect)
	use in CanDelete_ParentTable
	return lcError
ENDFUNC
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
Next
Reply
Map
View

Click here to load this message in the networking platform