Microsoft KB Archive/268059

= INFO: Using Macro Substitution with Index Commands =

Article ID: 268059

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 3.0b for Macintosh

-



This article was previously published under Q268059



SUMMARY
There are several ways to misuse macro expansion and name expressions in the INDEX command. This article demonstrates how to avoid these pitfalls.



MORE INFORMATION
The FoxPro documentation urges the use of name expressions (variables in parentheses) in preference to macro substitution wherever possible. The INDEX command is one place where this appears to be, but isn't necessarily, possible.

All of the following code can be typed or pasted in the Command window, or put together in a program. To begin, uncomment and run the code for your specific version of FoxPro: *!* The sample data changed between each version SET EXCLUSIVE ON
 * !* Necessary to create an index


 * !* *VFP 3
 * !* USE HOME + &quot;samples\data\customer&quot;
 * !* lnTag = 5 && There are already 4 indexes on the customer table.


 * !* *VFP 5
 * !* USE HOME + &quot;samples\data\customer&quot;
 * !* lnTag = 6 && In VFP 5 and above, a &quot;country&quot; index was added.

USE HOME(2) + &quot;data\customer&quot; lntag=6
 * !* *VFP 6

lcField = &quot;City&quot; INDEX ON (lcField) TAG (lcField) Use the TAG function to check the Index tag, and note that you see the following: ? TAG(lnTag) This indicates that name expressions are fine for the TAG clause. However, if you use the SYS(14) function to check the index expression, you see the following, indicating that it did not correctly substitute &quot;City:&quot; ? SYS(14, lnTag) The Help file indicates that an index is created using an expression, so the INDEX command uses the valid expression (lcField) to create the index. If you change the value of lcField at this point, the index uses the new values to display the data in the browse window, as shown: (The currently selected record remains visible because the record pointer has not been moved.) lcField = .F. BROWSE lcField = 0 BROWSE When you remove the variable and try to use the index with this code, RELEASE lcField BROWSE the following sequence of events occurs:  FoxPro returns the error message:
 * !* Prints &quot;City&quot;
 * !* Prints &quot;(lcCity)&quot;

&quot;Variable 'LCFIELD' is not found.&quot;

 FoxPro returns the error message:

&quot;Index does not match the table. Delete the index file and recreate the index.&quot;

 Index is treated as an index on &quot;(lcField)&quot; (that is, a character string) from here on out. If the table is closed and reopened, steps 1 and 3 occur.

To get your intended results, use the following command: INDEX ON &lcField TAG (lcField) INDEX ON &lcField TAG &lcField Another way to misuse macro expansion follows: lcCity = &quot;Seattle&quot; INDEX ON city TAG city FOR city = lcCity BROWSE That works as expected. It shows you the records where the city matches the value of lcCity. But if you want it to be Seattle every time, you might try using the following: INDEX ON city TAG city FOR city = &lcCity. However, this code returns the error message:
 * !* or

Variable 'SEATTLE' is not found.

For this to work, you need to enclose the macro expression in quotes, as follows: INDEX ON city TAG city FOR city = &quot;&lcCity.&quot;

