The Database Journal
03/10/2006
Adjusting Autonumber Increments

03/10/2006
Moving Access Data to Microsoft Excel

03/10/2006
Exporting Access Data with TransferSpreadsheet

03/10/2006
Export Access Data with VBA

03/13/2006
Data Layout in Microsoft Excel

03/17/2006
Excel Text to Number Conversion

03/21/2006
Transposing Excel Ranges

03/25/2006
Access VBA Timer Event

03/29/2006
What is MySQL?

04/02/2006
Microsoft Access Parameter Query Tutorial Video

04/06/2006
How does PHP Work?

04/10/2006
Microsoft Access VBA Tools - References

Click here to view all articles.

Database Design
Website Design
Search Engine Marketing
Home
Contact Us
About Us
Database Journal
Newsletters
Database Newsletter

Export Access Data with VBA

In this example, we’re going to have a look at exporting some fields to Microsoft Word by using VBA Code. Brace yourself, this gets a little strange!

We’re going to go ahead and create our very own procedure in order to loop through records in a table and move them into Microsoft Word.

Why would you do this?

Great question – probably if you either have too much time on your hands, or feel like creating more work for yourself in order to avoid washing your Manager’s car!

Seriously, there are quite a few reasons. One of the reasons I’ve used this method is to export text from a database but change the formatting. For instance, place it into a Word document with different line breaks or tab characters in order to format it for a specific purpose. This is really only scratching the surface of why you would do such a thing, but it’s a handy trick to have in your arsenal.

At this point, I’m going to assume you’re comfortable enough working in VBA code to make a few changes comfortably. The first step in this process is to create a new module (save it as bas_WordOpen or some such name), and paste the following code in the module:

Option Compare Database
Public WordApp As Word.Application
Public doc As Word.Document
Public sel As Word.Selection

Public Sub WordEx()

Set WordApp = New Word.Application
WordApp.Documents.Add
Set doc = WordApp.ActiveDocument
Set sel = WordApp.Selection
WordApp.Visible = True

End Sub

Before you run this code, you’ll have to click on Tools, then References, and enable the Microsoft Word Object Library. For XP, you’ll be enabling Microsoft Word 10.0 Object Library. We can’t have Access and Word chatting in different languages!

As it stands right now, this procedure will work on it’s own without any problems. Of course, all it will do is open up a Word document!

The next step is to create the procedure that will run through records in a table or query, and spit them out into Word in whatever format we desire. Lucky for you, I’ve created the code and you just have to copy and paste it into a new module (or combine it with the existing one – that’s all up to you):

Option Compare Database

Public Sub WordXPort()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld As Field

Set db = CurrentDb()
Set rs = db.OpenRecordset("qsel_Test")
Set fld = rs.Fields(0)

WordEx

Do Until rs.EOF

sel.TypeText Text:="ENTRY - " & fld.Value & vbCrLf & vbCrLf

rs.MoveNext

Loop

End Sub

Basically, this code sets up whatever recordset you want (in this case, I’m picking on “qsel_Test”), starts up word (that’s the “WordEx” procedure getting called – the first procedure we created), then loops through all of the records, putting the phrase, “ENTRY – “ in front of each line and giving us the value in the first field (that’s the “Fields(0)” reference you see).

The Word Document output would look something like this:

ENTRY – First Field Value

ENTRY – Second Field Value

ENTRY – Third Field Value

You can replace “ENTRY” with whatever you want – or add tabs, more line feed characters (The “vbCrLf” that you see), or any change you desire. This is extremely flexible, as most code solutions are, and can be changed, adjusted, enhanced and completely overhauled to get you your desired results!


©2010 Blue Moose Technology, LLC

David Badurina, President of Blue Moose Technology, LLC, is a relational database design expert. David's unique ability to easily explain virtually any technical concept has allowed him to work with companies such as AMD, Motorola, the American Heart Association, and countless small businesses. Learn more about database design right now at http://www.bluemoosetech.com.

Article Publishing and Re-Print Guidelines

You can easily link to this article!

If you'd like to link to this article from your own website, copy and paste the following HTML code onto your page:

On your page, the code will appear like this:

Export Access Data with VBA