Creating a table from an Access Query

C

Cyberwolf

How can I go about creating a table in a Word Document at a specific place
from a query. The number of rows will vary based on how many records were
input that matches that specific queries criteria. I would imagine the
bookmark in Word would come into play in the Word document, but I have been
unsuccessful in creating separate rows in the table. The columns would not
change.

Thanks,
 
D

Doug Robbins

Use something like the following:

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim i As Long, j As Long, col As Long
'Open a database
Set myDataBase = OpenDatabase("C:\Procurement\Procurement Plan.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Motors", dbOpenForwardOnly)

' Get the number of Fields in the Query
j = myActiveRecord.Fields.Count - 1
' Start inserting data in Row 2 of the table
i = 2
'Loop through all the records in the table until the end-of-file marker is
reached
Do While Not myActiveRecord.EOF
' Insert the data into the cells
For col = 0 To j
ActiveDocument.Tables(1).Cell(i, col + 1).Range.InsertAfter
myActiveRecord.Fields(col)
Next col
i = i + 1
ActiveDocument.Tables(1).Rows.Add
'access the next record
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close
myDataBase.Close

You will need a reference to the DAO object library (Tools>References in the
VBE)

--
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
 

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