Open Access Database from Word Macro

L

LDMueller

I want to open an MS Access 2003 database from Word 2003 using a macro which
I'll eventually put on a toolbar.

My code is as follows:
Public Sub OpenAccess()
AA = GetObject("C:\Program Files\Microsoft
Office\OFFICE11\SAMPLES\Northwind.mdb", "Access.Application")
End Sub

Unfortunately, I get a MS Visual Basic Run-time error '438': Object doesn't
support this property or method.

Everything else I've tried doesn't work either.

Does anyone have a suggestion.

Thanks!
 
J

Jezebel

Are you trying to open the database itself, or fire up the Access
application?

The basic error is that you're missing "set", and the variable
declaration --

Dim AA as object
Set AA = ...

But that will get you a reference to the Access application, which is not
what you want if your aim is to read/write data from the database.
 
L

LDMueller

I'm trying to open the database itself. Although in my example I listed the
Northwind.mdb database, my real code will use a database that houses address
so my users can lookup an address and other client information.
 
J

Jezebel

Add a reference to the database engine you're planning to use (DAO or ADO),
then use its methods to open the database. And check the MS site for the
MDAC documentation. Note that this has nothing whatever to do with Access.
Standard format databases (like the Northwind sample) are commonly -- but
mistakenly -- referred to as "Access databases", only because Access uses
this format also.
 
D

Doug Robbins - Word MVP

The following code in the initialize event of a userform will load a listbox
on the form with data from a table in an access database.

Private Sub UserForm_Initialize()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
' Open the database
Set db = OpenDatabase("D:\Access\ResidencesXP.mdb")
' Retrieve the recordset
Set rs = db.OpenRecordset("SELECT * FROM Owners")
' Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
' Set the number of Columns = number of Fields in recordset
LstOwners.ColumnCount = rs.Fields.Count
' Load the ListBox with the retrieved records
LstOwners.Column = rs.GetRows(NoOfRecords)
' Cleanup
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

LDMueller

I created an ActiveX Data Object (ADO) and added a Reference to the
appropriate object library item and selected Microsoft ActiveX Data Objects
2.8 Library, although I wasn't sure what version I should be selecting.

I'm really not sure what to do from here. I looked at the MDAC
documentation, but it was a little over my head.
 

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