Incompatible ByRef argument type

Solved
Robix -  
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   -
Hello,

A new question for the community...
Here is the problem:
It is divided into two parts:

[1] The first part consists of a module in which I created a specific function:
Function Cam_2_Contacts(A0 As Double, A1 As Double, A2 As Double, levee_max As Double, l As String, t2 As Double, t3 As Double, t4 As Double, t5 As Double, t6 As Double, t7 As Double, t8 As Double)
This function is supposed to return a set of values on an Excel page.

[2] The second part consists of a Userform where I enter the desired values to run my function (A0, A1,....). To do this, I decided that once the values are entered in my form, I just need to press a button to execute the function above and return the calculated values from this same function on an Excel page. I coded as follows:

Private Sub CommandButton1_Click()
B0 = CDbl(frmcam1.A0.Value)
B1 = CDbl(frmcam1.A1.Value)
B2 = CDbl(frmcam1.A2.Value)
l = CDbl(frmcam1.levee_max.Value)
p2 = CDbl(frmcam1.t2.Value)
p3 = CDbl(frmcam1.t3.Value)
p4 = CDbl(frmcam1.t4.Value)
p5 = CDbl(frmcam1.t5.Value)
p6 = CDbl(frmcam1.t6.Value)
p7 = CDbl(frmcam1.t7.Value)
p8 = CDbl(frmcam1.t8.Value)
test = Cam_2_Contacts(B0, B1, B2, l, frmcam1.ldmvt.Value, p2, p3, p4, p5, p6, p7, p8)
End Sub

frmcam1 is the name of my userform.

The problem is as follows...
When I click on CommandButton1 of my userform after entering all my data, the program returns an error message:
Incompatible ByRef argument type
I tested all the data (MsgBox VarType()) and they all conform to the required type for the execution of my function above.

I would like to point out that I am a novice in VBA...

Thank you for any help you can provide...

Robix
Configuration: Windows / Chrome 80.0.3987.163

3 answers

yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
 
Hello, to start, can you use the code tags when you post code, and indicate on which line the error message occurs?
Then, if it hasn't been done yet, can you add
option explicit
at the beginning of the module?
Then, show us the response (MsgBox VarType()) for each of these variables?

You have chosen, for one of the parameters, not to use a variable. Have you tried using a variable for this parameter?
1
Robix
 
Ok yes, sorry for the rather dreadful presentation...

I just ran a few tests again: At first, I forcefully removed all variable types in my function:
Function Cam_2_Contacts(A0, A1, A2, levee_max, l, t2, t3, t4, t5, t6, t7, t8)

Doing this caused no problems, everything works...

Then...
When I put back the respective types of each variable and inserted MsgBox VarType here and there, I got the following:
5 (B0)
5 (B1)
5 (B2)
5 (l)
8 (ldmvt)
5 (p2)
5 (p3)
5 (p4)
5 (p5)
5 (p6)
5 (p7)
5 (p8)
By putting option explicit at the beginning of the module (which I hadn't done), the message indicates an error at line 5 of the following code:
Private Sub CommandButton1_Click() B0 = CDbl(frmcam1.A0.Value) B1 = CDbl(frmcam1.A1.Value) B2 = CDbl(frmcam1.A2.Value) l = CDbl(frmcam1.levee_max.Value) p2 = CDbl(frmcam1.t2.Value) p3 = CDbl(frmcam1.t3.Value) p4 = CDbl(frmcam1.t4.Value) p5 = CDbl(frmcam1.t5.Value) p6 = CDbl(frmcam1.t6.Value) p7 = CDbl(frmcam1.t7.Value) p8 = CDbl(frmcam1.t8.Value) test = Cam_2_Contacts(B0, B1, B2, l, frmcam1.ldmvt.Value, p2, p3, p4, p5, p6, p7, p8) End Sub

This error indicates that the variable is not defined...

To finish, my last test was to rewrite the code as follows:
Private Sub CommandButton1_Click() A0 = CDbl(frmcam1.A0.Value) A1 = CDbl(frmcam1.A1.Value) A2 = CDbl(frmcam1.A2.Value) levee_max = CDbl(frmcam1.levee_max.Value) ldmvt = frmcam1.ldmvt.Value t2 = CDbl(frmcam1.t2.Value) t3 = CDbl(frmcam1.t3.Value) t4 = CDbl(frmcam1.t4.Value) t5 = CDbl(frmcam1.t5.Value) t6 = CDbl(frmcam1.t6.Value) t7 = CDbl(frmcam1.t7.Value) t8 = CDbl(frmcam1.t8.Value) test = Cam_2_Contacts(A0, A1, A2, levee_max, ldmvt, t2, t3, t4, t5, t6, t7, t8) End Sub


I deliberately renamed the variables according to the second person's response on my post (for the sake of clarity for those who will use it).
AND THERE... EVERYTHING WORKS!

I remain perplexed about the origin: Maybe, as you seemed to imply, the error came from my unwillingness to assign a variable to one of my parameters...

Thank you for the time you spent on me! (And thanks for the option explicit) which I didn't know about...
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > Robix
 
you don't show us the statements, which should be located in the sub.
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > Robix
 
In my opinion, it is preferable, in the function declaration, to declare the types of all parameters.
It is preferable not to use the same variable names in the function declaration and in the function call.
It is better not to leave any ambiguity about the type of what you are using.
When you write
frmcam1.ldmvt.Value
, you do not control its type.
If you write
cstr(frmcam1.ldmvt.Value)
, or
dim ch as string ch=frmcam1.ldmvt.Value
, you are certain of the type you are getting.
0
NHenry Posted messages 15235 Registration date   Status Moderator Last intervention   387
 
In my opinion, this comes from the value "frmcam1.ldmvt.Value" (typed Object, I think)

A good practice is that if you don't need to modify the values passed as parameters in your function, you should specify ByVal in the declaration:
Sub MaSub(ByVal Parameter1 As Double, ...)


Also, remember to give explicit names to your variables (p1 p2 t3 b0 are not very clear)

--
I mainly work in VB6 and VB.NET, with a bit of C#, but moderation often brings me to other languages.
In VB.NET, remember to enable "Option Explicit" and "Option Strict"
1
Robix
 
I ran some tests which I explained in a previous response...
Apparently, "frmcam1.ldmvt.Value" seemed to be stuck indeed...
Following up on your remark, I would like to ask you a question: What would be the return value when using the VarType function if the value is typed as Object?
Thank you for these two little coding lessons ;-)
As for explicit names, the users of this program prefer to stick with these names... I just follow orders!

Thank you for everything!
0