Run-time error 3265: error when updating an access table

Tharka

New Member
Joined
Nov 12, 2015
Messages
10
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 :
Code:
 adoRecSet!ColumnName = "Test"
and it works when i test it.

But I need to update based on a variable column.
I tried this :
Code:
adoRecSet! & item2 = "Test"
and this :
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:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I could be wrong but I think this doesn't change the value of a column's name, it only changes the value of the data stored in that row for that column.
adoRecSet!ColumnName = "Test"

Generally ADO is for updating the values of fields and not for changing the definition of fields. To put it another way, the names of the columns in the table should generally be treated as read-only.
 
Last edited:
Upvote 0
Hi and thanks for your answer!

I am actually trying to update the values of the table. I don't want to update the column name.

Sorry for the confusion.
 
Last edited:
Upvote 0
It should be:

Code:
adoRecSet(CStr(item2)).Value = "Test"

If item2 is never numeric, you don't need the CStr.
 
Upvote 0
if you know the column index I think you can just do
adoRecSet(0).Value = "Test"
adoRecSet(1).Value = 23
adoRecSet(2).Value = "M"
 
Upvote 0
Thanks a lot for your anwers, it helped.

For those interested, here is the 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"


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
    adoRecSet!Nom = MyDeal
    adoRecSet.Filter = adoRecSet.Fields(0).Name & " = '" & MyDeal & "'"
    For i = 1 To lFieldCount - 1
        For Each item In arr
            If adoRecSet.Fields(i).Name = item Then adoRecSet.Fields(i).Value = ws.Range(item).Value
        Next item
    Next i
Else
    For i = 1 To lFieldCount - 1
        For Each item2 In arr
            If adoRecSet.Fields(i).Name = item2 Then adoRecSet.Fields(i).Value = ws.Range(item2).Value
        Next item2
    Next i
End If


adoRecSet.Update
adoRecSet.Close


'close the objects
connDB.Close


'destroy the variables
Set adoRecSet = Nothing
Set connDB = Nothing
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top