Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert variable into a fieldname? 2

Status
Not open for further replies.

Mandy_crw

Programmer
Jul 23, 2020
585
PH
Hi everyone... May i ask help.... I have P1 to P10 fieldname in my database named payments, i want to replace the value of my fields if it is equal to zero... How would i do it...Thanks and Godbless....

USE payments_old ALIAS pay
INDEX on idnum TO idnumx

LOCATE FOR idnum = "0123456"


IF FOUND()

for i=1 to 10

m.deyta = "P"+transform(m.i)

IF pay.deyta = 0
REPLACE pay.deyta WITH 400
ENDIF

NEXT

ENDIF
 
Hi Mandy,

Use macro substitution (&). One of the most powerful tools of the VFP language.

Only if you think you need it change to : m.deyta = "pay.P"+transform(m.i)

Code:
if &deyta=0
[indent]replace &deyta with 400 [/indent]
endif
 
Hi jacktheC... It worked!!!! but i wanna understand how to use macro & .... Thank you so much....
 
If I understand you correctly you have a value in a variable p0 and want to put that into a field p0, then the easiest way is GATHER MEMVAR.

To explain macro substitution nontheless, it always starts with a a variable which has (partial) code and is substituted = put into place of incomplete code. You can do as crazy things as this:

Code:
lcMacro = 'een.cap'

_Scr&lcMacro.tion = 'Fox Rocks'

What happens when VFP executes such a line of code with & is, it replaces the variable name with its value. Since lcMacro is 'een.cap', that is substituted into the line, so

Code:
_Scr[b]&lcMacro.[/b]tion = 'Fox Rocks'

becomes
Code:
_Scr[b]een.cap[/b]tion = 'Fox Rocks'

Which then is compiled and executed.

It's far less useful in this case when you already know in advance before compiling that the line will be _Screen.caption = 'Fox Rocks' after macro substitution, then you don't need macro substitution at all and simply write what you want directly. Your cae is one that's a good example, but at the same time it's a bad practice to have a table with p0 to p10 field names. You woukld not need macro substitution if you had 10 records instead of 10 fields, which also has the advantage to be easily extended to 20, 100, 100 0records or just 5 instead of 10 fields.





Chriss
 
Thank you Chris for the comprehensive explanation of macro.... i'll take note of your teachings.... Thanks again...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top