Referring to a field name object

G

Guest

Access 2003 and a VB Module

I am trying to refer to a field name in a recordset (i.e. rst!fieldname =
"some text") for the purpose of writing data to that field(s) for a newley
created record in a table. The wrinkle to this is that I'm trying to variably
refer to a different field for each iteration of the loop that I'm running so
that the first loop will refer to
rst!.fieldname1, the second to rst!.fieldname2, etc.
I am using a variable to obtain the string value for fieldname1, fieldname2,
etc. in the process but when I use the code
rst!fieldname(i) = "some text"
I receive the message "object not found in collection" and the debug pane
shows that rst!fieldname(i) is set to nothing.
Any help is greatly appreciated.
 
A

Alex Dybenko

Mark,
if you have a field name in variable strFieldName - then you can use:

rst(strFieldName) = "some text"
 
J

John Spencer (MVP)

So your loop should look something like

For I = 0 to Rst.FieldCount -1
rst.Fields(I)="SomeText"
Next I

Or alternatively, using the field

For I = 1 to SomeNumber
Rst.Fields("FieldName" & I) = "SomeText"
Next I

Or if you want the fieldname of the field.

Rst.Fields(0).Name will return the name of the first field
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top