find and update access record using excel vba

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
I'm successfully writing values to an access DB table. At different points in my Excel VBA project, I may need to refer back to records stored in the access database for change from excel vba. I've been playing with the syntax, but can't seem to make a go of it. I'll post what I've got thus far.

I want to search the "TransactionSorting" table, column field "bins" for a value of 137. IF found, I would like to remove\clear the value.

Code:
Sub mrexcel()
    Set cont = Sheets("controls")
    strig = cont.Range("p15").Value & cont.Range("p16").Value 'file path and filename
    On Error Resume Next
    Set db = OpenDatabase(strig)
    Set rs = db.OpenRecordset("TransactionsSorting", dbOpenTable)
    i = "137" 'later to be supplied with variable
        With rs
            rs.MoveFirst
            While Not rs.EOF
                If rs.Fields("bins").Value = i Then
                    rs.Fields("bins").Value = ""
                    rs.Update
                    rs.MoveNext
                Else:
                    rs.MoveNext
                End If
            Wend
        End With

    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Sounds like just an UPDATE query. No looping. Just a one off query. [Untested] SQL like
Code:
UPDATE TransactionsSorting
SET bins Is Null
WHERE bins = 137
No need to open the database either. Google should find examples.
 
Upvote 0
like
Code:
  Set objRS = CreateObject("ADODB.Recordset") 'late binding
  objRS.Open strSQL, strConnection
  Set objRS = Nothing
 
Upvote 0
I'll give that a try. I found that if I substituted the "" for a value like "15", my procedure would work and rest any numbers of 137 in the bins field. So istead of using "" I should use null?
 
Upvote 0

Forum statistics

Threads
1,203,326
Messages
6,054,747
Members
444,748
Latest member
knowak87

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