How to Determine Whether a Record Is Blank or Empty

ID: Q137410

The information in this article applies to:

SUMMARY

FoxPro supplies functions to determine if the contents of a single field is blank (with ISBLANK()) or empty (with EMPTY()). This article will show how to use these functions to create user-defined functions (UDFs) that will return True (.T.) or False (.F.) to indicate if the entire record is blank or empty.

For more information about the ISBLANK() function, please see the FoxPro Help file or the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q99095
   TITLE     : ISBLANK() Function Provides Additional Null Value Support

   ARTICLE-ID: Q102453
   TITLE     : How to Test for a Blank Character Variable

MORE INFORMATION

Create a program/procedure file containing the following UDF:

   * Function: IsBlankRec
   *
   * Parameter: <expN> | <expC> - optional
   *
   * where <expN> is the number of the work area to check if record is
   *  blank or, where <expC> is the name of the alias to check if record is
   *  blank
   *
   FUNCTION IsBlankRec
   PARAMETER lAlias
   PRIVATE llSeleBack, lnSeleBack, llResult, lcFieldName, I

   llSeleBack = .F.
   lnSeleBack = SELECT(0)
   llResult = .T.

   IF PARAMETERS() = 1
      llSeleBack = .T.
      SELECT (lAlias)
   ENDIF

   FOR I = 1 TO FCOUNT()
      lcFieldName = FIELD(I)
      IF ! ISBLANK( &lcFieldName )
         llResult = .F.
         EXIT
      ENDIF
   ENDFOR

   IF llSeleBack
      SELECT (lnSeleBack)
   ENDIF

   RETURN llResult

This function tests to see if it needs to change work areas by the number of parameters passed to the UDF. It then loops through all the fields in the current work area, testing each field with the ISBLANK() function. The first field that returns NOT BLANK causes the memory variable llResult to be set to false (.F.) and exits the For loop. If necessary, the UDF changes back to the original work area and then return the results of the search for blank fields. If all fields in the record are blank, the UDF returns True (.T.).

NOTE: To test for an empty record, you may change the name of the function to IsEmptyRec and replace the following line:

   IF ! ISBLANK( &lcFieldName )

with:

   IF ! EMPTY( &lcFieldName )

Testing the Results

1. Place both functions in a .prg file.

2. In the Command window, type the following where <myproc> is the name of

   the .prg file created in step 1:

   SET PROCEDURE TO <myproc>

3. Open the Customer.dbf file located in the Tutorial directory, located
   in your Foxprow, Fpd26, or Fpw26) directory.

4. While on record number 1, enter the following command in the Command
   Window:

   ? IsBlankRec(), IsEmptyRec()

   Your results should be:

   .F. .F.

5. Type the command APPEND BLANK in the Command window.

6. While on the new record, enter the following command in the Command

   Window:

   ? IsBlankRec(), IsEmptyRec()

   Your results should be:

   .T. .T.

7. Edit the new record by entering zeros in the fields: Ytdpurch, Lat, and
   Long.

8. While on the new record, enter the following command in the Command
   window:

   ? IsBlankRec(), IsEmptyRec()

   Your results should be:

   .F. .T.

   The results are different because the fields Ytdpurch, Lat, and Long
   contain the value 0. ISBLANK() does not consider numeric fields
   containing zeros to be blank, but EMPTY() does consider them to be
   empty.

Additional reference words: FoxWin FoxDos 2.50 2.50a 2.50b 2.60 2.60a KBCategory: kbprg kbcode KBSubcategory: FxprgGeneral

Last Reviewed: September 29, 1995