Q:how to call a method with a variable number of parameters?

OK the plog mystery has been solved.
It wasn't too many quotes, it was not enough:
this works:

Sub PlogTest2()
str1 = Chr(34) & "Hello" & Chr(34)
Eval ("MsgBox(" & str1 & ")")
End Sub

The way he wrote it, it Eval() wasn't evaluating the "msgbox" it was opening the msgbox, in an attempt to get data to evaluate.

so YES, the FUNCTION needs to be in quote marks.

as for mystery 2: "The stringexpr argument must evaluate to a string or numeric value; it can't evaluate to a Microsoft Access object." Controls on forms is just an exception to that rule.

So using the corrected (PlogTest2) syntax, Access is no longer abruptly quitting.

I am getting a "cannot find name: MyObject" error. And I think that's the end.

You cannot use Eval() on methods. Functions: yes, methods: no.

You can't use Eval on a DoCmd statement, and now I'm reading that "VB is a compiler language, and you can't compile code during runtime"

So back to the OP: How to pass multiple parameters to a Method??
 
The method has to expect the variable numbers and declare the optional nature of the parameters appropriately, OR you have to find a way to pass literal parameters through a VARIANT array.

Galaxiom, I was imprecise as to my syntax on the "Optional" stuff.

This syntax works: Sub X(Optional ByVal sArg1 as String = "Default")

Then you don't need "IsMissing" and you can use NZ because if you omit the argument, you get the default string. But if you put a comma in the argument sequence, that value comes back as testable by NZ (or at least it did that on my system with Office 2013 32-bit.)
 
So, why can you not rewrite your method, or create a different method to expect one parameter, that of an array.

An array is actually passed byref, however you try to pass it, and within your method, you can use normal array handling proedures to determine the ubound and so on.

Maybe you need to pass two parameters, the array, and the dimension (colcount) of the array

then simply this, without requiring eval.
mymodule.mynewmethod(myarray, arraycolumncount)

it's just a matter of populating the array.
 
So, why can you not rewrite your method, or create a different method to expect one parameter, that of an array.

Because what they called "MyObject.MyMethod" was not their object at all but a third party COM object method, ImageMagick.Convert

MyObject probably wasn't really the best choice of a placeholder name.

Eval() is not going to do the job no matter how hard they try because all it can actually process is the VBA builtin functions. It can't even process a UDF let alone a method of an object.

I don't think there is any way to pass a variable number of parameters to the function. Instead simply call the method with a fixed number of parameter for each file.

Each case where a different number of parameters is essential will require a different call line. This could be built into a Class that accepted a Parameter Array and chose the call line depending on the number of items in the array.
 
Because what they called "MyObject.MyMethod" was not their object at all but a third party COM object method, ImageMagick.Convert

MyObject probably wasn't really the best choice of a placeholder name.

Eval() is not going to do the job no matter how hard they try because all it can actually process is the VBA builtin functions. It can't even process a UDF let alone a method of an object.

I don't think there is any way to pass a variable number of parameters to the function. Instead simply call the method with a fixed number of parameter for each file.

Each case where a different number of parameters is essential will require a different call line. This could be built into a Class that accepted a Parameter Array and chose the call line depending on the number of items in the array.


(I thought MyEverything was always a VB placeholder)

MSDN says clearly Eval() will work on USER functions.

This had me thinking, a lot. But at the top end we would have to have high hundreds of parameters, it would be unworkable.
But three things have occurred to me:

1. The ImageMagick Object “convert” command does return a string. (details about the image it saved.) So as such it is technically a “function.” But in my implementation I have dimensioned it as an object variable, and set an object reference.

2. I have created things I call “visibility functions.” If you want to set a variable in VB and then read it in a query you have to create a one line function like :

function funcMyVar()
funcMyVar = MyVar
end Function

3. I have used various modules that “expose API calls” ? – this is the edge of my knowledge – but many have used “aliases” and other kinds of references (that I don’t quite get) to enable things like Open File dialog boxes, or getting me access to the windows 7 taskbar.

So given 1, 2, and 3: would there be a class module – again the edge of my knowledge --- or some other way to call (or otherwise invoke) the ImageMagick object DLL in such a way as to make the .convert function visible to the Eval() statement as a function?
 
Last edited:
(sorry for the inadvertent bump, i was trying to post a new thread. this thread is years old)

FYI: there was no (vba) solution.

You simply can't call a method without a consistent number of parameters.

The workaround was to dump it all into a text file, then shell out and run an exe that reads the txt file. (like a .BAT)

way too much HD and OS work for a task that should have been internal, it was slow cumbersome and one of many things that killed that project.
 
This passes a string of values (field values from a table) to a function's paramarray using eval.
mymethod must be a public function in a standard module.

Code:
Public Function mymethod(ParamArray params() As Variant)
    Dim s As Variant
    For Each s In params
        Debug.Print s
    Next
End Function

Code:
Private Sub Command0_Click()
    Dim s As String, f As DAO.Field
    
    With CurrentDb.OpenRecordset("table1")
        Do Until .EOF
            For Each f In .Fields
                If Len(s) Then s = s & ","
                s = s & "'" & f.Value & "'"
            Next
            .MoveNext
        Loop
    End With
    
    Eval "mymethod(" & s & ")"
End Sub
 
This passes a string of values (field values from a table) to a function's paramarray using eval.
mymethod must be a public function in a standard module.

Yes. Eval() works on functions.

You've named that code "MyMethod" but you declared it as a Function.

A "Method" (IFAIK) is a... thing ... (piece of code) within a COM Object (a DLL file) Some Methods return values and as such act like functions.

EDIT: Controls also have Methods. btnMyButton.MoveSize(x,y,x2,y2) is a method of the command button control object.

I was trying to use Eval on a Method. THAT does not work.
 
Last edited:
It was just an example.

A class can have properties, where you set/let or get values.
A method is just something that does something - like a standard sub.

You can't use eval on an object, that's true. But you can a UDF, and a function can handle an object.

Code:
Eval "Run_mymethod(" & s & ")"

Public Function Run_mymethod(ParamArray params() As Variant)
    Dim c As New Class1

    c.MyMethod params()
    
    c.MyMethod "hello", "world"
    c.MyMethod 1, 2, 3, 4, 5
End Function

Of course the method needs to have been coded specifically for it to work.

Code:
Public Sub MyMethod(ParamArray params() As Variant)
    Dim s As Variant, p As Variant
    For Each p In params
        If IsArray(p) Then
            For Each s In p
                Debug.Print s
            Next
        Else
            Debug.Print p
        End If
    Next
End Sub
 

Users who are viewing this thread

Back
Top Bottom