VFP Tutorial - Program Code

Microsoft's Visual FoxPro language uses the same syntax in programs, functions and methods as it does in the interactive Command Window. The easiest way to start programming is to test your code in the Command Window and then copy it (or drag and drop it) into the program.

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" in the table so that it can prevent us trying to move any further forward:

* Prevent the Prev button going too 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 any dialect of 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. It's optional in FoxPro so there's no harm in being consistent.

The comment in the empty If clause is a personal habit. I like to use positive logic in an If because I know that negative logic leads to double (and sometimes triple) negatives.

An annoying inconsistency between Visual Basic and Visual FoxPro is that the final EndIf is one word in FoxPro. You will get an irritating syntax error if you use two words in the VB style End If.

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

Note that, unlike VB, FoxPro cannot accept GoTo after the If criterion. The FoxPro language has never had the unstructured jump command. It does however have a GoTo command of its own but this moves the record pointer within a table, it doesn't jump within a program.

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 as being part of the form.

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. Visual 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

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More tips from Alvechurch Data

Visual FoxPro Tutorial - The Goto statement

The truth about the FoxPro Goto statement

Read More

Visual FoxPro Tutorial - If ... Else ... EndIf

Using If EndIf to control program flow in Visual FoxPro

Read More

Visual FoxPro Tutorial - Navigation

Navigating between records in Visual FoxPro

Read More

Visual FoxPro Tutorial - Scan ... EndScan loop

Using a scan loop to control program flow in Visual FoxPro

Read More

Visual FoxPro Tutorial - Variables

Using variables in Visual FoxPro

Read More