Solved AddNew method with field from variable (array)

Fodo

New member
Local time
Today, 04:30
Joined
Jun 28, 2024
Messages
2
Hi,

I have two arrays, first with names of fields and second with values which I want to add to my recordset. But my code doesn't work. Is possible to do it by this way?

In line myR![label(i)] = data(i) break my code with "Runtime error '3265': Item not found in this collection".

Problem can by in square brackets. When I write myR![Customer] = data(i) (Customer is name of field) code is working. From my code is output myR!["Customer"] = data(i). I don't know how to solve it. Can you help me?


Code:
Sub AddNewItem(table, data, label)

    Dim myR As Recordset
    Set myR = CurrentDb.OpenRecordset(table)

    myR.AddNew
   
    For i = 0 To UBound(data) - 1
        myR![label(i)] = data(i)
    Next
   
    myR.Update
   
    myR.Close
    Set myR = Nothing

End Sub
 
Try

myR(label(i))=

Also your update needs to be within the loop
 
The bang operator is not suitable at this point.
Code:
' using the recordset's field collection
myR.Fields("label" & i) = data(i)

' using the passed array
myR.Fields(label(i)) = data(i)
 
Thank you very much.

Solution myR.Fields(label(i)) = data(i) working
 

Users who are viewing this thread

Back
Top Bottom