Compile error in VBA for Access- User defined type not defined
Hi I'm new to VBA and I'm just trying to create a connection object to the database and also a recordset object in order to add new records to the database. I am directly connected to the database and everytime I compile the following piece of code which is inside a Module it gives me a compile error for Dim dbCurrent As Database and dbMichael As Database. How do I get rid of it. Here is the code:
Option Compare Database
Option Explicit
Function AddPlayer()
Dim dbCurrent As Database
Dim rstPlayers As Recordset
Dim rstCards As Recordset
Dim dbMichael As Database
Set dbCurrent = DBEngine.Workspaces(0).Databases(0)
'This will create a table-type recordset.
Set rstPlayers = dbMichael.OpenRecordset("Players", "databases\Michael.mdb", DB_OPEN_DYNASET)
'These will create a dynaset-type recordset.
Set rstCards = dbMichael.OpenRecordset("Cards", "databases\Michael.mdb", DB_OPEN_DYNASET)
rstPlayers.Move 4
End Function
Hopefully somebody can help- thanks in advance :sick: :sick:
Re: Compile error in VBA for Access- User defined type not defined
Have you added a reference for ADO to your project, if not then you need to !
Re: Compile error in VBA for Access- User defined type not defined
I thought that was just for Vb, I'm a bit confused between the two. Haven't done VBA before. I thought I was already connected to the database as the textboxes automatically filled up with records when I attached them to the table recordsource. So do I add an ADO object at design time and then create the connection through the Jet Engine?
Re: Compile error in VBA for Access- User defined type not defined
Since this is VBA inside an Access database you should have the reference for DAO
- Microsoft DAO x.x Object Library. If not then add it. You can explicitly
dimention your objects like so.
VB Code:
Option Compare Database
Option Explicit
Function AddPlayer()
Dim dbCurrent As DAO.Database
Dim rstPlayers As DAO.Recordset
Dim rstCards As DAO.Recordset
Dim dbMichael As DAO.Database
Set dbCurrent = DBEngine.Workspaces(0).Databases(0)
'This will create a table-type recordset.
Set rstPlayers = dbMichael.OpenRecordset("Players", "databases\Michael.mdb", DB_OPEN_DYNASET)
'These will create a dynaset-type recordset.
Set rstCards = dbMichael.OpenRecordset("Cards", "databases\Michael.mdb", DB_OPEN_DYNASET)
rstPlayers.Move 4
End Function