PDA

View Full Version : Variable for table field


ABC123

jas
04-16-2008, 01:54 PM
It seems to me there is a way to do this:

I want to use a variable for the table pointer and field to enter a record.
For example:

dim t as p
t=table.open("Customer')
t.enter_begin()
t.lastname="Smith" <<<<<<<<<<<<<<<<<<
t.enter_end()
t.close()

Since the field I will be writing to will change based on other criteria
I want to replace t.lastname with an expression that would evaluate to t.lastname.

like:
dim fldname as c
fldname="Firstname"

"t."+fldname="James"

Thanks
JIim

Stan Mathews
04-16-2008, 02:20 PM
You need the eval() function.


tbe = tbl.open("a_email")
? tbe.FROM_NAME
= "Stan Mathews "

fldname = "FROM_NAME"
? eval("TBE."+fldname)
= "Stan Mathews

Sorry, misread the post. You need evaluate_template() to write to the field. The other suggestion reads/evaluates the fieldname supplied.


tbe = table.open("a_email_outbox")
? tbe.Sent_by
= "SMathews@Houchensindustries.com "
tbe.enter_begin()
evaluate_template("tbe."+fldname+"="+quote("AMathews@Houchensindustries.com"))
tbe.enter_end(.t.)

? tbe.Sent_by
= "AMathews@Houchensindustries.com

Or


tbe = table.open("a_email_outbox")
? tbe.sent_by
= "SMathews@Houchensindustries.com "
fldname = "Sent_by"

tbe.enter_begin()
eval("tbe."+fldname) = "AMathews@Houchensindustries.com"

tbe.enter_end(.t.)

? tbe.Sent_by
= "AMathews@Houchensindustries.com

jas
04-16-2008, 03:21 PM
Steve: thanks for your quick reply, but I don't think I explained myself properly. There are 24 fields in a table...M1 - M24...I will enter one record in the table putting data only in the "clientno" field. Then I need to loop 24 times, writing to the same record but to field M1 on the first loop, field M2 on the second loop and so forth thru field M24.

dim t as p
t=table.open("mytable')
t.enter_begin()
t.clientno= "000001"
t.enter_end()
t.close()

dim count as n
count=24
Begin:
if count=0
goto finished
end
else
'''''''''do some stuff here that will give me the criteria for the "M" number
'''''''''and the data that needs to be written to the field

dim t as p
t=table.open("mytable')
t.change_begin()
??????
t.change_end()
t.close()
count=count-1
goto begin
end if
Finished:
end

Where the ?????? is, I need a variable string that evaluates to
" t.M1 = something" the first time thru
" t.M2 = something else" the 2nd time thru etc. Otherwise I have to repeat the code 24 times. I tried using eval() but couldn't get it to work in this context.

I hope this makes what I'm trying to do clearer.
Thanks
Jim

Stan Mathews
04-16-2008, 03:40 PM
See how this works.

If you are wanting to write to the record just created in the first part, remove the first t.close() and don't open and close inside the loop.

dim t as p
t=table.open("mytable")
t.enter_begin()
t.clientno= "000001"
t.enter_end()
t.close()

FOR count = 1 TO 24
'''''''''do some stuff here that will give me the criteria for the "M" number
thispass = "M" number derived for this loop
'''''''''and the data that needs to be written to the field
thisfldvalue = the data that needs to be written to the field
dim t as p
t=table.open("mytable")
t.change_begin()
eval("t.M"+alltrim(str(thispass))) = thisfldvalue
t.change_end()
t.close()
NEXT count
t.close() 'here's where we close
end

jas
04-16-2008, 04:22 PM
Thanks much Stan. It works like a charm!!

Jim