Tricky Form class module VBA syntax issue

  • Thread starter Thread starter Jon Furman
  • Start date Start date
J

Jon Furman

I ran into a funny VBA issue in Access this weekend, took me a little while
to figure it out.and I'm still not sure as to the why's of the matter.
Here's what I ran into, I had a form with a public sub declared. In some VBA
code outside of the forms class module I called the method like this:

[Forms]![MyForm].[MyMethod]


And I got errors at runtime. Then I changed the method call to:

Forms("MyForm").MyMethod

and everything worked!


It was my understanding that the two syntaxes above are complelely
equivalent, but apparently they're not. So my question is, why not? Thanks
for reading.


Jon Furman
 
Jon Furman said:
I ran into a funny VBA issue in Access this weekend, took me a little while
to figure it out.and I'm still not sure as to the why's of the matter.
Here's what I ran into, I had a form with a public sub declared. In some VBA
code outside of the forms class module I called the method like this:

[Forms]![MyForm].[MyMethod]


And I got errors at runtime. Then I changed the method call to:

Forms("MyForm").MyMethod

and everything worked!


It was my understanding that the two syntaxes above are complelely
equivalent, but apparently they're not. So my question is, why not? Thanks
for reading.


Jon Furman

The problem is the square brackets around MyMethod. Those square brackets
indicate that MyMethod is a field - which it isn't, it's a method.

FWIW, [Forms]![MyForm].MyMethod and Forms("MyForm").MyMethod are indeed
identical.
 
Thanks, I'm off to try that out now. The funny thing is that I tried the
same thing with a standard class module and it did work. I'll check more
carefully and see what the final bracket situation is. Thanks.


Jon Furman
 
Ok I'm back from the test..you're right omitting the final brackets does
force VBA to recognize the last bit as the method. Bracketing the forms
method causes it to fail, but it's also true when calling a standard class
module VBA recognizes the method whether there's a bracket or not. So I
still feel like I'm missing something subtle here.


Jon Furman
 
still feel like I'm missing something subtle here.

Probably, but it's undocumented behaviour, and it is
different in different versions of Access. It is probably
just something that fell out of the parsing engine,
not a deliberate attempt to guide and assist users.

(david)
 
Jon Furman said:
Ok I'm back from the test..you're right omitting the final brackets does
force VBA to recognize the last bit as the method. Bracketing the forms
method causes it to fail, but it's also true when calling a standard class
module VBA recognizes the method whether there's a bracket or not. So I
still feel like I'm missing something subtle here.


Jon Furman

At a guess, I would say it's because a standard class module doesn't have
fields, so there is no scope for confusion.
 
Back
Top