Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Newsgroup Archive
Word Newsgroups
Word Mail Merge
How to import certain fields from access database?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Doug Robbins - Word MVP, post: 5611486"] You should look at the links in my initial response to this thread. Here are a couple of routines. The first imports data from a database into a Word document and the second populates a listbox (or it could be a combobox) on a userform with data from a table in an Access database Dim myDataBase As Database Dim myActiveRecord As Recordset Dim i As Long Dim dtable As Table, drow As Row 'Open a database Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb") 'Access the first record from a particular table Set myActiveRecord = myDataBase.OpenRecordset("Currencies", dbOpenForwardOnly) 'Add a table to the document with one row and as many fields as there are in the database table Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, numcolumns:=myActiveRecord.Fields.Count) Set drow = dtable.Rows(1) 'Loop through all the records in the table until the end-of-file marker is reached Do While Not myActiveRecord.EOF 'Populate the cells in the Word table with the data from the current record For i = 1 To myActiveRecord.Fields.Count drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1) Next i 'Add a new row to the Word table and access the next record Set drow = dtable.Rows.Add myActiveRecord.MoveNext Loop 'The last row will be empty, so delete it drow.Delete 'Then close the database myActiveRecord.Close myDataBase.Close 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 ListBox1.ColumnCount = rs.Fields.Count ' Load the ListBox with the retrieved records ListBox1.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 [/QUOTE]
Verification
Post reply
Forums
Archive
Newsgroup Archive
Word Newsgroups
Word Mail Merge
How to import certain fields from access database?
Top