Share via

Creating an Access database from within Excel

Anonymous
2013-03-29T16:00:11+00:00

I have the following code:

Sub CreateAccessDatabase()

    Dim accessApp As Object

    Dim dbs As Object

    On Error Resume Next

    Set accessApp = GetObject(, "Access.Application")

    If accessApp Is Nothing Then Set accessApp = CreateObject("Access.Application")

    If accessApp Is Nothing Then Exit Sub

    On Error GoTo 0

    accessApp.DBEngine.CreateDatabase "TempData.accdb", accessApp.dbLangGeneral

    Set dbs = accessApp.CurrentDb

    MsgBox dbs.Name ' Used to verify that the database was actually created

    accessApp.Quit

    Set dbs = Nothing

    Set accessApp = Nothing

End Sub

I receive the following error message when the accessApp.DBEngine.CreateDatabase... line is executed:

    Run-time error 438: Object doesn't support this property or method.

Can someone please tell me how to fix the aforementioned line so that it successfully executes?  Thanks in advance for any assistance.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2013-03-29T17:17:21+00:00

You would have to create an Access.Application object the way you originally did.

Then open the database that you created, using

accessApp.OpenCurrentDatabase ...

You can then use Access commands, prefixing them all with accessApp:

accessApp.DoCmd.TransferSpreadsheet ...

Was this answer helpful?

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2013-03-29T16:21:13+00:00

CreateDatabase is a DAO method, not an Access method.

Try

Sub CreateAccessDatabase()

    Dim DAO As Object

    Dim dbs As Object

    Set DAO = CreateObject("DAO.DBEngine.120")

    Set dbs = DAO.CreateDatabase("TempData.accdb", ";LANGID=0x0409;CP=1252;COUNTRY=0")

    MsgBox dbs.Name

    Set DAO = Nothing

End Sub

The second argument of CreateDatabase is the value of dbLangGeneral; we can't use the symbolic constant since we're using late binding.

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-03-29T17:33:43+00:00

    Thanks a million!

    After you pointed out that CreateDatabase is a DAO method and not an Access method, I was unsure if I could still utilize the Access.Application object that I had created.

    As always, I greatly appreciate your help.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-03-29T16:51:59+00:00

    CreateDatabase is a DAO method, not an Access method.

    Ah, I didn't know that.  Your solution worked perfectly.  Thanks!

    If I may ask one follow-up question:  Since your code does not create the accessApp object, how would I incorporate methods like DoCmd.TransferSpreadsheet and Execute in your code?

    Thanks again.

    Was this answer helpful?

    0 comments No comments