I have this small VBA script in an Access database. There are a number of field comparisons I would like to do. In other languages, I would be able to make an array of keys and then use a for..each loop in order to test each key. Something like this:
string[] myKeys = { "key1", "key2", "key3" };
foreach(string myKey in myKeys)
{
if(myRecordset!myKey == myTable!myKey) DoSomething()
}
I tried this in VBA, but it gives me Run-time error ‘3265’: Item not found in this collection when using the following code:
For Each myKey In myKeys
If Not IsNull(myR![myKey]) Then Me.Recordset![myKey] = myR![myKey]
Next myKey
Is what I am trying to do even possible in VBA?
If you want to reference the value of a field whose name is stored in your variable
myKey, use this …That will work whether your recordset is a DAO or ADO object.
Error 3265, “Item not found in this collection”, isn’t a complaint about the array. It’s telling you your recordset has no field whose literal name is “myKey”. Perhaps this code will make it clearer. It works without error as written. Un-commenting either of the first 2
Debug.Printlines will trigger error 3265.