Error assigning a value (1 Viewer)

David44Coder

Member
Local time
Today, 13:12
Joined
May 20, 2022
Messages
109
Oops, thought I had this nailed (could swear it did work but not anymore)

Forms!frmTest!Single = "ddd"
Forms("frmTest")!Single = "Q213"

Run—time error '2450':
Microsoft Access cannot find the referenced form 'frmTest'.

The Form and the field named "Single' do exist.
 

June7

AWF VIP
Local time
Yesterday, 17:12
Joined
Mar 9, 2014
Messages
5,465
Is frmTest also name of subform container control?
 

David44Coder

Member
Local time
Today, 13:12
Joined
May 20, 2022
Messages
109
No, I don't think so. How do I confirm that ?
 

June7

AWF VIP
Local time
Yesterday, 17:12
Joined
Mar 9, 2014
Messages
5,465
In design view look at the Name property of the container. I always give them names different than objects they hold, like ctrTest.

Must reference controls/fields through the container control name.
 

David44Coder

Member
Local time
Today, 13:12
Joined
May 20, 2022
Messages
109
Thank you, I have found 2 things that work

Forms!frmTest!subTest2.Form!Single = "ddd"
Form_frmtest.Single ="eee"

Must admit I don't quite follow it all, but one method better than another? The latter looks tidier.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:12
Joined
Sep 21, 2011
Messages
14,231
Must admit I don't quite follow it all, but one method better than another? The latter looks tidier.
Well you need to, so pay attention to June7's comments.
It is important to understand the path when using subcontrols.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 11:12
Joined
Jan 20, 2009
Messages
12,851
Forms!frmTest!subTest2.Form!Single = "ddd"
Form_frmtest.Single ="eee"

Must admit I don't quite follow it all, but one method better than another? The latter looks tidier.
The first one will only work on a specific form that is already open with that subform in it. Otherwise it will throw an error.

The second one will open a hidden instance of the specified form object if it doesn't find an already open instance somewhere.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 11:12
Joined
Jan 20, 2009
Messages
12,851
In design view look at the Name property of the container. I always give them names different than objects they hold, like ctrTest.

Must reference controls/fields through the container control name.
It is often used, but the word "container" is a poor choice of terminology for the subformcontrol. Access already has a Container Object.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 11:12
Joined
Jan 20, 2009
Messages
12,851
Forms!frmTest!subTest2.Form!Single = "ddd"
Using the bang (!) is not a good practice for controls. It is a Late Bound Reference to the Default Collection of the Parent Object. As such the control it refers to will not be validated as existing during compilation. The dot (.) is an Early Bound reference and will be checked during the compile.

BTW Another subform reference that would work in this context but is little known is:
Code:
 Forms!frmTest.subTest2!Single = "ddd"

This works because the bang after subTest2 refers to the default collection of the subformcontrol which is Form.Controls.
Mentioned for interest's sake only. I'm not suggesting anyone uses it, firstly because it is Late Bound and secondly because it would bewilder the vast majority of developers who were subsequently faced with the code.
 

David44Coder

Member
Local time
Today, 13:12
Joined
May 20, 2022
Messages
109
Interesting,... so which is best Galaxiom? Or doesn't it matter much:" And thanks for the explanations. I've never seen that anywhere else.
Sorry June I certainly read what you wrote but I was guessing a bit on the meaning (which I assume are the names making up the command separated by "." and "!". I read "container" to be database.
 

Users who are viewing this thread

Top Bottom