How to Use READKEY() to Test If Data Has Been Changed

Last reviewed: June 27, 1995
Article ID: Q114671
The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, 2.5b, 2.6
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, 2.5a, 2.5b, 2.6
  • Microsoft FoxPro for Macintosh, versions 2.5b, 2.5c

SUMMARY

The UPDATED() function returns .T. if any object has changed during a READ, or if a control such as a push button has been selected.

This article explains how you can also determine if the data itself has changed during a READ.

MORE INFORMATION

The READKEY() function provides an easy method of testing if the current user has made any modifications to the data during the READ. It does not test if another user has modified the same record residing on a network drive.

READKEY() can be used to return two distinct sets of values. If READKEY() is issued with no arguments, as in the following example, it returns a value in the range 0-36 if no data has changed, and 256-292 if data has changed:

   ?READKEY()

If any numeric argument is specified, as in the following example, READKEY() returns a value in the range 1-6, indicating the method used to terminate the READ:

   ?READKEY(0)

For information about possible READKEY() return values, refer to the "Language Reference" or the FoxPro Help file.

The following steps will create a simple screen based on memory variables. A push button labeled Cancel will test if the data has been changed. If no data has been changed, the READ will terminate. If any data has been changed, a wait window will be displayed and the user will remain within the READ.

  1. USE the CUSTOMER database in the TUTORIAL subdirectory. Create a quick screen based on memory variables.

  2. In the Setup code snippet, enter the following code:

          m.cancel = ""
          SCATTER MEMVAR
    

  3. Add a terminating push button with the prompt "Cancel". Enter M.CANCEL
as the variable for the push button.

  1. In the READ VALID snippet, enter the following code:

          IF READKEY(0) = 3     && Execute only if terminating button chosen
             IF readkey() > 255
                WAIT WINDOW "Can't cancel ... Data has changed"
                RETURN .F.      && Don't allow READ to terminate
             ELSE
                RETURN .t.      && Terminate READ
             ENDIF
          ENDIF
    
    

  2. Generate and run the screen program.

The value returned by READKEY() is valid only while the record pointer remains on the current record. If the record pointer is moved, READKEY() will reset to a value indicating that no data has been changed.

The code to test for the value of READKEY() must be located in the READ VALID snippet or in the Cleanup code, not in the VALID snippet of the push button. The value of READKEY() will not change until after the VALID snippet of the push button has been executed.

REFERENCES

For additional information about how to test if data on disk has been changed by another user, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q97638
   TITLE     : How to Check for Changes Made in a Database Record

In a multiuser environment, you may need to use both techniques in order to guarantee data integrity.


Additional reference words: FoxMac FoxDos FoxWin 2.00 2.50 2.50a 2.50b 2.60
2.50c
KBCategory: kbprg
KBSubcategory: FxprgMultiuser


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 27, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.