简体   繁体   中英

Type Mismatch in conditional

I am new at VBA, but i have been programming a lot of C#. My issue here is that i keep getting "Runtime error 13. Type Mismatch" error in my VBA function.

I am extracting data from a Table in access and then trying to filter some of the data.

My function looks like this:

Function FlowType(deliveryAdrID As String, deliverType As String, note As String) As String
    If (note = "*J") Then
        FlowType = "Weekend"
    ElseIf (deliveryAdrID = "62242" & deliverType = "H") Then
        FlowType = "AirGotland"
    ElseIf (deliveryAdrID <> "62242" & deliverType = "H") Then
        FlowType = "Air"
    Else
        FlowType = "Standard"
    End If
End Function

Why do I get this error? The error occurs on this line:

ElseIf (deliveryAdrID = "62242" & deliverType = "H") Then

Your problem is because you use & , instead of And . The ampersand symbol is an operator to concatenate strings, whereas And is a Boolean operator. So, change your comparisons like so:

ElseIf ((deliveryAdrID = "62242") And (deliverType = "H")) Then

With & , (assuming both conditions are satisified) the expression you are using will evaluate to

ElseIf ("TrueTrue") Then

which doesn't make sense since "TrueTrue" is not Boolean.

You declare deliveryAdrID as Double, but you try to assign a String to it:

deliveryAdrID = "62242"

That's a Type Mismatch, as VBA tries to tell you. ;)

So, just use

deliveryAdrID = 62242

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM