Results 1 to 4 of 4
  1. #1
    Lounger
    Join Date
    Dec 2007
    Location
    Sacramento, California, USA
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    macro (MS Access 2003)

    Bear with me... I am very new to this. Someone on a Craigslist forum walked me through being able to browse for a file to import into a table. It involved a module and then we built the expression in VB attached directly to a button on a form.

    What I want to do is instead of the action being tied directly to the "on click" even in the properties of the button I want it to be in a macro. How do I do this?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: macro (MS Access 2003)

    Welcome to Woody's Lounge!

    You probably have an event procedure looking like this:
    <code>
    Private Sub cmdImport_Click()
    ...
    ...
    End Sub
    </code>
    where cmdImport is the name of the command button. You could do the following (in the Visual Basic Editor):
    - Select all the code in the lines between the Sub and End Sub lines (i.e. the code represented by the ... above).
    - Press Ctrl+X to cut the code to the clipboard.
    - Select Insert | Module to create a new standard module.
    - Type the following line in the new module:
    <code>
    Public Sub Import()
    </code>
    - Press Enter. You'll see
    <code>
    Public Sub Import()

    End Sub
    </code>
    - The blinking insertion point will be in the empty line between Sub and End Sub.
    - Press Ctrl+V to paste the code you cut.
    - Switch back to the form module.
    - Insert a line between Sub and End Sub.
    - Press the Tab key, then type <code>Call Import</code>
    - The code should now look like this:
    <code>
    Private Sub cmdImport_Click()
    Call Import
    End Sub
    </code>
    - You can now use the Import procedure anywhere in your database.

  3. #3
    Lounger
    Join Date
    Dec 2007
    Location
    Sacramento, California, USA
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro (MS Access 2003)

    Thank you so much.

    What action do I use in a macro to run this?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: macro (MS Access 2003)

    Frankly, I almost never use macros in Access (with two exceptions: a macro named AutoExec can be used to execute actions when the database is opened, and a macro named AutoKeys can be used to define database-wide keyboard shortcuts). I find VBA code much easier to write, read and maintain than macro instructions.

    If you want to call VBA code from a macro, you must use a Function instead of a Sub:

    Public Function Import()
    ...
    End Function

    You can then call this function from a macro using the RunCode action. Specify Import() as the function name argument.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •