VFP Tutorial - Program Code

Visual FoxPro uses the same language in programs, functions and methods as it does in the interactive Command Window.

Prev button

This button will move us to the previous record in the table so we must make sure that the user cannot try to navigate beyond the first record. An If statement in the click method of the Prev button will detect whether we are trying to move to the record before the first one and this code will prevent the user seeing an error message.

It uses the BOF() function to determine whether we are at "Beginning of File":

* Prevent the Prev button going to far
If Bof() Then    * Do nothing - we are already on the first record
Else
   * Skip to the previous record
   Skip -1
EndIf

The Then in the first line is optional. Anybody who works in Visual Basic as well as in FoxPro will already be in the habit of adding it because it's compulsory in that family of languages. The final EndIf is one word. You will get a syntax error if you use two words in VB style End If.

The Else clause is optional but the components of the If structure must always be on separate lines.

Next button

The Next button needs to detect whether we are at "End of File" to prevent the user moving beyond the last record. It has similar code using the EOF() function but the logic has to be more complex because the EOF() function does not become true until after the record pointer has moved beyond the last record.

* Prevent the Next button pushing us off the end of
* the data

If Eof() Then    * We've already fallen off - go to the last record
   Go Bottom Else
   * Skip to the next record
   Skip 1
   * Has this move made us fall off?
   If Eof() Then      * We have fallen off - go to the last record
     Go Bottom    Else
     * Do nothing - we're safe
   EndIf
EndIf

Enabling buttons

Preventing the user from seeing these error messages improves the usability of the form. Another improvement would be to set the Enabled property of the Next and Last buttons false when we are on the last record. This will grey out the buttons to make it obvious that the user cannot move any further in that direction.

ThisForm.cmdNext.Enabled = .F.
ThisForm.cmdLast.Enabled = .F.

We will need similar commands to disable the Prev and First buttons when on the first record.

Note that you have to give the full names of the buttons. They have no independent existence so you must specify them completely.

True and False values are represented by .T. and .F. in FoxPro. They are a separate logical data type and are not related to integers in any way. FoxPro uses .Null. to represent a null value in any data type.

Going further

These two pages give more details about variables and programming.


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

Access Tips

FoxPro Tips

General Tips

 

Related Items

Visual FoxPro Tutorial - Program control

Program control structures in Visual FOxPro

Read More

Visual FoxPro Tutorial - Do Case

Visual FoxPro Tutorial - Using the Do Case structure to control program execution

Read More

Visual FoxPro Tutorial - Development Environment

The Integrated Development Environment (IDE) for Visual FoxPro

Read More

Visual FoxPro Tutorial - Build an executable

Visual FoxPro Tutorial - Build a Windows executable from a Foxpro project

Read More

Visual FoxPro Tutorial - Form Designer

Visual FoxPro Tutorial - Using the Form Designer

Read More