سؤال

I'm using a DAO recordset to update a table because of problems enocuntered here.

This works fine when I know the name of the field I'm updating, e.g.:

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

works perfectly.

However, the field that I'm trying to update won't always have the same name, so I attempted to use a variable here too, expecting it to evaluate and be equivilent to the above code:

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

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

I've tried this various ways, including:

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

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

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

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

none of which work.

Am I going about this the wrong way, or am I attempting something impossible?

هل كانت مفيدة؟

المحلول

Try to use this one:

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

نصائح أخرى

Very Simple answer YES you can access VARIABLE FIELD NAMES. For example: I have a form that has multiple buttons... Here's how to name and access them.

Name each Button on your form: MyButton(1) through MyButton(X) The code places the number on each button by changing the caption. stButton is the string value of the Button name

X=10

For Btn = 1 To X

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

StButton.Caption = Btn

Next Btn

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top