Object required - what object?

kirkm

Registered User.
Local time
Tomorrow, 05:04
Joined
Oct 30, 2008
Messages
1,257
I have two Forms open and a button on one Form to read values in the other. I'm trying:
Code:
Private Sub Cmd1_Click()
Dim s as String
With Form_frmMain
s= .Comm  (I also tried "s!Year")
End with
End Sub

Getting Run time error 424 Object Required.
"Form_frmMain" is the name in project explorer and "Comm" is the control name on that Form.
Where would I add an Object reference?
Thanks.
 
To get a value from another open form, the syntax is:
Code:
Forms!TheNameOfTheForm!ControlName
So your code should be:
Code:
With Forms!frmMain
Keep in mind, the project explorer add "Form_" to the form name, use the navigation pane instead to get the correct name.
 
dot (.) is used for member of the class,
in your case the Form.

you can use the Bang (!) to reference
controls attached to the form:

With Form_frmMain
s = !Comm
Debug.Print s
End With
 
"Form_frmMain" is the name in project explorer and "Comm" is the control name on that Form.

Further to JHB's comment, you should never refer to any object via its module. This includes referring to its methods (public functions and subs).

This is because, if the object is not already open, the call will result in a hidden instance of the object being loaded. This instance will remain open and share the same name as any subsequent intentionally opened instance.
 
Thanks very much... a relief to get it working !
 
Galaxion, that is not use the name starting with "Form_"/Ok !
 
Galaxion, that is not use the name starting with "Form_"/Ok !

Exactly. Never use Form_formname in any reference.
Always Forms!formname

(Either the Dot or the Bang work and it makes no practical difference.)

BTW If you need to test if a particular form is already loaded use
Code:
 CurrentProject.AllForms("formname").IsLoaded
 
Exactly. Never use Form_formname in any
reference.
Always Forms!formname

may i know where or which book did you
get this idea.

'coz all the books i read about form Class
never said to "Never" use it.

the authors are MVPs.
 
galaxiom; said:
Never use Form_formname in any reference.
Always Forms!formname
may i know where or which book did you
get this idea.
'coz all the books i read about form Class
never said to "Never" use it.
the authors are MVPs.

Regardless of the credentials of the author, every claim must stand on its own merits and they would need to explain why the bang shouldn't be used. MVP or not, a statement not supported by a logical and rational argument carries no weight.

Right or wrong, unsubstantiated claims are sometimes uncritically accepted and repeated until they become the prevailing opinion. The difference between the bang and dot operators is probably the most prevalent case of this phenomenon in Access/VBA.

While always open to consider any counter arguments, I have little doubt that the bang is actually more correct than the dot in this context.

Firstly, from a practical sense. Access 2010 introduced Intellisense to the Query Designer. On the criteria row, a list of forms in the project will only be offered when Forms is followed by a bang, not a dot. That would certainly be an odd design decision by Microsoft if the bang were incorrect.

It can also be argued from basic principles.

The dot is used for early bound references to members of an object while the bang is a late bound reference to the default member and is ignored during compilation.

A form object as a member of the Forms Collection only makes sense in a Late Bound context since the Forms Collection is empty until run time. Hence the bang would be the appropriate operator.
 

Users who are viewing this thread

Back
Top Bottom