contents.gifindex.gifprev1.gifnext1.gif

To write an Access macro

In Access, to write and run a macro containing ODBC calls you execute the following steps. Specific versions of Access may vary slightly.

1. Create a new macro module.

Create a new module for your Access macro. To do this, click the Module tab and click "New." Then type the AccessBasic source code for the macro. Save the module using the File menu "Save" command.

2. Create a macro.

Create a macro to execute a particular function. To do this, select "New Macro" from the File menu, select "RunCode" from the resulting Action list box, then type the function name into the Function Name entry field. Save the macro using the File menu "Save" command.

3. Execute the Macro.

There are a few different ways to invoke macros from Access. Most commonly, you click the "Run" button (the one with an exclamation mark on it). But if you want the macros to invoke automatically when Access is activated, you must name it "AutoExec".

AccessBasic Sample

A sample macro written in AccessBasic is included on your AcuODBC installation disks. It is designed to execute an SQL command and call notepad to display the query results.

This macro assumes that you created a DSN named "Customer" when running the sample program in Chapter 4. If you did not run the sample, create this DSN now, as described in Section 4.4 The Sample Data Source Name. Then to run the macro, do the following:

1. Start Access.

2. In Access, select "Get External Data" from the File menu, and click Import.

3. Select "ODBC Databases" from the "Files of type:" list box and click "OK."

4. Select "Customer" for your data source selection, then click "OK."

5. Select "CUSTOMER" for your table selection, then click "OK."

6. Create a new module and insert the demo text into it. To do this, select "Module" from the Insert menu, select "File" from the Insert menu, select "Files of type: All", then select "access32.mac" (or "access16.mac") from the \acuodbc\macros directory. (Be sure to check the declarations after you insert the text. The statement "Option Compare Database" should be listed only once. The statement "Option Explicit" should be commented out.) Save the module as "AcuODBC Access Demo" using the File menu "Save" command. Close this module.

7. Create a macro that runs the AcuODBC Demo function. To do this, select "Macro" from the Insert menu, select "RunCode" from the resulting Action list box, then type the function name "AcuODBCDemo()" into the Function Name entry field. Save the macro as "AcuODBC Macro" using the File menu "Save" command.

8. Click the "Run" button.