none
Can I reference a function directly in a control's OnClick event property? RRS feed

  • Question

  • I have a form that was written in Access 2003, that executes a function directly from the OnClick event of several button controls. The function name is entered directly in the Property Sheet for the On Click event. For example, where you would normally see [Event Procedure] for the On Click event property, the function is referenced directly, =MyClickFunction(1), where the argument (1) refers to which button was clicked.

    I converted the application to Access 2010 (accdb file format), and now the On Click events don't work. I get an error message "The expression On Click you entered as the event property produced the following error: The expression you entered has a function name that [My App] can't find."

    The code was a great timesaver, since every button needs to run the same function but with a different argument. Being able to reference the function directly on the property sheet saved me from having to write a separate On Click function for each control.

    Any ideas about why this used to work but doesn't now would be greatly appreciated!

    Monday, October 8, 2012 12:37 AM

Answers

  • Then I figured out why I need the full reference. I have another form that also has a public function with the same name.

    But it still seems strange that I didn't have this problem in the Access 2003 version; it only popped up when I converted to 2010.

    Interesting.  I don't think we have the full explanation yet, because I just created a database in Access 2010 with two different forms, each of which has a public function named "TestButtonClick", and each of which has a command button that invokes its own TestButtonClick function directly from the button's OnClick property, as

        =TestButtonClick()

    These forms and their command buttons work fine, whether opened singly or both at the same time.  Clicking the button on either form invokes that form's version of the function, and Access doesn't complain.

    I wondered if it could be that you also have a public function in a standard module with the same name, so I tested whether that made a difference in my test database, but it didn't.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by Mike Storms Wednesday, October 10, 2012 4:13 PM
    Tuesday, October 9, 2012 8:38 PM

All replies

  • Did you have a macro named [My App] in the 2003 database?
    Monday, October 8, 2012 1:00 AM
  • No, I don't have any macros. The actual error message reads: "The expression you entered has a function name that SNAP III can't find." -- where SNAP III is the name of the application.
    Monday, October 8, 2012 1:48 AM
  • 1.  Have you checked the References collection of the database and see if any Reference is missing?

    2. Have you recompiled the code after converting to ACCDB to eliminate compilation errors?

    3.  Have you added the folder where the database resides into the Trusted Folders in the Trust Center settings for your Access installation?


    Van Dinh

    Monday, October 8, 2012 2:57 AM
  • Thanks for responding to my post.

    1. No, Tools > References does not show any missing references.

    2. Yes, I ran the /decompile switch and recompiled.

    3. Yes, I added the database folder to Trusted Locations.

    Monday, October 8, 2012 4:46 PM
  • This should work the same as it did in A2003, provided that VBA code is enabled -- which it should be if you designated the database folder as a trusted location.

    Can you successfully call the function directly from the Immediate Window?


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Monday, October 8, 2012 7:12 PM
  • Is the function name really MyClickFunction?

    If it's in a standard module is the Function Public?
     
    If it's in a standard module was that module renamed to the same name as the function?

    Chris.

    Brisbane Australia, GMT+10, Access 2003, Public at SkyDrive

    Monday, October 8, 2012 9:32 PM
  • Thanks for the reply. I can execute the function in the Immediate Window if I use the full reference: forms.MyForm.MyClickFunction(1). So then I tried using the full reference in the control's Property Sheet On Click event, and that works too.

    Then I figured out why I need the full reference. I have another form that also has a public function with the same name.

    But it still seems strange that I didn't have this problem in the Access 2003 version; it only popped up when I converted to 2010.

    Tuesday, October 9, 2012 7:40 PM
  • Then I figured out why I need the full reference. I have another form that also has a public function with the same name.

    But it still seems strange that I didn't have this problem in the Access 2003 version; it only popped up when I converted to 2010.

    Interesting.  I don't think we have the full explanation yet, because I just created a database in Access 2010 with two different forms, each of which has a public function named "TestButtonClick", and each of which has a command button that invokes its own TestButtonClick function directly from the button's OnClick property, as

        =TestButtonClick()

    These forms and their command buttons work fine, whether opened singly or both at the same time.  Clicking the button on either form invokes that form's version of the function, and Access doesn't complain.

    I wondered if it could be that you also have a public function in a standard module with the same name, so I tested whether that made a difference in my test database, but it didn't.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by Mike Storms Wednesday, October 10, 2012 4:13 PM
    Tuesday, October 9, 2012 8:38 PM
  • I'm puzzled, too. It turns out that some other things are not working correctly since I converted from 2003 mdb format to 2010 accdb. When I get a chance to troubleshoot this some more, I'll post my findings here.
    Tuesday, October 9, 2012 10:37 PM
  • Hi Mike Storms,

    Welcome to the MSDN forum.

    >> So then I tried using the full reference in the control's Property Sheet On Click event, and that works too.

    Have you tried to put the function in the standard module instead of the Form's module?

    Have a nice day.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 10, 2012 9:57 AM
    Moderator
  • Thanks for your reply, Yoyo. As you can see from the exchange between Dirk and me, there seems to be something else going on here, related to the conversion of my file from Access 2003 to 2010. When I get a chance to do more troubleshooting on this, I'll post more information.
    Wednesday, October 10, 2012 4:16 PM
  • Thanks for your reply, Chris. The actual name of the function is ClickMe(). It's in the module behind the form.
    Wednesday, October 10, 2012 4:17 PM