Record is out of range (#5)

Something tried to access a record that does not exist in the selected table.

Cause: Index corruption

Cause: Listbox and Requery

A listbox is bound to a cursor using the RecordSourceTypes
2 (Alias) or
6 (Fields) and its value points to any record. Now you reduce the number of records in this RecordSource. Typically the error appears when you use ZAP, but occasionally it also appears upon re-executing a SELECT statement or using the REQUERY() statement.

Once this happened, the error likely to appear in various situations. It happens, when you access a property of the listbox. But it also happens when VFP internally refreshes the listbox. The reason is simple. Visual FoxPro stores the number of the current record and navigates to this record when you access the listbox, or when it needs to repaint it. When you removed records and there's no record with the old number, then you get the error. The same applies to ComboBoxes, as well.

Remains the question why you don't always get it. Visual FoxPro appears to contains a lot of code that notifies controls of a change in a table. That explains, why the grid is immediately blanked out, even though there's no need for the grid to repaint it. These event notifications are necessary to avoid errors like this one. When you change the number of records of the RecordSource from within the Requery() method, you don't see any problem. That's obvious, because VFP knows that the RecordSource is likely to change. But even when you execute SELECT...FROM somewhere in the program, the listbox seems to be notified sometimes and does not raise an error. ZAP seems to be the least cooperative command, as it causes most such problems.


The most obvious reason for this error is an invalid record number used by the GOTO or LOCATE RECORD command. Valid record numbers are 1 through RECCOUNT() and negative numbers for added records. LOCATE RECORD can also get RECCOUNT()+1 as a parameter and positions the table at EOF() in this case.