calling a function

  • Thread starter Thread starter dede
  • Start date Start date
D

dede

This function is working :
Private Sub Mise_à_jour_CRA_Click()
Call Macro_Mise_a_jour.Macro_Mise_a_jour
End Sub

But if I create an "Autoexec" macro (or any other one) with a
RunCode
Macro_Mise_a_jour()
an error message appear
And if I convert this macro to VB here's the code !
could you tell me what is wrong ?

Function Macro2()
On Error GoTo Macro2_Err

Macro_Mise_a_jour()
Call Macro_Mise_a_jour.Macro_Mise_a_jour
Macro_Mise_a_jour


Macro2_Exit:
Exit Function

Macro2_Err:
MsgBox Error$
Resume Macro2_Exit

End Function
 
Hi dede,

You could use the RunMacro action in the AutoExec instead of the
RunCode action. With RunCode it tries to find a function with the specified
name; Macro_Mise_a_jour, which I presume does not exist. Also, the RunCode
requires a Function. It will not run a Sub. When you converted to code, you
still had the issue of no function of that name. Also, to run a macro from
VBA you generally do this:

DoCmd.RunMacro "macro name"

Hope that helps,

Clifford Bass
 
Hi Bass
I try it but unfortunately not working !
But regarding your mail, I did a mistake, It's really a function but I named
it like(Macro_Mise_a_jour) & the system tooks "Macro" as perhaps a real Macro
So I rename it only as (Mise_a_jour) and know it's work !!

Many thank's for your help
have a good day
Cheers
Yves
 
Hi Yves,

That is rather curious. However since me message assisted you, even if
indirectly, that is all that matters. You are welcome. And a good day to
you too.

Clifford Bass
 
NO NO
I'm not able to reproduce that !! it was working one's !
It's really a function, I'm able to see it under the expression builder, but
I still have the error message

I also try to call them sub Mise_a_jour() but do not working !
I'm perhaps too tired !!
I will see tommorow
 
Hi Yves,

Where is the Mise_a_jour function located? It should be in a regular
module--that is one that is not associated with a form or with a report.
Also, it may help to add the word Public in front.

Public Function Mise_a_jour()

Clifford Bass
 
It would appear that you named the module in which the function exists the
same as the function.

They must have different names.
 
Hi Douglas,

No, they do not. You can preface a function/subroutine call with its
module name: MyModule.MyFunctionOrSub. Optional when they have different
names. Necessary when they are named the same. But your comment does point
out something that was staring me in the face (pound head on desk); three
calls to the same function, the second indicating, as you noted that they are
named the same:

Macro_Mise_a_jour()
Call Macro_Mise_a_jour.Macro_Mise_a_jour
Macro_Mise_a_jour

Hence the error. Which I should have asked what it stated. Live and
learn!

The only one that would work would be the second.

For Yves: the "Call" is not necessary. Get rid of the first and third
calls. So you should end up with just (assuming you are back to this
original spelling):

Function Macro2()
On Error GoTo Macro2_Err

Macro_Mise_a_jour.Macro_Mise_a_jour

Macro2_Exit:
Exit Function

Macro2_Err:
MsgBox Error$
Resume Macro2_Exit

End Function

Or get rid of that function entirely and in your macro use
Macro_Mise_a_jour.Macro_Mise_a_jour().

Clifford Bass
 
Hi Douglas & Clifford

Many thank's, It's work.
Douglas is right,I have to named the module in which the function exists
with a other name else it's not working !!
and it's as well true that I do not need the call, it's not necessary.

Have a good day
Cheers
Yves
 
Hi Douglas,

Yves' comment made me go back and double-check. It would appear that
while you can call a function that uses the same name as its module from VBA,
that you cannot from a Macro. Nor, I discovered, could you do so with
Application.Run. Not a big issue is it is one's own code--just rename the
function. But if that is not an option, then one could always create a VBA
wrapper function to get around the problem.

Clifford Bass
 
Back
Top