I’m using DAO (been asked not to use ADO.NET) to update an Access database. I’m currently thus far, however, VB2008 is telling me that the variable “daoengine” is not declared before it is used. What am I doing wrong in the following code?
Function update_db()
Dim daoengine As DAO.DBEngine
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
dbs = daoengine.OpenDatabase("Project.mdb")
rst = dbs.OpenRecordset("Log", dao.RecordsetTypeEnum.dbOpenDynaset)
End Function
When you say
you’re just creating a variable,
daoengine, but it doesn’t actually point to anything. You need to do this afterwards:You can also type
Dim daoengine As New DAO.DBEngine, but it’s better to do it in the two lines above. If you put theNewin theDimline, you create what’s called an “auto-instancing” variable which can magically come to life again after you thought you’d disposed of it.For more details see the Don’t Use Auto-Instancing Object Variables here: Declaring Variables (in VBA)