Beginner
Beginner

Reputation: 52

Error: Compile Error,ByRef argument type mismatch

I have an error that says "Compile Error,ByRef argument type mismatch". Can anyone let me know why this error occurs or how to fix this?

Upvotes: 1

Views: 1029

Answers (2)

YowE3K
YowE3K

Reputation: 23984

The declaration of your sub is:

Private Sub AddtoWhere(FieldValue As Variant, FieldName As String, mycriteria As String, argcount As Integer)

You are calling it as (for example):

AddtoWhere cboProduct1, "Product", mycriteria, argcount

Because you have only declared one variable (Search as a String) all other variables are declared as Variant by default.

This means you are trying to pass:

  • cboProduct1 (a Variant) to FieldValue (a Variant) - because a Variant can be a Variant/Object, it is possible that cboProduct1 is an object of some sort (ComboBox?) and this will be handled correctly
  • "Product" (a String) to FieldName (a String)
  • mycriteria (a Variant) to mycriteria (a String)
  • argcount (a Variant) to argcount (a Integer)

Because the variables being passed to myCriteria and argCount do not have the same data type, you get an error.

The best way around the problem (and the best thing to do even if you didn't have a problem) would be to declare all your variables.

So add the following statements (at least) to your cmdsearch1_Click subroutine:

Dim mycriteria As String
Dim argcount As Integer

Upvotes: 1

Edward Barthelemy
Edward Barthelemy

Reputation: 26

When you pass an argument by reference it means you are passing a function or sub the same values you have previously. It expects to receive the same. A quick fix for you problem could be to change the function/sub definition to expect variant or to make sure you are passing correct type.

Hope this helped, leave some code next time so we can help more.

Upvotes: 1

Related Questions