Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How Can I Change Uniqueness of index STOCKID is violated
Message
From
21/09/2002 05:12:12
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00702776
Message ID:
00703035
Views:
26
>Thanks for the reply Gregory,
>how can i do this would you like send a sample code here ?
>
>TIA

Soykan,

I just went through some code of my early vfp days (vfp 3.0). The code has been modified over the years with quick fixes.
Hope it helps as a sample

Success.

I normally have : ON ERROR Error_(Error(), Program(), LineNo() )
But it can be disabled as in
local sError
sError = on('Error')
private Failed
Failed = FALSE
on error Failed = TRUE

&& do some command
on error &sError
if( Failed )
  ...
endif
It has never become clear to me when executing code in a form, the Form.Error() fires and when the on error fires.
That is one of the reasons the form.Error() follows the on('Error') (besides having only one error routine)

(1) Include file
** Variable.h
#define	ILANG	'N'
&& this file is included anywhere in the project
&& took only the significant bits
** Base.h
#include	"Variable.h"
#include	"FoxPro.h"

#define	TRUE	.T.
#define	FALSE	.F.

* Errors
#define	ERROR_TABLE_FIELD_DOES_NOT_ACCEPT_NULL_VALUES	1581
#define	ERROR_TABLE_FIELD_RULE_VIOLATED	1582
#define	ERROR_TABLE_TRIGGER_FAILED		1539
#define	ERROR_TABLE_RULE_VIOLATED		1583
#define	ERROR_TABLE_KEY_NOT_UNIQUE		1884

#define	MESSAGE_FORM_ERROR	'Error in Form'

* Messages
*-----------TABLE-------------------
#if (ILANG = 'E' )
	#define Caption_YES	'YES'
	#define Caption_NO	'NO'
	#define Caption_NULL	'???'

	#define	_none_	'(none)'
	#define MESSAGE_TABLE_FIELD_DOES_NOT_ACCEPT_NULL_VALUES ;
			'Field does not accept Null Values'
	#define	MESSAGE_TABLE_FIELD_RULE_VIOLATED ;
			'Error in field contents'
	#define	MESSAGE_CHANGE_OR_UNDO 'Correct or UNDO the changes'
	#define	MESSAGE_TABLE_RULE_VIOLATED		;
			'Record rule violated !'
	#define	MESSAGE_TABLE_TRIGGER_FAILED_INSERT ;
			'Cannot insert '
	#define	MESSAGE_TABLE_TRIGGER_FAILED_UPDATE ;
			'Cannot modify '
	#define	MESSAGE_TABLE_TRIGGER_FAILED_Delete ;
			'Cannot delete '
	#define	MESSAGE_TABLE_KEY_NOT_UNIQUE	;
			'not unique '
#else
	#define	_none_	'(geen)'
	#define Caption_YES	'JA'
	#define Caption_NO	'NEE'
	#define Caption_NULL	'???'

	#define MESSAGE_TABLE_FIELD_DOES_NOT_ACCEPT_NULL_VALUES ;
			'Aanvaardt geen NULL waarden'
	#define	MESSAGE_TABLE_FIELD_RULE_VIOLATED ;
			'Inhoud klopt niet '
	#define	MESSAGE_CHANGE_OR_UNDO 'Verbeter of maak de wijziging ongedaan'
	#define	MESSAGE_TABLE_RULE_VIOLATED		;
			'Record regel overtreden !'
	#define	MESSAGE_TABLE_TRIGGER_FAILED_INSERT ;
			'Bijvoegen gaat niet '
	#define	MESSAGE_TABLE_TRIGGER_FAILED_UPDATE ;
			'Wijzigen gaat niet '
	#define	MESSAGE_TABLE_TRIGGER_FAILED_Delete ;
			'Verwijderen gaat niet '
	#define	MESSAGE_TABLE_KEY_NOT_UNIQUE	;
			'niet uniek (er is twee maal hetzelfde)'
#endif
(2) Form error routine
LPARAMETERS nError, _program, _lineno

this.LockScreen = FALSE
assert nError==Error()

private  __Object_with_Error__	&& for error handler
private __LineNoPassed__	&& for error handler

__Object_with_Error__ = this
__LineNoPassed__ =  _lineno


local s
s = on('error')
&s

release __Object_with_Error__, __LineNoPassed__
(3) Error routine
procedure Error_(nError, _program, _lineno  )

if( inlist(nError, 5, 2012) )
	return
endif

local _message, _linecode
_message = Message()
_lineCode = Message(1)

local	_Error[1], _msg, _Answer, _Table, _Field, _Caption, _x, _afields[1]
=aerror(_Error)
msg_Field = ''

* define _form && _obj if possible
local _form, _obj

store	Null to _form, _obj

if( type('__Object_with_Error__') == T_OBJECT )
	_program = program( program(-1) - 2)	&&&&&&&&
	_obj = __Object_with_Error__
	
	if( type('__LineNoPassed__') ==  T_UNDEFINED )	&& from Sendmail eg
		_lineno = -1
	else
	
		_lineno = __LineNoPassed__
	endif
	if( _obj.BaseClass == 'Form' )
		_form = _obj
	endif
endif


if( !isnull(_form) )
	_form.ErrorOccurred = .T.
	if( _form.IgnoreErrors ) && for refreshForm()
		return
	endif
	_Form.LockScreen = FALSE
endif

if( (type('BatchMode') <> T_UNDEFINED) and BatchMode )
	if !isnull(_form)
		_msg = MESSAGE_FORM_ERROR + ' ' + _form.Name + CR ;
			+ str(_Error[1]) + ':'  + _Error[2] + CR ;
			+ _program ;
			+ ' line ' + str(_Lineno) + CR ;
			+ _linecode
	else
		if( !empty(_program) )
			_msg = 'Error ' + str(nError) + CR + ;
			+ _message + CR + ;
			+ 'Program : ' + _program + CR + ;
			+ 'Line ' + str(_Lineno) + CR + ;
			+ _linecode
		else
			_msg = _message
		endif
	endif
	=Trace(_msg)
endif

if( inlist(nError, 1582) )
	*suspend
endif

do case
case _Error[1] == ERROR_TABLE_FIELD_DOES_NOT_ACCEPT_NULL_VALUES
	_Table = GetTableName(_error[4])
	_Field = proper(field(_Error[5], _Error[4]))
	_Caption = ''
	if( !empty(cursorgetprop('DataBase', _error[4])) )
		_Caption = dbgetprop(_Table + '.' + _Field, 'Field', 'Caption')
	endif
	if( empty(_Caption) )
		_Caption = _Field
	endif
	
	_msg = ''
	msg_Field = _Table + ': ' + _Caption + ': ' + _msg
	
	_msg	= _Table + ': ' + _Caption + ': ';
				+ _msg + CR + MESSAGE_TABLE_FIELD_DOES_NOT_ACCEPT_NULL_VALUES 
	= MessageBox( _msg, MB_ICONEXCLAMATION, _Box_Title() )

case _Error[1] == ERROR_TABLE_FIELD_RULE_VIOLATED
	_Table = GetTableName(_error[4])
	_Field = proper(field(_Error[5], _Error[4]))
	_Caption = ''
	if( !empty(cursorgetprop('DataBase', _error[4])) )
		_Caption = dbgetprop(_Table + '.' + _Field, 'Field', 'Caption')
		_msg = _error[2]
	else
		=Afields(_afields, _error[4])
		_msg = _afields[_error[5], 8]
		if( at('"', _msg) == 1)
			_msg = eval(_msg)
		endif
	endif
	if( empty(_Caption) )
		_Caption = _Field
	endif
	
	msg_Field = _Table + ': ' + _Caption + ': ' + _msg
	
	_msg	= _Table + ': ' + _Caption + ': ';
				+ _msg + CR + MESSAGE_TABLE_FIELD_RULE_VIOLATED
	_msg = _msg + CR + MESSAGE_CHANGE_OR_UNDO
	= MessageBox( _msg, MB_ICONEXCLAMATION, _Box_Title() )
	
	
case _Error[1] == ERROR_TABLE_TRIGGER_FAILED
	_Table = GetTableName(_error[4])
	_msg = _Table + ': '
	do case
	case _Error[5] == 1		&& Insert
		_msg = _msg + MESSAGE_TABLE_TRIGGER_FAILED_INSERT + CR + MESSAGE_CHANGE_OR_UNDO
	case _Error[5] == 2		&& Update
		_msg = _msg + MESSAGE_TABLE_TRIGGER_FAILED_UPDATE + CR + MESSAGE_CHANGE_OR_UNDO
	case _Error[5] == 3		&& Delete
		_msg = _msg + MESSAGE_TABLE_TRIGGER_FAILED_DELETE
	endcase
	= MessageBox( _msg, MB_ICONEXCLAMATION, _Box_Title() )

case _Error[1] == ERROR_TABLE_RULE_VIOLATED
	_Table = GetTableName(_error[4])
	_msg = _Table + ': ' + MESSAGE_TABLE_RULE_VIOLATED
	local RuleText
	RuleText = dbgetprop(_Table, 'Table', 'RuleText' )
	if( !empty(Ruletext) )
		_msg = _msg + CR + eval(RuleText)
	endif
	_msg = _msg + CR + MESSAGE_CHANGE_OR_UNDO
	 = MessageBox( _msg, MB_ICONEXCLAMATION, _Box_Title() )

case _Error[1] == ERROR_TABLE_KEY_NOT_UNIQUE
	_Table = GetTableName(_error[4])
	_msg = _Table + ': ' + _error[3] + ' ' + MESSAGE_TABLE_KEY_NOT_UNIQUE
	_msg = _msg + CR + MESSAGE_CHANGE_OR_UNDO
	= MessageBox( _msg, MB_ICONEXCLAMATION, _Box_Title() )
otherwise
	if !isnull(_form)
		
		_msg = MESSAGE_FORM_ERROR + ' ' + _form.Name + CR ;
			+ str(_Error[1]) + ':'  + _Error[2] + CR ;
			+ _program ;
			+ ' line ' + str(_Lineno) + CR ;
			+ _linecode

	else
		if( !empty(_program) )
			_msg = 'Error ' + str(nError) + CR + ;
			+ _message + CR + ;
			+ 'Program : ' + _program + CR + ;
			+ 'Line ' + str(_Lineno) + CR + ;
			+ _linecode
		else
			_msg = _message
		endif
	endif
	if( IsRunTime() )
		_Answer = MessageBox( _msg, MB_ICONEXCLAMATION + MB_AbortRetryIgnore, _Box_Title() )
		do case
		case _Answer == IDABORT
			do while(!empty(txnlevel()))
				rollback
			enddo
			if( !isnull(_form) )
				=_form.Release()
			else
				quit
			endif
		case _Answer == IDRETRY
			retry
		case _Answer == IDIGNORE
			return
		endcase
	else
		do case
		case !isnull(_form)
			_Answer = MessageBox( _msg, MB_ICONEXCLAMATION + MB_AbortRetryIgnore, _Box_Title() )
			do case
			case _Answer == IDABORT
				suspend
			case _Answer == IDRETRY
				retry
			case _Answer == IDIGNORE
				return
			endcase
		case !empty(_program)
			_Answer = MessageBox( _msg, MB_ICONEXCLAMATION + MB_AbortRetryIgnore, _Box_Title() )
			do case
			case _Answer == IDABORT
				suspend
			case _Answer == IDRETRY
				retry
			case _Answer == IDIGNORE
				return
			endcase
		case empty(_program)
			_Answer = MessageBox( _msg, MB_ICONEXCLAMATION + MB_OK, _Box_Title() )
			return
		endcase
	endif
endcase
endproc

function	GetTableName(wa)
	local _alias
	
	_alias = iif(empty(wa), alias(), alias(wa) )
	if( !empty(cursorgetprop('DataBase', _alias)) )
		return proper(cursorgetprop('SourceName', _alias))
	else
		return Proper(_alias)
	endif
endfunc
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform