Option Infer ON

723 Views Asked by At

Does option infer not work in VBA?

I'm try to test out VB tutorial code on arrays in Microsoft Access.

Dim numbers = {{1, 2}, {3, 4}, {5, 6}}

This doesn't work without Option Infer ON, but you get compile error if you add it in.

Total noob question.

2

There are 2 best solutions below

0
On BEST ANSWER

The Option Infer directive is a feature added to VB.Net in Visual Studio 2008. The VBA compiler is a completely different implementation that was never updated for Option Infer hence there is no way to make this work

2
On

Just to add to this post; though Option Infer is not present, in VBA you can still turn Option Explicit off which gives you a poor mans version. Be careful, however, as this isn't truly Option Infer. What you're doing is simply making VBA primarily use variants to store the values. The VBA engine then decides what the best subtype of variant is. For example:

Dim a, b
a = 2
b = 3.2
Debug.Print a * b

Will work quite happily. However, you can then go on to change the value later, like so...

Dim a, b
a = 2
b = 3.2
Debug.Print a * b
a = "Hello, World!"
Debug.Print a

Which works quite happily and produces the output:

 6.4
Hello, World!

Not the best practice.

Revision

Just to further reinforce the poor quality of this approach, I have had several experiences where the [original programmer's] code has happily crashed, bringing down the whole application, because variables were not properly defined.