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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a variable as a field name 1

Status
Not open for further replies.

noeuro

Programmer
Apr 9, 2003
1
GB
My table has n fields, named fld1, fld2, fld3,....fldn
I want to loop through them ..
for i=1 to n
strF="fld" & str(i)
rec.edit
rec!strF = .....
Of course, this doesn't work. How do I get round it?
 
noeuro,

Have you tried:
Dim strF as string
Dim strTmpF as string

for i=1 to n
strTmpF="fld" & str(i)
strF=strTmpF
rec.edit
rec!strF = .....

Don't have a ready application to test this on my end.

Hope this helps.
DougCranston
 
Simple change in syntax should do the trick. Try the following:

dim strF as string
for i=1 to n
strF="fld" & str(i)
rec.edit
rec(strF) = .....
rec.update
etc.

I assume that you're opening the recordset appropriately, though you're not showing any of that in your snippet.
Use the bracket syntax as illustrated, instead of the bang syntax (ie. !fieldname) if you are using a variable instead of a hard coded fieldname.

Generally speaking, you could use rec("fld1") or rec!fld1 interchangably.

Hope this answers your question,
Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
You can have access to the fields for any recordset like this:
DIM DB AS DATABASE,RS AS RECORDSET,N AS INTEGER,ST
SET DB=CURRENTDB
SET RS=DB.OPENRECORDSET(&quot;<<TABLE NAME OR SQL STATEMENT>>&quot;)

FOR N=0 TO RS.FIELDS.COUNT-1
ST=RS.FIELDS(N).NAME 'IF YOU WANT FIELD'S NAME OR
ST=RS.FIELDS(N).VALUE 'IF YOU WANT FIELD'S VALUE OR
RS.EDIT 'IF YOU WANT EDIT RECORD
RS.FIELDS(N)=<<ANY VALUE>>
RS.UPDATE
NEXT N
RS.CLOSE
SET RS=NOTHING
DB.CLOSE
SET DB=NOTHING
GOOD LUCK
GNIKOL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top