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.

RECNO()

This function returns the physical position number for a record. It also has an optional parameter that makes it part of FoxPro’s “soft seek” process.

Usage

nReturnValue = RECNO( [ cAlias | nWorkArea  | nSoftSeek ] )

Parameter

Value

Meaning

cAlias

Character

Return the record number in the table open as cAlias.

Omitted

If nWorkArea is also omitted, return the record number in the current work area.

nWorkArea

Numeric

Return the record number in the table open in work area nWorkArea.

Omitted

If cAlias is also omitted, return the record number in the current work area.

nSoftSeek

0

Return the record number of the closest match to the last SEEK.

Omitted

Return the record number of the current record in the specified work area.

RECNO() has two totally different functions, one of which most FoxPro users are familiar with and the other far more obscure. When the nSoftSeek parameter is omitted, RECNO() finds the physical record number of the current record in the specified table.

When 0 is passed for nSoftSeek, RECNO() follows up on the last SEEK and returns the record number of the closest match found. If no match is found, RECNO(0) returns 0. Soft seek can be used only in the current work area—you can’t specify an alias or work area number together with the zero. The definition of “closest match” is based on the normal string-matching scheme used in FoxPro. That is, start matching from the left and stop when you can’t match anymore. Unlike a regular comparison, soft seek can ignore non-blanks after the matching portion of either string. For example, if you SEEK “Comfort”, and there’s no record for “Comfort”, but there is one for “Compass”, RECNO(0) points to the “Compass” record, even though “Compass” is never equal to “Comfort”, regardless of the setting of SET EXACT. We can’t remember ever actually using this information, though—on those rare occasions when we want to do a soft seek, we tend to use SET NEAR. Remember that the physical record number isn’t terribly meaningful. The only place it’s really useful is with GOTO. Pretty much the only time we ever use record numbers is when we need to keep track of the current record pointer position when we want to move it temporarily.

VFP’s data buffering adds a new wrinkle to record numbers. When table buffering is on, new records are assigned temporary record numbers until they’re committed to the table. The temporary record numbers are negative, starting with -1. Be careful when working with these record numbers. First, you need to be aware that some record numbers may be negative. Second, the record number for a particular record may change (usually when a previously added record is removed with TableRevert()), so you can’t just save a record number and assume that you’ll be able to find that record again. All this is one more reason not to rely on record numbers.

And, if you need one more reason, be aware that using RECNO() in a query isn’t reliable either, unless the query involves only a single table.

Example

USE Customer
? RECNO()        && returns 1
SKIP 5
? RECNO()        && returns 6

* now check out softseek
SET ORDER TO Company_Na
SEEK 'CAESAR'
? FOUND()  && returns .F.
? RECNO(0) && returns 13, which is "Centro comercial Moctezuma"

* hold pointer position and return
IF EOF()
   nCurrentRec=0
ELSE
   nCurrentRec=RECNO()
ENDIF
* do some processing
IF nCurrentRec=0
   GO BOTTOM
   SKIP
ELSE
   GO nCurrentRec
ENDIF

See Also

GoTo, Seek, Set Near