Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

VALIDATE DATABASE

This command is a rescue mission. It lets you check whether a database is damaged and, as much as possible, repair the damage.

Usage

VALIDATE DATABASE [ RECOVER ]
        [ NOCONSOLE ]
        [ TO PRINT [ PROMPT ] |
          TO [ FILE ] FileName [ ADDITIVE ] ]

By itself, VALIDATE DATABASE examines the current database and reports any problems it finds. Problems can occur if you delete files that belong to the database without removing them first (for example, if you delete a table using Explorer). You can also wreak havoc by manipulating the DBC as a table, if you’re not careful. VALIDATE DATABASE lets you know what’s wrong.

You can actually issue VALIDATE DATABASE on a database that’s not open exclusively, but all you can get from that is a report as to whether maintenance is needed. With exclusive access, the database’s index gets rebuilt as well.

The RECOVER clause lets you become part of the solution instead of part of the problem. For each inconsistency, you’re prompted to either locate the missing file or delete the offending item from the database. More often than not, it seems the only choice is to remove the offender—we sure wish this command were smarter. Since it’s not, your best bet is often to ignore the errors and open the DBC as a table, and then try to repair the problems manually. As an alternative, you may want to keep Stonefield Database Toolkit or another tool that can repair tables and databases in your arsenal.

In versions prior to VFP 7, VALIDATE DATABASE RECOVER was available only from the Command Window. Using it in a program resulted in an error message. Fortunately, in VFP 7, this command can be used in code, so it's available in a runtime environment. Of course, since this is a potentially dangerous command (it can remove tables from a database, for example) and it displays dialogs that are confusing to the typical end-user ("What is a backlink and why would I want to update or remove it?"), you'll want to use it only in "administrator" functions in your applications.

By default, the validation report appears in the active window. The remaining options let you send it elsewhere.

In VFP 7, if the database has Database Events turned on, the BeforeValidateData and AfterValidateData events fire.

Example

CREATE DATABASE ValTest
CREATE TABLE Test1 (cFld C(1))
INDEX ON cFld TAG cFld
USE
CREATE SQL VIEW TestView AS SELECT * FROM Test1

* Okay, now we've got data. Validate.
VALIDATE DATABASE  && Displays "Database container is valid"

* Now let's see what kind of trouble we can cause
CLOSE DATA
DELETE FILE Test1.CDX
USE ValTest.DBC
LOCATE FOR ObjectType="View"
REPLACE ObjectType WITH "Not a View"
USE

* Now open the database again and see what happens
OPEN DATA ValTest EXCLUSIVE
VALIDATE DATABASE          && Reports lots of problems
VALIDATE DATABASE RECOVER  && Prompts you to fix things up

See Also

AfterValidateData, BeforeValidateData, Open Database, Remove Table, Set Database