Event Procedures on Subform Won't Work From Within Main Form

How I Fixed this

microsoft.public.access.forms

Tuesday, April 17, 2001 09:49:00

I have a Pop-up Calendar Control visual basic module, which generally stores the Calendar Control in its own form, "calendar_form" and plugs the date selected into the text box of the "regular" form (called "sub_form" for the purposes of this discussion). It's activated by double-clicking the date text box. The code in the text box named beg_date (on double click) looks like this:

Private Sub beg_date_DblClick(Cancel As Integer)
' Show calendar_1 and set its date.
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "calendar_form"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Forms![calendar_form]!calendar_1.Visible = True
Forms![calendar_form]!calendar_1.SetFocus
' Set to today if beg_date has no value.
Forms![calendar_form]!calendar_1.Value = IIf(IsNull(beg_date), Date,
beg_date.Value)
End Sub

The code which appears in the "on click" event of the calendar in the "calendar_form" looks like this:

Private Sub calendar_1_(Click)
' Set beg_date to the selected date and hide the calendar_1.
Forms![sub_form]!beg_date.Value = Forms![calendar_form]!calendar_1.Value
DoCmd.Close
End Sub

This code works perfectly, as long as "sub_form" is the only form open. However, whenever the "sub_Form" is an actual subform opened up under another parent form, this does not work. When the date field is double-clicked, the calendar form successful pops up. However, when the date is picked, the following error pops up:

Run-time Error 2450--Can't Find the form "sub_form" referred to in a Macro or Visual Basic Code.

Obviously, it's the 2nd part of the code in the calendar which needs to be fine-tuned.

How do you make this work whenever "sub_form" is opened as a subform within a parent form as opposed to being the only open form?


From Sasha Furr sbfurr@earthlink.net

Larry

you have to refer to the sub_form control of the parent form
I usually use the expression builder to get the syntax right.

If you are saying Forms!name
then Access assumes it is another form independant of the current form

If you say Forms!sub_form_name.Forms!controlname
then Access knows to look inside the main form for a child form and then
treat that child form as a form itself.

Forms![calendar_form]![subformcontrolname].Form!beg_date.Value

hope this is helpful,
Sasha


How I Fixed This (Me, Larry Harrison Jr)

I was correct. The problem was the syntax of the Visual Basic in the calendar control on the "calendar_form." The original syntax looked like this:

Private Sub calendar_1_(Click)
' Set date_1 to the selected date and hide the calendar_1.
Forms![log_form]!date_1.Value = Forms![calendar_form]!calendar_1.Value
DoCmd.Close
End Sub

The 3rd line (in bold) is the one which had to be changed. Log_Form was the name of the form which had the date_1 field which was the field to be affected. The thing is, since log_form in this case was a subform, the syntax had to be changed to account for this. It was a subform under the main_form form. I noticed that the name of the this subform (log_form) control within "main_form" was, in fact, called "log_form." So the syntax of the line now becomes:

Forms![main_form]![log_form].Form!date_1.Value = Forms![calendar_form]!calendar_1.Value

The name main_form is the actual name of the parent form, and the name log_form is what the subform is called within this main form. And date_1 is the name of the field within log_form.