Cannot create dynamic reference to subform control

geoB

Registered User.
Local time
Today, 05:07
Joined
Oct 10, 2008
Messages
68
I've had a working function that modified some command buttons on a form. The code read, in part:

Code:
Set frm = Forms!frmmomsbabies
strSQL = "SELECT baby_id,baby FROM babies WHERE mom_id = " & mom & " ORDER BY baby"
Set rs = CurrentDb.OpenRecordset(strSQL)
For i = 1 To 5
    strCMD = "cmdBaby" & Trim(str(i))
    frm.Controls(strCMD).Visible = False
Next i
The form is now a subform, and I've not been able to get similar code to work:

Code:
Dim frm As SubForm
Set frm = Forms!frmMomsBabies!ctlSubMothers
strSQL = "SELECT baby_id,baby FROM babies WHERE mom_id = " & mom & " ORDER BY baby"
Set rs = CurrentDb.OpenRecordset(strSQL)
For i = 1 To 5
    strCMD = "cmdbaby" & Trim(Str(i))
    frm.Controls(strCMD).Visible = False
Next i
It fails at the line above Next i with an error regarding incorrect reference to a form/report.

Any help is immensely appreciated.

George
 
i haven't checked this but i'm guessing it's something like this...

Dim frm As SubForm 'subforms are controls on forms
Set frm = Forms!frmMomsBabies!ctlSubMothers

try something like:
Code:
Dim frm As Form
Set frm = Forms!frmMomsBabies!ctlSubMothers.Form

sry i can't check the syntax right now.
 
Thanks for the suggestion. Unfortunately,
Code:
Dim frm As Form
Set frm = Forms!frmMomsBabies!ctlSubMothers.Form
yields error 2455 at the Set frm line. I've used the dim as subform, etc., with success in referring to controls on subforms before. Somehow, the syntax of "frm.Controls(strCMD)" doesn't like the construct.

George
 
Learn something new everyday. Just wish it wasn't so painful. The reason for the invalid reference was that the form was not visible! I never realized that visibility was required for subform references to work. Now I do.

Thanks for listening. We now return to our regular programming already in progress.

gwb
 
thanks for posting back.
 

Users who are viewing this thread

Back
Top Bottom