Amend a record VIA VBA to Access

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
769
Office Version
  1. 365
Platform
  1. Windows
Good Afternoon

I am trying to update a record in my daabase from Excel.
I am new at attempting to update Access from Excel and am having an error "Method 'Range' of object' _Global' failed"
at the ****** point below. I am unsure if I am using the correct request ie find

I am trying to find the reference in cell ref "NoToCheck" in column "Ref No" in my database. As I have copied the code from the internet I am unsure of the reference of "Primary Key in the code, but the primary key is in the "Ref No".

I hope this is understandable.
Thanks in advance

Rich (BB code):
Sub AmendAccessRecord()

Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stDB As String
Dim stCon, NoToCheck As String

NoToCheck = Worksheets("Today").Range("F13").Value

Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
 
 'Pathway and name of the database
stDB = "mydestination.accdb; Jet OLEDB:Database;"
 
 'Create the connectionstring.
stCon = "Provider=Microsoft.Ace.OLEDB.12.0; Persist Security Info = False;" & _
"Data Source=" & stDB & ";"
 
cnt.Open stCon

'Find and Update the record in Access
With rst
    .Index = "PrimaryKey"
    .CursorLocation = adUseServer
    .Open "ProcessesRaised", cnt, 1, 3, adCmdTableDirect
    .Seek Range("NoToCheck").Value '*********
    If Not rst.EOF Then
        .Fields("Due Date") = Sheets("today").Range("D16").Value
        .Fields("Status") = Sheets("Today").Range("M16").Value
        .Fields("Worked By") = Sheets("Advocate Data").Range("K5").Value
        .Fields("Description") = Sheets("Today").Range("I16").Value
    .Update
    Else
    MsgBox "Update Failed"
    End If
End With

rst.Close
cnt.Close
Set cnt = Nothing
Set rst = Nothing

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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