using a variable as a field name

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi

What is the correct way to use a variable as the field name and set the
value of it?

Example:

MyVar = "W14" 'Field Name (Changes to other field name with a "For I"
routine)
MyValue = 1050 'Text I need to paste to MyVar field
 
For a field you would go through the fields collection of the recordset to
form the reference - any of the following would work:

rst.fields("W14).value=myValue
rst.fields("W14)=myValue
rst.fields("W" & I).value=myValue

For controls, you go through the controls collection of the form or report
using similar syntax:

forms!MyForm.controls("W14").value=myValue
forms!MyForm.controls("W14")=myValue
forms!MyForm.controls("W" & I)=myValue

The Value property is the default property of fields and of controls so it
is optional. I tend to use it in this type of reference.
 
Thanx, that really sorted me out... Works 100% now...

Regards

Chris

Sandra Daigle said:
For a field you would go through the fields collection of the recordset to
form the reference - any of the following would work:

rst.fields("W14).value=myValue
rst.fields("W14)=myValue
rst.fields("W" & I).value=myValue

For controls, you go through the controls collection of the form or report
using similar syntax:

forms!MyForm.controls("W14").value=myValue
forms!MyForm.controls("W14")=myValue
forms!MyForm.controls("W" & I)=myValue

The Value property is the default property of fields and of controls so it
is optional. I tend to use it in this type of reference.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.
Hi

What is the correct way to use a variable as the field name and set the
value of it?

Example:

MyVar = "W14" 'Field Name (Changes to other field name with a "For
I" routine)
MyValue = 1050 'Text I need to paste to MyVar field
 
Back
Top