| VBA - Accessing Access | |||||||||||||||||||||||||
| Part 5: The VBA Code | |||||||||||||||||||||||||
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.
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 |
|||||||||||||||||||||||||

