Import data from access into a table

J

JKarchner

Ok i would like to import data from an access database table into a table in
a microsoft word file. The table is already created and i would to just
insert the data into that table. Here is the tricky part: each record
should have a new table. The table created is a template for how all the
tables should look. Is there a way to to have it so that a new table is
copied from the template and the data is inserted into it. Or will i have to
create a table for each record on my own. I was given some sample code to
use with it and would like to know if it is correct, and also some tips to go
about using it. The source of my code was not sure if the code for the table
was correct, and also i would assume that cell(#,#) is (row,column).

Sub GetDataFromDataBase()

'allocate memory for the database object as a whole and for the active record
Dim myDataBase As Database
Dim myActiveRecord As Recordset

'Open a database
Set myDataBase = OpenDatabase("C:\DataBaseFolder\Database.mdb")

'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Table_1", dbOpenForwardOnly)

'Loop through all the records in the table until the end-of-file marker is
reached

Do While Not myActiveRecord.EOF

'If field #1 contains a non-zero value,
'insert the value of field #2 into the document
'after the current cursor or selection location

If myActiveRecord.Fields("Field_1") <> 0 Then

'this will put the data after the cursor, you have to set it up so that
it goes in the correct field in your table, if you want it in a table, i
believe it's selection.tables(#).cell(#,#)

selection.tables(#).cell(#,#) myActiveRecord.Fields("Field_2")

End If

'access the next record
myActiveRecord.MoveNext

Loop

'Then close the database
myActiveRecord.Close
myDataBase.Close

End Sub

Thank you for any help that you can give.
 
D

Doug Robbins - Word MVP

Why not just use a catalog, or in Word XP and later, it is called a
directory, mail merge? If you put two paragraph returns after your table,
each record will end up in a separate table with each table being a replica
of the one in the main document.

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

JKarchner

thank you, but could you explain it a little better. i have never done
anything of this type before. thanks again.
 
D

Doug Robbins - Word MVP

Probably best to select Toolbars from the View menu and then check the Mail
Merge item to display the Mail merge toolbar.

Then use a document created from your "template" (assuming that it is a .dot
file), which I assume has the table already created in it and click on the
first button on the Mail merge toolbar and select Directory as the type of
merge that you want to perform. Then click on the second button to select
the data source which will be the table in your Access database. Now, using
the Insert Merge Field button, insert the required fields from your data
base into the cells of the table. Make sure that there are two paragraph
returns after the table and then execute the merge to a new document using
the button on the right hand side of the Mail merge toolbar.

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