Creating an array with SQL

It's easy to create an array with SQL in FoxPro by using the INTO ARRAY clause in an SQL SELECT statement. This example creates an array of customer details:

SELECT Cust_ID ;
   FROM Customer ;
   INTO ARRAY laCust ;
   WHERE Country = 'England'

If all goes well, the SQL statement will create an array of the right size and shape to hold the records retrieved. This example is simple, there are some records for customers from England in this table so this piece of SQL will create an array holding their names.

The real world is more complicated because you will not know whether or not any records are going to meet the criterion of the WHERE clause. If no records match then nothing will be returned and the array will not be created. Any later attempt to refer to this non-existent array will cause an error.

The solution is to test the _TALLY memory variable to test whether any records have been returned:

IF _TALLY = 0
  *-- The array has not been created by the SQL.
  *-- Create a single-element array and store ' '
  *-- in the only element.

  DIMENSION laCust[1]
  laCust[1] = SPACE (1)
ENDIF

You don't get this problem when using SQL to send information to other destinations such as a cursor or table. FoxPro can create an empty cursor or table if no records happen to be selected but it cannot create an array of length zero.

FoxPro maintains the number of records in _TALLY automatically and updates it whenever any of the following commands are used:

  • APPEND FROM
  • AVERAGE
  • CALCULATE
  • COPY TO
  • COUNT
  • DELETE
  • INDEX
  • PACK
  • REINDEX
  • REPLACE
  • SELECT - SQL
  • SORT
  • SUM
  • TOTAL
  • UPDATE

Strangely enough, the _TALLY memory variable is not read-only. You can alter the value that it holds:

_TALLY =1

but it's difficult to know why you'd want to do this.

MS Access Tips

Visual FoxPro Tips

General Tips

 

Related Items

Alvechurch Data - Microsoft FoxPro and Access Database Development and Training

Alvechurch Data are based in Worcestershire and provide Microsoft Access and Visual FoxPro training, development and support.

Read More

Autocomplete in Visual Fox Pro

VFP textbox with Autocomplete

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.

Read More

FoxPro Functions

FoxPro has always had functions like FREAD and FWRITE to read and write files at a low level. They can handle binary files which defeat the STRTOFILE and FILETOSTR functions.

Read More

Development Services

Alvechurch Data specialise in Microsoft Access and Visual FoxPro and have fifteen years experience developing databases for small business use.

Read More

Access and Visual FoxPro Training

Visual FoxPro and Microsoft Access training courses and workshops offered by Alvechurch Data in Worcestershire.

Read More