1

I have the following code which is doing some collecting of data from webpages. My questions is why does the code 'work' as I expect with the 'CALL' statement, but doesn't work without it...

    Dim matchURL            As String
    Dim FixtureDetailsTab   As HTMLTable

    For Each match In FixtureCollection
        matchURL = match.getMatchURL
    '
    '  Load up the match table
    '
        oXML.Open "GET", matchURL, False
        oXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        oXML.send

        Set htmlDoc = New MSHTML.HTMLDocument
        Set htmlBody = htmlDoc.body
        htmlBody.innerHTML = oXML.responseText
    '
    '  And once again look for the elements with class 'engineTable'
    '  (only interested in the first one)
    '
        Set Elements = htmlDoc.getElementsByClassName("engineTable")
        For Each element In Elements
            Set FixtureDetailsTab = element
            Exit For
        Next element

        Call match.addDetails(FixtureDetailsTab)

    Next match

match is a custom class in a separate Class Module, and 'addDetails' is defined as follows

    Public Sub addDetails(detailTab As HTMLTable)
        ....
    End Sub

The code above works, but if I remove the call statement and just try and invoke the Sub as below

    match.addDetails (FixtureDetailsTab)

I get 'Run time error 13' - Type mismatch

I'm puzzled. Any help appreciated.

Thanks

Jernau
  • 23
  • 1
  • 4

1 Answers1

2

When you call a function/sub in VBA, there are two alternatives:

  1. Use CALL and enclose the argument list in parentheses

     CALL MySub(param1, param2, param3)
    
  2. Or omit CALL and omit the parentheses

     MySub param1, param2, param3
    

Any of the parameters may be an expression, that itself includes or is enclosed in parentheses:

MySub param1, (param2 + 2), param3

In your example without CALL, you are passing a single parameter, which is an expression enclosed in parentheses:

match.addDetails (FixtureDetailsTab)

In VBA, enclosing an object in parentheses will typically return the default property of that object.

Hence you are attempting to pass the default property of FixtureDetailsTab to your method, resulting in a type mismatch.

If you add the CALL statement, the parentheses are interpreted as part of the CALL syntax, and the FixtureDetailsTab is passed as an argument, as you'd expect.

Such charming inconsistencies are part of the reason why VB is loved so much.

Joe
  • 122,218
  • 32
  • 205
  • 338