Home » excel » VBA Excel – UPDATE record in ACCESS

VBA Excel – UPDATE record in ACCESS

Posted by: admin April 23, 2020 Leave a comment

Questions:

I got a problem.

I want to update an existing record in table in Access Database using VBA in EXCEL.

My code:

sqlik = "UPDATE query which works in access"

Set ZAP_QUERY2 = baza.CreateQueryDef("", sqlik)

With ZAP_QUERY2

    ![abc] = Edit_Form.abc.Text
    ![bcd] = Edit_Form.bcd.Text
    ![cde] = Edit_Form.cde.Text

End With

When I used sqlik = “SELECT query which works in access” it didn’t make any changes. But with sqlik = “UPDATE query which works in access” it says that “Cannot find any object in this collection”

Any ideas?

Thanks for your help

How to&Answers:

A solution using DAO:

...
dim db as DAO.Database, rec as Dao.Recordset
dim strSQL as String
Set db = OpenDatabase("c:\DatabaseFolder\YourDatabase.accdb")
strSQL = "SELECT * FROM yourTable WHERE yourField=1"
Set rec = db.OpenRecordset(strSQL, dbOpenDynaset, dbEditAdd)
With rec
    .MoveFirst
    ![aField] = Edit_Form.abc.Text
    .Update
End With
rec.Close
db.Close
...

Hope this helps you