1. Home
  2. Computing & Technology
  3. Visual Basic
VBA - Accessing Access
Part 5: The VBA Code
 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 6: The VBA Code
 
 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)

 

Now we do some code! Make sure the form is in design view and click the Toolbox in the main menu to display it and draw a command button on the form. Access assumes that the only reason you would put a command button on a form is to perform some standard action so Access automatically starts a wizard. Although we're not going to use the wizard generated code "as is", this is as close as Access comes to providing a Record New Macro function so select the Record Operations category and Add New Record action and click the Finish button. If you open the Visual Basic editor now, you will see that Access has generated a subroutine in the form that is mostly error handling, but has one other line of code:

DoCmd.GoToRecord , , acNewRec

The dot suggests that DoCmd is an object and GoToRecord is a method. acNewRec is an argument that further defines what the GoToRecord method does. This type of coding goes back to Access 95 and it's still used in Access 2002. So we're going to "piggyback" on it and just let it do what it does.

This is the main area where experienced Access programmers will object that there are more advanced ways to do it (and they're right). More advanced applications will declare database, query definition, recordset, connection, and other types of objects that make up the different access methods available. After you're comfortable with VBA, I recommend that you learn about these techniques and use them.

With our 'database' statement written for us, it's time to write the code to bring in our Word data. I created a Word document with a verse from the Lewis Carroll poem (available in the download). The code to retrieve the lines of the verse are quite similar to what we have done before.

Private Sub Command1_Click()
  Dim oApp As Word.Application
  Dim oWord As Word.Document

  Set oApp = _
    CreateObject("Word.Application")
  Set oWord = _
    oApp.Documents.Open( _
    FileName:="{file path}\aboutacw.doc")
  oApp.Visible = True

Unlike the previous Word examples, in this one, the entire Word application opens and the document is visible using the statement:

oApp.Visible = True

By sizing the Word window and single stepping through the VBA code in Excel, you can see the effect of the next statements while you run the program. Here's the code to copy the lines of the verse into rows in the Access database:

oApp.Selection.HomeKey _
    Unit:=wdStory
    oApp.Selection.EndKey _
      Unit:=wdLine, Extend:=wdExtend
    DoCmd.GoToRecord , , acNewRec
  Do Until oApp.Selection.End + 1 >= _
    oApp.ActiveDocument.Content.StoryLength
    Me.AboutText = oApp.Selection.Text
    DoCmd.GoToRecord , , acNewRec
    oApp.Selection.MoveDown Unit:=wdLine, _
      Count:=1
    oApp.Selection.HomeKey _
      Unit:=wdLine
    oApp.Selection.EndKey _
      Unit:=wdLine, Extend:=wdExtend
  Loop

Exit_WordDoc:
  Me.AboutText = oApp.Selection.Text
  DoCmd.GoToRecord , , acNewRec

Notice that the DoCmd.GoToRecord statement created by the Access wizard is now buried in code that reads the Word document. The statement that copies the Word text to the Access form textbox is:

Me.AboutText = Selection.Text

Next page > Finishing the Program > Page 1, 2, 3, 4, 5, 6
Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.