Search code examples
vbams-access

What are the correct syntaxes for referring to a subform, and why is the recommended one producing an error?


I have the following two lines of code:

Debug.Print Forms!DocLoader!DL_RowBox!DLR_FileName.Name
Debug.Print Forms!DocLoader!DL_RowBox.Form!DLR_FileName.Name

The second one, which I have seen recommended in almost every VBA reference, including the answer being suggested from SO as I type this, follows this structure:

Debug.Print Forms![Form Name]![Subform Control Name].Form![Control Name].Name

These two lines of code should produce the same result. However, the second, recommended syntax throws error 40036, "Application-defined or object-defined error" unless I am in design view. I cannot use it at runtime, but I have never seen this limitation mentioned in any of the reference documentation or forum posts I have looked at. The first line, using only default parameters, seems to work no matter the context.

With the second line, I have tried just about every combination of bang and period I can, and I have also tried enclosing field names in brackets, but the common denominator is that as soon as I reference ".Form" the application throws an error. Even something simple like ".Form.Caption" has thrown an error. So what I would like to know is:

  1. Are there any other correct ways of referring to a subform's form properties, since I need these as well as its controls
  2. Why would the first line execute correctly while the second, recommended one does not seem to work?

Solution

  • Running the compiler appears to have fixed the issue.