Trouble launching Function with Button

G

Guest

Hi,

I have lengthy vba function which works perfectly fine when run from the
code viewer. I have a macro which launches this function (using Runcode),
which again works perfectly when run by itself.

However: when I attatch this macro to a button on a form (in order to run
the vba function from a form...) this does NOT work, and produces the
following error:

"Procedure declaration does not match description of event or procedure
having the same name."

Can anyone help with this?

Now, I have had a look about on here and other forums for a solution, but
found nothing very helpful; the consensus seems to be that there might be an
ill-defined variable/ argument in the vba function - which I cannot find in
my code. I find this an unlikely solution in any case, as the function runs
fine when run independently of the form, either on its own or when launched
in the macro. Thoroughly confused, and help (or alternatives) greatly
appreciated!
 
G

Guest

Not having your command button code available, I can't be sure, but here are
a couple of things to look at.

First, the procedure name for your event is determined by Access. For
example if your command button is named cmdFoo then it will be
cmdFoo_Click(). If you are trying to use your function by name instead of
the name generated by Access, it will not work. You put your function call
in the click event Sub

Private Sub cmdFoo_Click()
Dim x as Variant

x = MyFunctionNameHere

The other possibility is that you took the code from your original function
and pasted it into your command button's click event. Since a click event is
a sub, not a function, you cannot return a value. Somewhere in your original
code you may have had something like:
MyFunctionNameHere = strSomeVariable
Even if you tried chaning it to
cmdFoo_Click = strSomeVariable
it is not going to work.
If it is intended to return a value, then you will have to use the model
above where you call the function from witihin the click event sub;
otherwise, you can use the existing code from your function and remove the
return value assignment, and it should work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top