none
How to create a button in Access 2010 that runs VBA code

    Question

  • OK, this is just ridiculous.  I am an experienced user of previous Access programs, however, I cannot figure out how to do something that should be very simple in Access 2010.

    I have created a VBA routine in Access that performs some working custom code.  I want to run this code via a single button click in Access 2010, but I cannot find out how to do this.  I have tried creating a macro that "Runs Code", but it never finds my VBA routine (and I have tried several different naming conventions).

    Could someone please give me some guidance?  The more specifics the better, as the included "help" from Access is completely worthless on this basic functionality.

    Thanks in advance!

    Tuesday, July 27, 2010 6:08 PM

Answers

  • Never mind - I found the solution here: http://www.mofeel.net/10-microsoft-public-access-macros/894.aspx

    Solution details:

    • Create VBA function called "ImportTextFile".
    • Create a new Macro.
    • Within macro, add a New Action for "RunCode"
    • In the Function Name field, enter "=ImportTextFile()". (Note the equals character, and parenthesis)
    • Save the macro (& run to test).
    • Right-click on Ribbon & select "Customize the Ribbon".
    • Add a new Custom Group. Add a button to run the specific Macro.

    It is frustrating how much more convuluted this is compared to older versions of Access.  Sigh....

    • Marked as answer by Loki70 Wednesday, July 28, 2010 6:40 PM
    Wednesday, July 28, 2010 6:40 PM

All replies

  • Jennifer,

    Thanks for your reply, but quite frankly, the posts you mentioned are over my head (and I program for a living).  Why again do I need to create & edit XML files? Why do I need to get this involved in the creation of a button?  My application is incredibly simple. 

    I have single VB module named "Module1" with a single sub in it called "ImportTextFile".  I thought I could just create a macro that used the "RunCode" function to call my sub.  When I put "ImportTextFile" into the Function Name box & run the macro, it always comes back with this error:

    Microsoft Access cannot find the name 'ImportTextFile" you entered in the expression.  You may have specified a control that wasn't on the current object without specifying the correct form or report context.  ...  For example Forms![Products]![Units In Stock].

    I have tried multiple way to call this (Module1.ImportTextFile, Modules![Module1].[ImportTextFile], etc...) but I keep getting the same error.  I would appreciate any other comments or feedback on how I can execute my Sub.

    Thank you!

    Wednesday, July 28, 2010 6:16 PM
  • Never mind - I found the solution here: http://www.mofeel.net/10-microsoft-public-access-macros/894.aspx

    Solution details:

    • Create VBA function called "ImportTextFile".
    • Create a new Macro.
    • Within macro, add a New Action for "RunCode"
    • In the Function Name field, enter "=ImportTextFile()". (Note the equals character, and parenthesis)
    • Save the macro (& run to test).
    • Right-click on Ribbon & select "Customize the Ribbon".
    • Add a new Custom Group. Add a button to run the specific Macro.

    It is frustrating how much more convuluted this is compared to older versions of Access.  Sigh....

    • Marked as answer by Loki70 Wednesday, July 28, 2010 6:40 PM
    Wednesday, July 28, 2010 6:40 PM
  • Loki70, you are the man!  This works great.
    Sunday, September 11, 2011 4:18 AM
  • Thanks a lot. Worked like a charm.

    Monday, March 03, 2014 6:18 AM