Microsoft KB Archive/129756

= PRB: REPLACE Causes "Field Rule Has Been Violated" Error =

Article ID: 129756

Article Last Modified on 2/10/2000

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q129756



SYMPTOMS
If you programatically or interactively replace a field with an illegal value, you may receive an error number 1582:

Field Rule has Been Violated.



CAUSE
The value that you attempted to place in the field caused a value of .F. to be returned from the validation rule defined in the database container for this field.

If you replaced the field with an invalid value programatically, Visual FoxPro returns error number 1582 and executes an ON ERROR routine if one is in effect. If you replaced the field interactively, no error number is returned, and the ON ERROR routine is not executed.

You will be unable to move the pointer (cursor) off the offending field until an acceptable value is typed in.



RESOLUTION
Your ON ERROR routine must handle error 1582. Although it may seem logical to have your ON ERROR routine issue a REPLACE statement to replace the field with the OLDVAL function, you should simply return .F. from your ON ERROR routine. When you return .F., Visual FoxPro takes care of returning the field's value to its value before the change.

By returning .F., you enable your ON ERROR routine to handle both existing and newly appended records. The OLDVAL function always returns blank for an appended record, even if the database container specifies a default value for the field. The code snippet below demonstrates how to handle error 1582 in an ON ERROR routine: ON ERROR DO errhandler

PROCEDURE errhandler = AERROR(aLastErr)

DO CASE CASE aLastErr(1) = 1582 RETURN .F.        * Include code to handle other anticipated errors. ENDCASE



Steps to Reproduce Behavior
  Create a new database container and modify it by issuing the following commands in the Command window: CREATE DATABASE test MODIFY DATA  Choose the New Table icon from the Database Designer toolbox, and add a new table named STATE. This table should contain one field, stcode.  Add a validation rule to the stcode field that uses this expression: IIF(stcode = "NC", .T., .F.)   Create a program that contains the following lines of code, and run it. SET MULTILOCKS ON  =CURSORSETPROP("Buffering",5) OPEN DATABASE test USE state APPEND BLANK REPLACE stcode WITH "XX" 

An error 1582 should occur.

Additional query words: VFoxWin 3.00

Keywords: kberrmsg kbcode KB129756

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.