VBA - Why does this code need the 'CALL' Statement

Jernau

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

Joe

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does tail call optimization need an op code?

From Dev

Why does Scala need the def statement?

From Dev

Excel VBA Code Does Not Work With If Statement

From Dev

VBA Code for SUMIF statement - Need to build-in additional conditions

From Dev

Why does this code need the volatile keyword?

From Dev

Why does this code print all of the if statement conditions?

From Dev

Why does `Empty` not work in this VBA code?

From Dev

Ranges - VBA - Why does this code not work?

From Dev

Why does my vba code keep freezing?

From Dev

Why this statement need in php

From Dev

Why does BackgroundWorker not need to call EndInvoke when it calls BeginInvoke on a delegate?

From Java

Why does the compiler need an implementation of a trait to call a default free function?

From Dev

Why does a method call need to be disambiguated when it can in principle be a constant?

From Dev

Why does my function need an argument and how to call it with+without?

From Dev

Vba code if statement

From Dev

Vba code if statement

From Dev

Why is this VBA if statement not working?

From Dev

Why does clang still need libgcc.a to compile my code?

From Dev

Why does the Spark application code need to be an object rather than a class?

From Dev

Why does this code have a missing return statement error?

From Dev

Why does this code seem to execute after the return statement?

From Dev

Does this code need synchronization?

From Dev

Why does printing a variable and function call together in this code give an error?

From Dev

why does my vba code see comma as new line?

From Dev

Why does VBA say "(Not Responding)" even when code is running fine?

From Dev

Why does statement with two operands need to be above statements with one operand for correct output?

From Dev

Why does `nanosleep` need the argument of `req` while kernel has the chance to restart the system call again internally(`-ERESTARTSYS`)?

From Dev

Why does `nsenter` need to call `fork` before `exec` to ensure that any children will also be in the newly entered PID namespace?

From Dev

Why does C# not allow me to call a void method as part of the return statement?

Related Related

  1. 1

    Why does tail call optimization need an op code?

  2. 2

    Why does Scala need the def statement?

  3. 3

    Excel VBA Code Does Not Work With If Statement

  4. 4

    VBA Code for SUMIF statement - Need to build-in additional conditions

  5. 5

    Why does this code need the volatile keyword?

  6. 6

    Why does this code print all of the if statement conditions?

  7. 7

    Why does `Empty` not work in this VBA code?

  8. 8

    Ranges - VBA - Why does this code not work?

  9. 9

    Why does my vba code keep freezing?

  10. 10

    Why this statement need in php

  11. 11

    Why does BackgroundWorker not need to call EndInvoke when it calls BeginInvoke on a delegate?

  12. 12

    Why does the compiler need an implementation of a trait to call a default free function?

  13. 13

    Why does a method call need to be disambiguated when it can in principle be a constant?

  14. 14

    Why does my function need an argument and how to call it with+without?

  15. 15

    Vba code if statement

  16. 16

    Vba code if statement

  17. 17

    Why is this VBA if statement not working?

  18. 18

    Why does clang still need libgcc.a to compile my code?

  19. 19

    Why does the Spark application code need to be an object rather than a class?

  20. 20

    Why does this code have a missing return statement error?

  21. 21

    Why does this code seem to execute after the return statement?

  22. 22

    Does this code need synchronization?

  23. 23

    Why does printing a variable and function call together in this code give an error?

  24. 24

    why does my vba code see comma as new line?

  25. 25

    Why does VBA say "(Not Responding)" even when code is running fine?

  26. 26

    Why does statement with two operands need to be above statements with one operand for correct output?

  27. 27

    Why does `nanosleep` need the argument of `req` while kernel has the chance to restart the system call again internally(`-ERESTARTSYS`)?

  28. 28

    Why does `nsenter` need to call `fork` before `exec` to ensure that any children will also be in the newly entered PID namespace?

  29. 29

    Why does C# not allow me to call a void method as part of the return statement?

HotTag

Archive