Updating Access from Excel

peterhw

New Member
Joined
Jan 23, 2012
Messages
39
I have followed several links here and elsewhere and just can't figure out what I am doing wrong!
I select items from a listbox display and then wish to update (2 lines for my testing).
The first iteration appears to go through all steps when in debug mode BUT does not actually update the database. The second iteration FAILS at the update command and goes to ErrHandler:. ID is a unique record ID in Access and Trans_Comment is a valid field name. The records have not been updated or deleted
The error is
[TABLE="width: 282"]
<tbody>[TR]
[TD="width: 282"]3021:Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.[/TD]
[/TR]
</tbody>[/TABLE]

I have had something similar working previously then made changes.
Code:
Sub Category_update()
Dim int1 As Integer, Item_ID As Integer
Dim str0 As String, str1 As String, str2 As String, str3 As String, str4 As String, mycomment As String
On Error GoTo ErrHandler
    str1 = DBtable
    Connect2Access_Init
    For int1 = 1 To ListBox2.ListCount - 1
        If ListBox2.Selected(int1) = True Then
            str1 = ListBox2.List(int1, 4)
            MsgBox ("List Item ID = " & str1 & " is selected to update")
            mycomment = "Utilities Energy"                                  '   hard coded for tesTing
            Set rs = New ADODB.Recordset
            mycmd = "SELECT * FROM " + DBtable + " WHERE ID = " + str1 + ";"
            With rs
                .Open Source:=mycmd, ActiveConnection:=cn, LockType:=adLockOptimistic
                .Fields("Trans_Comment") = mycomment
                .Update
                .Close
            End With
        End If
    Next int1
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    Exit Sub
ErrHandler:
    Error_Handler ("Category_update")
End Sub

any help appreciated
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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