Always Select or Specify a Work Area

by Frank Perez December 14, 2008

Just the other day I got burned by what I consider a rookie mistake. I was working with a Visual FoxPro application (compiled as a multi-threaded DLL) that records application events to a Visual FoxPro table. To limit the size of the log table, the application re-used records after 500,000 events by updating the oldest record instead of adding new records.

As a general rule I know that I should always use a SELECT command before performing any command or function that processes a work area. For example, in the following sample code the SCATTER command is used to copy data from the current record to an array. Because this command processes the current work area only, I should always select the desired work area first.

* create a cursor with a single record
CREATE CURSOR "CURSOR01" (character1 C(10))
INSERT INTO "CURSOR01" (character1) VALUES("VALUE01")
* create another cursor with a single record
CREATE CURSOR "CURSOR02" (character1 C(10))
INSERT INTO "CURSOR02" (character1) VALUES("VALUE02")
* before calling the SCATTER command, select the work area
SELECT("CURSOR01")
SCATTER NAME loValues
* this should display "VALUE01"
WAIT WINDOW loValues.character1

The exception to this rule is any command that has an IN clause or alias parameter. For example, in the following sample code the REPLACE command can safely be used without selecting the desired work area first.

* create a cursor with a single record
CREATE CURSOR "CURSOR01" (character1 C(10))
INSERT INTO "CURSOR01" (character1) VALUES("VALUE01")
* create another cursor with a single record
CREATE CURSOR "CURSOR02" (character1 C(10))
INSERT INTO "CURSOR02" (character1) VALUES("VALUE02")
* this should display "CURSOR02"
WAIT WINDOW ALIAS()
* change the value in CURSOR01 cursor
REPLACE character1 WITH "NEWVALUE" IN "CURSOR01"
* this should display "NEWVALUE"
WAIT WINDOW CURSOR01.character1

In this particular situation, I was using a LOCATE command without specifically selecting the work area first. In an application compiled into an EXE, executing the LOCATE command when the current work area is blank would cause an Open File Dialog similar to the one below to appear.

However, in an application compiled into a DLL, the LOCATE command appears to be ignored if the current work area is blank. An error does not get thrown and the program simply executes the next line of code. :(

Fortunately, I was able to track down and fix the problem quickly. I'm sure that this is one of those mistakes that we all make at one time or another. I'm just hoping that by blogging about it, I will be less likely to make it again {g}.

Keywords:

Filed Under: VFP

Comments are closed

About Frank

Frank lives in West Bloomfield, Michigan with his wife and three children.  When he is not writing code, he enjoys long distance running and riding his motorcycle.

Month List

Tag Cloud