When a record is deleted by the user, FileFlex does not physically remove the record from the file automatically. Rather, it marks the record as deleted. This allows your program to "undelete" a record.
When you wish to make these deletions irretrievable, you can either use the two-step process of physically removing the marked records and compacting the file to remove unused space or simply zap the records from the file as documented below.
Because of this complexity, FileFlex includes five functions that deal with deleting records:
Marking Records for Deletion
The DBDeleteRecs function marks a range of records for deletion. It takes two arguments: a starting record number and an ending record number. The following function call, then, would mark records 9-15 of the current database file for later deletion:
put DBDeleteRecs(9,15) into dbResult
Unmarking Delete-marked Records
To unmark one or more records previously marked for deletion, use the DBRecallRecs function. Like the DBDeleteRecs function, it takes two arguments defining a range of records previously marked for deletion but not yet deleted. The records are unmarked and will not be deleted by a subsequent use of the DBPack function described below. Here's a sample use of this function:
put DBRecallRecs(11,12) into dbResult
Physically Removing Marked Records
To remove all records that are currently marked for deletion and pack the file to remove unused space resulting from the deletions, use the FileFlex DBPack function. This function requires no arguments since it operates on all previously marked records. Its use would appear something like this:
put DBPack() into dbResult
Physically Deleting Records
When you wish to physically remove one or more records from the current database file in one step, use the DBZapRecs function. It requires two arguments that define a range of records to be marked for deletion and deleted. In effect, the DBZapRecs function combines the results of the DBDeleteRecs and DBPack functions into one operation. Here is a sample of its use:
put DBZapRecs(9,15) into dbResult
Determining if a Record is Deleted
The FileFlex function DBRecordDeleted allows you to determine whether any given record in a database file is marked for deletion. This function requires a record number as an argument and returns "Y" if the record is marked for deletion, or "N" if it is not. For example:
put DBRecordDeleted(39) into recDeleted
Tutorial: Navigating Over Deleted Records
Since FileFlex doesn't remove records that have been marked for deletion, it's often possible to navigate onto a "deleted" record. That is, it's often possible for a search function, or DBSkip or DBGo to move the current record pointer to a record that's marked for deletion. This is often desirable, but there are times it's not.
In order to avoid operating on a delete-marked record, you'll need to check the status of the record before doing any subsequent operation. Let's assume you want to get the value of a record with DBGetFieldByName. The following example shows how to avoid retrieving values from delete-marked records:
repeat while DBRecordDeleted(DBCurrRecNum()) = "Y" put DBSkip(1) into theResult -- you should normally evaluate theResult and handle errors end repeat put DBGetFieldByName("EMPLOYEE") into theEmployee
As the example shows, the routine gets the current record number with DBCurrRecNum, then passes that as a parameter to DBRecordDeleted. This tells DBRecordDeleted to determine if the current record has a deletion flag. As long as the record has a deletion flag, DBSkip moves forward one record in whatever search order has been set up. When the record no longer shows as deleted, the value of the EMPLOYEE field is returned.
Discuss this chapter on the FileFlex Boards.