Subform based on query (1 Viewer)

Lady Gragor

Registered User.
Local time
Tomorrow, 06:36
Joined
Nov 4, 2008
Messages
37
Hi, Can anyone see something wrong here? I've done this plenty of times before but for some reason I haved overlooked something and I'm sure it's staring me in the face. I have a form with a combo box named cbo_ProjectName. The subform should populate according to the selection from the combo box (qry_Project) but I'm getting an error. The query qry_Project works on it's own.

Private Sub cbo_ProjectName_AfterUpdate()

'Update the subform
Equipment_SubFrm.Form.RecordSource = "qry_Project"

End Sub

Any suggestions please?
 

speakers_86

Registered User.
Local time
Today, 16:36
Joined
May 17, 2007
Messages
1,919
Try to requery it. Something like this (off the cuff).

Code:
me.controlname.requery
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:36
Joined
Jan 20, 2009
Messages
12,852
What is the error?

Check that Equipment_SubFrm is the name of the subform control on the main form rather than the subform object itself. The wizard makes them the same but they don't have to be.

Also it is best to fully qualify the subform by including the reference to the mainform.
Assuming the code is on the main form module.

Me!subformcontrolname.Form.RecordSource
 

speakers_86

Registered User.
Local time
Today, 16:36
Joined
May 17, 2007
Messages
1,919
Thank you for correcting me with the bang (Me!) and the .Form.

I don't get why vba requires the .Form. Something like Me!controlname.Requery seems to me like that should work. I guess that would be too convenient. Or maybe the code is referencing the Form inside the Control inside the form Me, not just the control itself. Thus

Me!controlname!form.requery

Maybe it makes sense.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:36
Joined
Jan 20, 2009
Messages
12,852
I don't get why vba requires the .Form. Something like Me!controlname.Requery seems to me like that should work.

I guess that would be too convenient. Or maybe the code is referencing the Form inside the Control inside the form Me, not just the control itself.

Me!controlname!form.requery

Exactly.

On a main form the default property is Form so it can be left out, hence the shortened version is very often encountered. (This adds to the confusion about when Form should be included.)

However with a subform, the subform control is being addressed not the form object listed as its Source Object property. The default property of a subform control is not Form so to address anything on the subform object the Form property must be included.

For some reason, probably known only to Microsoft, there is no default property of a subform control. Why they didn't make it Form is probably something to do with consistency. With other controls, Value is the default property and the subform has no Value property.

BTW I have seen it written in a book scanned by Google that Text is the default property of a control. This is incorrect.
 

Lady Gragor

Registered User.
Local time
Tomorrow, 06:36
Joined
Nov 4, 2008
Messages
37
Thanks guys! I was referencing the object not the form itself.

FormName.Form.RecordSource = "qry_Project"

:)
 

Users who are viewing this thread

Top Bottom