How to CallByName a sub in another module?

R

Renny Bosch

That did it. Here's the whole thing.

In my Form's Object Module, there is:

Sub Run_Click()
Dim pn As Integer
If IsNull(Me.probno) Then 'read the problem number
pn = 0
Else
pn = Me.probno
End If
On Error GoTo err_handler
Me.panel = Eval("Euler" & pn & "()") 'call the function and show the
result
Exit Sub
err_handler:
If Err = 2425 Then
Me.panel = "No such problem number (yet)"
End If
End Sub

And the various Euler## functions are in some Standard Modules, such as:

Function Euler2() 'Find the sum of all even Fibbonacci numbers < 4,000,000
Dim n1 As Long
Dim n2 As Long
Dim s As Long
n1 = 1
n2 = 2
Do
If n2 Mod 2 = 0 Then s = s + n2
n2 = n1 + n2
n1 = n2 - n1
Loop Until n2 > 4000000
Euler2 = _ 'return the result
"The sum of all even Fibbonacci numbers < 4,000,000 is" & vbCrLf _
& s
End Function

And it all works like a charm, without a long tedious ElseIf chain like

If pn = 1 then
Euler1
ElseIf pn = 2 then Euler2
ElseIf pn = 3 Then Euler3
etc.
End If

Yesterday my testing made me think the Eval() call had to be in a Standard
Module, not in the Form's Object Module, but today I find it is working as
it should.

Thanks to all who helped so much.

Best regards.

Renny
 

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