0

I was going to ask for help but uncovered the reason for the seemingly strange behavior. It seems very obscure to me, so I thought I'd post it in case anyone else runs into something like this. If a guru here can enlighten me why this happens, I'd like to know.

I'm calling a routine that saves UserForm metrics in the registry. The declaration is:

Public Sub SaveFormSettings(FormReference As Variant)

I use the Variant type because UserForms are classes (different types). The userform uses the following call to save its metrics:

SaveFormSettings Me

I generally called this from the userforms' QueryClose event so that the positions and sizes can be restored on the next run with RestoreFormSettings (the form metrics are no longer available at the UserForm Terminate event).

Recently, I added an intermediate routine that does additional work in addition to calling SaveFormSettings, which I call instead in the QueryClose event. That routine looks like:

Public Sub QueryCloseEvent(FormReference As Variant, Optional SaveSettings As Boolean = True)

and it does this:

If SaveSettings Then SaveFormSettings (FormReference)

There is no error reported on compilation. But now, the SaveFormSettings routine is receiving the FormReference parameter not as a UserForm type, like MyForm1, but rather it's of type "Controls". This results in an error when that routine tries to get the name of the form to build the registry key value, via:

n = FormReference.Name & " Form Metrics"

This statement worked when I didn't have the intervening QueryClose function.

Now as it turns out, while typing up this question and analyzing my code for possible errors, I discovered that the problem is caused by the parentheses around the FormReference. When I changed the code to:

If SaveSettings Then SaveFormSettings FormReference ' <-- no parens

it now works as intended. Why?

1 Answers1

2

remove parentheses

If SaveSettings Then SaveFormSettings FormReference

as per the explanation in What are the rules governing usage of parenthesis in VBA function calls?:

"VBA evaluates that object and, absent a property, returns the default property."

the default property of a Userform object seems to be its Controls property

user3598756
  • 28,893
  • 4
  • 18
  • 28