You are here:About>Computing & Technology>Visual Basic
About.comVisual Basic
VBA - Accessing Access
Part 6: Finishing the Program
 More of this Feature
• Part 1: VBA - Accessing Access
• Part 2: VBA in Access
• Part 3: Coding the Access Example
• Part 4: The Access Form: Our VBA Container
• Part 5: VBA Code Calling the Excel Server
 
 Join the Discussion
Have you created a system using VBA Automation ?
Tell us about it!
 
 Related Resources
• Beginning Visual Basic
• Visual Basic 6
 
 Elsewhere on the Web
• Updates from Microsoft
(Browse to Office 2000)

 

The last two lines, after the end of the document are necessary due to the way the Word cursor movement statement Selection.MoveDown works. (It's worth mentioning that the meaning of the word "cursor" is very different in Access and Word. It's a good example of how you have to be aware of the way a hosting environment works to use VBA successfully.) One of the best ways to discover exactly how to code programs like this is to trace through the code as you develop it using the Debug tool. Single step through the program and display the results in the Immediate window of the Visual Basic editor.

oApp.ActiveDocument.Close
oApp.Quit
Set oWord = Nothing
Set oApp = Nothing
Exit Sub

The end of the program releases resources and closes Word. Although some texts say this is an optional clean up step, in this case, it's more than that. Without it, running the program twice will result in an error because Access VBA doesn't release the reference to the Word objects and this interferes with the automation on the second execution.

This is also the reason that all of the statements that reference the Word object are fully qualified with the object name oApp. The program will run without the qualification because VBA searches it's reference objects for the right one and uses it. But it doesn't release the reference if it's not fully qualified at the end of the program. As a debugging note, Microsoft provides an excellent database of this type of problem at MSDN.Microsoft.com. This particular problem is documented here.

The end result of executing the program updates the table like this. You can download the Access database (which includes the VBA code) and the Word Document here.

The Final Result

Return to the first page > VBA - Accessing Access > Page 1, 2, 3, 4, 5, 6
From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Newsletters & RSSEmail to a friendSubmit to Digg
 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.