PDA

View Full Version : Solved: Open Access Database by VBA



MichaelS
06-14-2006, 08:47 AM
Can somebody help?

I want to open an existing database from another database by VBA.
I can successfully open a word document also an excel workbook by using the following code (Got from http://www.databasedev.co.uk/forms.html), however I have tried using the same format for opening a database and have failed miserably.

Code to open Word document

Sub OpenWordDoc(strDocName As String)
Dim objApp As Object

'Opens the document

Set objApp = CreateObject("Word.Application")
objApp.Visible = True
objApp.Documents.Open strDocName
End Sub

Code to open Excel document adapted from above code

Sub OpenExcelxls(strDocName As String)
Dim objApp As Object

'Opens the document

Set objApp = CreateObject("Excel.Application")
objApp.Visible = True
objApp.Workbooks.Open strDocName
End Sub

Thanks in anticipation

Mike

Norie
06-14-2006, 09:47 AM
Mike

What did you try for the Access database?

MichaelS
06-15-2006, 03:28 AM
Norrie

Thanks for your time. the code I have tried to open my database is as below.

Sub OpenDatabaseX()
Dim objApp As Object
strDocName = ("C:\DatabaseX.mdb")

Set objApp = CreateObject("Access.Application")
objApp.Visible = True
objApp.Databases.Open strDocName

End Sub

It works to the point of opening a new blank access screen but fails to open DatabaseX. At this point I close the new screen which reveals Run time Error 438, "Doesn't support this property or method", when press debug the line obj.Databases.Open strDocName is highlighted (Maybe the word Databases in this line is wrong I had to mess with this in my OpenExcel code before it would work).

Mike

stanl
06-15-2006, 06:29 AM
I generally use MDAC, but isn't it something like

objApp.OpenCurrentDatabase(strDocName)

Stan

MichaelS
06-15-2006, 07:01 AM
stanl

That did the trick, many thanks:clap:

Mike