PDA

View Full Version : Calling VBA Subroutine from a switchboard



pklocke
06-21-2010, 05:07 PM
I am attempting to call a subroutine I have created, via a switchboard. I want to do this just begin to create a simplified menu that execute code I run frequently (like sync'ing external data to my tables, for example).

I have created a switchboard entry for it that looks like this:
http: //i69.photobucket.com/albums/i41/pklocke/switchboard.jpg
(sorry...I guess I am too new to post a link...so merge the above back together with no spaces)



the "PrepareTicketFile()" entry is the name of my subroutine. But alas, when I click on it to execute it in the switchboard, I get an error, saying:

"The object doesn't contain the Automation object 'PrepareTicketFile."

Probably somethings superobvious...so don't laugh (note: using access 2007).

thanks!

pklocke

CreganTur
06-22-2010, 07:07 AM
Your Sub has to be module or class level to be called from the switchboard. All code written in Form modules is private to that Form.

pklocke
06-22-2010, 07:36 AM
Thanks...the code is in a module, thus why I thought it would work. What I did figure out, tho, was that if I changed the subroutine to a function, instead (I am not returning anything), then it worked fine.

I am not up on "classes" at this point, so not sure what that is all about...but trying to learn.

So, I guess I got it working...but perhaps not in the best of ways.

Imdabaum
06-25-2010, 08:23 AM
Thanks...the code is in a module, thus why I thought it would work. What I did figure out, tho, was that if I changed the subroutine to a function, instead (I am not returning anything), then it worked fine.

I am not up on "classes" at this point, so not sure what that is all about...but trying to learn.

So, I guess I got it working...but perhaps not in the best of ways.

All it means is you cannot call a subroutine from Form1, when you are in Form2, Form3, etc... Subroutines only apply to the class/form that they are built under.

If you have functionality that is used in Multiple forms, then you create a public function in a module. Functions can usually be accessed accross all classes/forms, queries and other modules provided they are declared public and not private.

Long story short, if you want the subroutine to run in your switchboard, you need to put it in the switchboard directly, or use a function(not subroutine) as you discovered.

pklocke
06-25-2010, 08:28 AM
Makes sense...(now LOL).

Appreciate the help!

Pat