Can you use a variable for a field name when using AddNew in a recordset?

I am using a DAO recordset to update a table due to problems with enocuntered here .

This works fine when I know the name of the field I'm updating, for example:

rs2.AddNew
rs2![ContactID] = rs.Fields(0).Value
rs2![Fee Protection Insurance] = "" & strValue & ""
rs2.Update

works great.

However, the field I'm trying to update will not always have the same name, so I also tried to use the variable here, expecting it to be evaluated and equivalent to the above code:

rs2.AddNew
rs2![ContactID] = rs.Fields(0).Value
rs2!["strFieldName"] = "" & strValue & ""
rs2.Update

but it tells me that the item is not in the collection, even when strFieldName is set to Fee Protection Insurance.

I tried these various ways, including:

rs2![" & strFieldName & "] = "" & strValue & ""

rs2![strFieldName] = "" & strValue & ""

rs2!["" & strFieldName & ""] = "" & strValue & ""

rs2![cStr(strFieldName)] = "" & strValue & ""

none of them work.

Am I mistaken about this, or am I trying to do something impossible?

+3
2

:

rs2.Fields(strFieldName) = "" & strValue & ""
+4

, . : , ... , .

: MyButton (1) MyButton (X) , . stButton -

= 10

Btn = 1 To X

StButton = Me ( "MyButton" "(" Btn ")" )

StButton.Caption = Btn

Btn

0

All Articles