×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to convert variable into a fieldname?
2

How to convert variable into a fieldname?

How to convert variable into a fieldname?

(OP)
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

RE: How to convert variable into a fieldname?

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 --> vfp

if &deyta=0
replace &deyta with 400
endif

RE: How to convert variable into a fieldname?

(OP)
Hi jacktheC... It worked!!!! but i wanna understand how to use macro & .... Thank you so much....

RE: How to convert variable into a fieldname?

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&lcMacro.tion = 'Fox Rocks' 

becomes

CODE

_Screen.caption = '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

RE: How to convert variable into a fieldname?

(OP)
Thank you Chris for the comprehensive explanation of macro.... i'll take note of your teachings.... Thanks again...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close