Populating Textbox Within Embedded Form on Tab Control

chathag

Registered User.
Local time
Today, 23:02
Joined
May 22, 2009
Messages
32
I have a sub form (SubForm1) embedded in a Tab Control on Form1. Subform1 includes a text box called Date1. When the user clicks in Date1 a Calendar form opens. When the user selects the date on the Calendar the date should be populated in Date1. Instead of that happening the following error message occurs:

Run time error 2450
Microsoft Office Access can't finf the form "SubForm1" referref to in a macro expression or Visual Basic code.

If I try this on Subform1 whilst it is not embedded in the tab control it works fine.

The code within the calendar form is:

Forms![Subform1]![Date1] = Me.Calendar.Value

Thanks in advance

C
 
A subform is not a member of the 'Forms' collection and therefore cannot be addressed using the 'Forms!FormName' syntax.
To avoid this problem you can pass an object reference to the calendar form so it knows exactly what object it is intended to update.
Put a public variable on the Calndar form ...
Code:
Public TargetControl as Control
...and then consumers of the calendar can set that variable...
Code:
Private Sub AnyFormTextbox_Click()
[COLOR="Green"]  'calendar opens[/COLOR]
  DoCmd.OpenForm "fCalendar"
[COLOR="Green"]  'calender public variable is set[/COLOR]
  Set Forms!fCalendar.TargetControl = Me.AnyFormTextbox
End Sub
Once this is done, code on the calendar can work with the actual textbox object that originated the request....
Code:
Private Sub CalendarForm_DateSelected()
[COLOR="Green"]  'value is assign to the actual textbox on the consumer[/COLOR]
  TargetControl = Me.DateSelected
[COLOR="Green"]  'and close the calendar[/COLOR]
  DoCmd.Close acForm, Me.Name
End Sub
Does that make sense?
Cheers,
 
You can download my free calendar form from here:

It is specially designed to work on sub-forms Tab controls and the like. It should work just about anywhere really.

There’s a set of video instructions, and a demo of it working.

Video number 3 shows you how to set it up in less than 2 minutes!
 
Thanks lagbolt. Your method makes sense, however when I implement it there is nothing being passed to the TargetControl variable on the calendar, therefore an error message appears advising me "Object variable or With block variable not set".
 
Are you asking a question? What line causes the error? Do you understand that at this line...
Code:
  Set Forms!fCalendar.TargetControl = Me.AnyFormTextbox
... you need to replace AnyFormTextbox with the name of your textbox? Is your calendar form called fCalendar?
Cheers,
 
Hi Lagpot,

yes I did change the names of the AnyFormTextbox and fcalendar to the names I have given. The error occurs at the following line on the calendar form when I'm trying to assign the date value from the calendar to the control:

TargetControl = Me.Calendar.Value



Thanks

C
 
I came across this thread while doing a search and saw that it wasn't resolved so I'll add my two cents.

When referring to a control on a form from WITHIN that form's code module, use the Me. qualifier. Therefore
Me.Date1 = Me.Calendar puts a value into Date1 on the currently active form.

Referring to a control on a different form requires full qualification.
Forms![Subform1]![Date1] = Me.Calendar puts a value into Date1 on the form named Subform1. However, once the subform has been embedded onto another form, you need to include its parent form in the reference.
Forms![Mainform]![Subform1]![Date1] = Me.Calendar

If you want to reference a property on a control on a subform, you need even more qualification.
Forms!Mainform]![Subform1].Form![Date1].Visible = False
 

Users who are viewing this thread

Back
Top Bottom