2

I am having trouble determining exactly why the examples below are behaving the way they are.

Works:

Debug.Print Forms!Form1![Subform1]!control1 

Doesn't Work:

Debug.Print Forms!Form1![Subform1].control1 

With Error:

Run-time error '438':
Object doesn't support this property or method


At first glance I just assume .control1 is accessing a property of Subform1 instead of the control itself so it makes sense why there would be an error.

Yet that reasoning doesn't seem to apply when using these to examples:

Debug.Print Forms!Form1![Subform1].Form.control1 
Debug.Print Forms!Form1!Subform1.Form.control1


Screenshots of Results:

With Square Braces

Without Square Braces

To me these examples also look like I am accessing the a .CHAT_MESSAGE_ID property of the Chat's form object, yet I get no error message and they work fine.

Why do these two examples work yet Debug.Print Forms!Form1![Subform1].control1 doesn't?

Newd
  • 2,174
  • 2
  • 17
  • 31

2 Answers2

1

The bang (!) operator accesses default properties and objects in collections. Obviously Subform.Form is the default property of the Subform control and the Controls collection is the default property of the Form class. Therefore you can access controls on the subform with Subform!ControlName. If, however, you say Subform.ControlName you specifically try to access a property named "ControlName" of the Subform control. And that one does not exist.

The bank (!) operator searches for a matching object. The dot (.) operator assumes that an existing property is referenced directly.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

In many situations, an Access form object lets you access its controls and record source fields as if they are properties of the form. That means you can employ Intellisense when you're editing VBA code within that form, so you can refer to the value of a control named control1 with Me.control1 or Me.control1.Value

That feature is also why Debug.Print Forms!Form1!Subform1.Form.control1 can work. However Debug.Print Forms!Form1!Subform1.control1 fails because Subform1 is a control, not a form object, and it does not include a property named control1. (That is what the error message complains about.)

The situation can get more complicated if your subform's SourceObject is a table or query instead of an actual form object. I haven't worked through the details with those variations myself, but I suggest you look into them if you really want to use the SubformControl.Form.property pattern.

I just use something like Forms!Form1!Subform1!control1, which is less confusing to me.

HansUp
  • 95,961
  • 11
  • 77
  • 135
  • This line: `fails because Subform1 is a control, not a form object, and it does not include a property named control1.` was definitely a moment of clarity. Which also explains why saying `.Form` sort of "casts" the control as a form and then it behaves "normally" again. – Newd Jul 09 '15 at 17:52
  • 2
    Good, I struggled with that. ;-) RE the last part of your comment, I would say `.Form` refers to the form object which is contained within the subform control. That form object supports a property named *control1* but the subform *control* does not. – HansUp Jul 09 '15 at 17:57