The FoxPro GOTO statement

Early programming languages such as BASIC relied on the GOTO command to control the flow of program execution by jumping to another position in the program. A large project would end up as "spaghetti code" with a confusing tangle of jumps. FoxPro has never used GOTO in this way but has a variety of control structures instead.

Despite this, FoxPro does in fact have a GoTo statement but it is nothing to do with program control. Instead it moves the record pointer to the record number specified. These examples:

Goto 12


Go 12

will move the record pointer to record number 12. Note that they will move the record pointer regardless of the setting of SET DELETED or SET FILTER. If you move the pointer to a record that it's not supposed to be able to reach then the next SKIP command will move the pointer to the next available record. This can be a source of obscure bugs.

The command does not need to have an literal numeric value and is even more confusing when used with a variable - especially a global variable that has been set elsewhere in the program.

topp = 12
*-- Lots of other program lines.
*-- Lots of other program lines.
Go Topp


This command should not be being used in new work but you may find it in older systems. At first sight it looks like an old GW-BASIC instruction with a jump to line 12 but of course FoxPro does not use line numbers.

The word "Goto" is actually optional and the command can be shortened. The simple statement:


on its own will move the record pointer to record 12.

This an idiosyncracy of the language which should never be used in production code.

The Goto command was useful in small record sets but would be slow in a large table and it goes completely against the spirit of a relational database. When you use it you assume that you will always be able to rely on the absolute position of a record in a data set and this means you restrict yourself to using native FoxPro data sources.

Introduction | Environment | Project | Tables | Forms | Navigation | Executable