Hello!
Here is my issue:
I wrote a code on Excel to update an Access Table. I am using a ADODB recordset. I want to update the value of certain columns (Fields). These columns are variables.
I know the code to update a hard coded column is :
and it works when i test it.
But I need to update based on a variable column.
I tried this :
and this :
But none of these work, it returns a run-time error 3265 which means it cannot find the column.
I am sure that the spelling is correct (no spaces).
Anyone knows how to fix this ?
Thanks!
Tharka
FYI here is the full code :
Here is my issue:
I wrote a code on Excel to update an Access Table. I am using a ADODB recordset. I want to update the value of certain columns (Fields). These columns are variables.
I know the code to update a hard coded column is :
Code:
adoRecSet!ColumnName = "Test"
But I need to update based on a variable column.
I tried this :
Code:
adoRecSet! & item2 = "Test"
Code:
adoRecSet("adoRecSet!" & item2).Value = "Test"
But none of these work, it returns a run-time error 3265 which means it cannot find the column.
I am sure that the spelling is correct (no spaces).
Anyone knows how to fix this ?
Thanks!
Tharka
FYI here is the full code :
Code:
Dim strMyPath As String, strDBName As String, strDB As String, strSQL As StringDim i As Long, n As Long, lFieldCount As Long, k As Long
Dim rng As Range
Dim MyDeal As String
Dim NRange As Name
Dim arr(1 To 1000) As String
Dim item As Variant
Dim item2 As Variant
'instantiate an ADO object using Dim with the New keyword:
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
'--------------
'THE CONNECTION OBJECT
strDBName = "Database.accdb"
strMyPath = "C:\Users\JDoe\Documents"
strDB = strMyPath & "\" & strDBName
Set connDB = New ADODB.Connection
Set adoRecSet = New ADODB.Recordset
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB
Dim ws As Worksheet
'set the worksheet:
Set ws = ActiveWorkbook.Sheets("Sheet2")
'Set the ADO Recordset object:
Set adoRecSet = New ADODB.Recordset
'Opening the table :
strTable = "Profil"
'--------------
'COPY RECORDS FROM ALL FIELDS OF A RECORDSET:
'refer Image 9d to view records copied to Excel worksheet
adoRecSet.Open Source:=strTable, ActiveConnection:=connDB, CursorType:=adOpenStatic, LockType:=adLockOptimistic
lFieldCount = adoRecSet.Fields.Count
MyDeal = ws.Range("A1").Value
For Each NRange In ActiveWorkbook.Names
M = M + 1
arr(M) = NRange.Name
Next NRange
adoRecSet.Filter = adoRecSet.Fields(0).Name & " = '" & MyDeal & "'"
If adoRecSet.EOF Then
adoRecSet.AddNew
For Each item In arr
adoRecSet("adoRecSet!" & item).Value = ws.Range(item).Value
Next item
Else
For Each item2 In arr
adoRecSet("adoRecSet!" & item2).Value = ws.Range(item2).Value
Next item2
End If
adoRecSet.Update
adoRecSet.Close
'close the objects
connDB.Close
'destroy the variables
Set adoRecSet = Nothing
Set connDB = Nothing
Last edited by a moderator: