Opening Access Database from Word

L

LDMueller

Hello,

I have Word 2003 and Access 2003. I'm trying to write VBA code to open a
database from within Word.

Can anyone explain why this code works (see below) ...
Sub OpenDatabase()
' Opens the Northwind database
Dim MyAccess As Object
Set MyAccess = GetObject("C:\Program Files\Microsoft
Office\OFFICE11\SAMPLES\Northwind.mdb", "Access.Application")
End Sub

And this code does not work (see below) ...
Sub OpenDatabase()
' Opens the MyDatabase database
Dim MyAccess As Object
Set MyAccess = GetObject("C:\Program Files\Microsoft
Office\OFFICE11\SAMPLES\MyDatabase.mdb", "Access.Application")
End Sub

My database named MyDatabase.mdb resides in the same path as the
Northwind.mdb database so I can't figure out why this code won't work. Any
assistance would be greatly appreciated.
 
A

aidan.heritage

Corrupt access database? But I'm not sure why you want to use
GetObject - more efficient would be to get the data you need and
manipulate it through code - though you haven't specified what you
want to do with it so cannot be much more specific
 
L

LDMueller

First of all, thank you for responding to my request.

The database isn't corrupt. I can create a new one with the same results.

I'm not sure why I'm using GetObject. I don't program much in VBA, but my
hope is to be able to write a Word 2003 macro which I'll then put on a
toolbar that will open my database. I have an Access 2003 database which
contains information I want my users to be able to easily get to while
they're typing their Word documents.
 
A

aidan.heritage

I would do it by getting the information they need I think - but is the
intention just to run the database - if so AppActivate or Shell may be
better ways forward - the method I would use to get SPECIFIC data would
be

Dim WrkJet As Workspace
Dim dbsdb As Database
Dim MySQL as string
Dim dbsPubs As Recordset
Set WrkJet = CreateWorkspace("", "admin", "", dbUseJet)

Set dbsdb = WrkJet.OpenDatabase("C:\Program Files\Microsoft
office\OFFICE11\SAMPLES\MyDatabase.mdb")
MySQL = "Whatever valid query you want to run"
Set dbsPubs = dbsdb.OpenRecordset(MySQL)
If Not dbsPubs.BOF Then
'have data you can reference
end if

Set dbsPubs = Nothing
Set dbsdb = Nothing
Set WrkJet = Nothing
 
L

LDMueller

This was exactly what I needed. I used the AppActivate after I looked up how
to use it in the online help. This opened my database and once it opened, my
popup within the database took them where they needed to go.

Thank you so much! You really helped me a great deal and I sincerely
appreciate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top