FoxPro at development and runtime

You do not want to be using the live data for development so you must switch between test and live data when you distribute your application. If you do this manually then there is the danger that one day you will forget. It's better to switch automatically.

The problem falls into two parts:

  • detecting the environment.
  • switching to a new data path.

Detecting the environment

At different times I have used four techniques to determine whether or not the executable is running from within the development environment. Each has its advantages and drawbacks:

  1. Use the VERSION(2) function to determine which version of FoxPro is running. This returns 0 for RunTime, 1 for Standard, and 2 for Professional.
  2. Use the SYS(0) function to get the user's logon name. SYS(0) returns a string in the form:
      user name#machine name
    so I use the AT() function to find the position of the hash then read all the characters to the left of that position. If the user name is 'GEOFFF' then I assume that this is me working in development mode. A simple approach that can give me debugging information in the runtime environment on any machine.
  3. Pass a parameter into the executable. Treat Main.Prg as though it were a procedure by starting with an LPARAMETERS statement and pass the parameter in by adding it to the command line in the desktop shortcut. Use the PCOUNT() function early in Main.Prg to detect whether or not a parameter has been supplied. If there's no parameter, I must be in development mode. If there is a parameter I can either switch to a predetermined data directory or read the data path from the parameter.
  4. Use the FILE() function to see if there is an INI file in the same directory as the executable. As with the parameter method above, I can either use the existence of the file to indicate runtime mode or I can read the path to the data directory from the ini file.

Whichever technique I have used to detect the environment, I always set up a global flag so that I can use it throughout the application to set development options like SET ESCAPE ON.

Switching to a new path

You can either use the SET PATH command to tell FoxPro where to find its data files or build a fully-pathed name for each file explicitly. I prefer to use the SET PATH technique because it will apply throughout the application. It can be useful to store other files - such as an audit log for example - in the data folder too.

There are two techniques to use with the SET PATH command. One option is to name the data path explicitly:

IF glDevMode
   *-- Use the test files in the local Data folder.
   SET PATH TO Data
ELSE
   *-- Use the data on network drive J
   SET PATH TO j:\LiveData
ENDIF

The other technique is to build (or read) the path as a string and use macro substitution:

SET PATH TO &lcDataPath

In both cases remember that you will have to add quotes around the path if it includes spaces.

Access Tips

FoxPro Tips

General Tips

 

Related Items

Starting FoxPro

Startup options to customise the FoxPro development environment

Read More

FoxPro configuration file

Using and distributing the FoxPro configuration file

Read More

FoxPro command line switches

FoxPro command line switches

Read More

VBA Code for Events in Access

How to open the VBA method code editor automatically in Microsoft Access

Read More

Startup Options for a Microsoft Access database

Different ways of using the Access StartUp options to improve database security

Read More