Datasheet

Discovering Code as You Go
590
Cutting and pasting code
Many VBA programmers post examples of code they’ve written on Web
pages. When you come across some sample code you want to incorporate
into your own database, retyping it all into the Visual Basic Editor is not nec-
essary. Instead, just use standard Windows cut-and-paste techniques to copy
the code from the Web page into the Visual Basic Editor.
Say you come across some code in a Web page you want to use in your own
database. Here’s the sequence:
1. In the Web page, you drag the mouse pointer through the code you
want to copy to select that code. Then press Ctrl+C to copy that
selected code to the Windows Clipboard.
2. Back in Access, create a new module or open an existing module in
which you want to place the code.
3. In the Code window, click at the position where you want to put the
copied code. Then paste the code to the cursor position by pressing
Ctrl+V.
Bear in mind, however, that just pasting code into the Code window doesn’t
make the code do anything. Most code examples are based on a sample
database. Just dropping the example into your database may not be enough
to get it to work.
When you copy and paste from a Web page, you might get some HTML tags,
weird characters, weird spacing, and so forth. If that happens, you can copy
the code from the page and paste it into a simple text editor like Notepad first.
That should get rid of any unusual tags and characters. Then copy and paste
the text from the Notepad document into the VBA Editor’s Code window.
But even if you do find an example that’s generic enough to work in any data-
base, the code won’t actually do anything until some event in your database
triggers it into action. We’ll look at the many ways you can trigger code into
action in the next chapter.
38_036494 bk08ch01.qxp 11/17/06 8:34 AM Page 590