PRB: REPLACE Causes "Field Rule Has Been Violated" Error (129756)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0

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 <fieldname> 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(<fieldname>) 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
				

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new database container and modify it by issuing the following commands in the Command window:
       CREATE DATABASE test
       MODIFY DATA
  2. Choose the New Table icon from the Database Designer toolbox, and add a new table named STATE. This table should contain one field, stcode.
  3. Add a validation rule to the stcode field that uses this expression:
       IIF(stcode = "NC", .T., .F.)
  4. 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.

Modification Type:MajorLast Reviewed:2/10/2000
Keywords:kbcode kberrmsg KB129756 kbAudDeveloper