How to link dynamically loaded unbounded subforms to main form? - vba

I have a bound main form (FormA) with a combobox on it and two unbound subforms (subfrmA & subfrmB). (Both forms are attached to a table however I want them to load onto the main form where I placed an unbound subform as a placeholder.)
The combobox has two values “a” and “b.” When a is selected I want subfrm A to load onto Form A. When b is selected I want subfrmB to load onto Form A. I think have this part working.
However when I select a record on the main form the associated subforms don’t appear. When I try to link the subforms to the main form an error message appears saying I can’t build a link between unbound forms.
The packageID is the link between the main form and subform and is a hidden field on all forms. Whenever the packageID is automatically updated the psckageID in the subform fields are also updated.
Case”A”
Me.subfrmAB.SourceObject=“FormA
Me.packageDetailsID=Me.subfrmAB.packageDetailsID
Case “B”
Me.subfrmAB.SourceObject=“FormB”
Me.packageDetailsID=Me.subfrmAB.packageDetailsID
EDIT: I ended up creating two subforms subfrmA (Form A) and subfrmB (Form B). Then I linked both to the parent form via the master and child links.
I make one of the subforms visible and the other invisible depending on what the user selects in the combobox of the main form.
Everything works except Form B won’t load, but the container loads. I tried loading Form B separately by itself it still won’t load. I also deleted subfrmA and Form B still doesn’t load.
Here is my edited code:
Select Case Me.Authorization.Text
Case “A”
Me.subfrmA.Visible = True
Me.subfrmB.Visible = False
Me.subfrmA.SourceObject = “Form.A”
Case “B”
Me.subfrmB.Visible = True
Me.subfrmA.Visible = False
Me.subfrmB.SourceObject = “Form.B”
End Select
The only line that doesn’t work is the Me.subfrmB.SourceObject=“Form.B” and really there’s something that’s preventing the form specifically loading. I wrote the same code for Form A and Form B but can’t figure out what’s wrong with Form B.

Can certainly be done. Here is a simple example that works for me.
Main form is bound to table Games. Forms used as subform are Umpires and Teams.
Combobox properties:
ControlSource: UNBOUND
RowSource: Umpires;Plate;UmpID;Teams;HomeTeam;TeamID
RowSourceType: ValueList
BoundColumn: 1
ColumnCount: 3
ColumnWidths: 1.0";0";0"
Code:
Private Sub Combo108_AfterUpdate()
With Me
.ctrAB.SourceObject = .Combo108
.ctrAB.LinkMasterFields = .Combo108.Column(1)
.ctrAB.LinkChildFields = .Combo108.Column(2)
End With
End Sub
You could have "A", "B" for the form names in combobox RowSource and then if both forms have same name key fields, don't need them in RowSource, just hard coded. Not entirely clear what the key field names are. Then code like:
.subfrmAB.SourceObject = "subfrm" & .Combo108
.subfrmAB.LinkMasterFields ="packageDetailsID"
.subfrmAB.LinkChildFields = "packageDetailsID"
If you want to save "A" and "B" to main form record, then bind the combobox to field. Then for subforms to change for each record while navigating main form, also have code in form OnCurrent event.
Something to be aware of when coding interaction between form/subform: subforms load before main form - seems odd but is true.

One option is to create a dummy table with one record and bind the subform to that. But you'd have to read and write all the values with code.

Related

How to Update the Values of a Dropdown in a Subform when the Main Form Changes

I have two forms:
InterviewMaster and InterviewDetail
InterviewDetail opens up as a subform in InterviewMaster and these two forms are linked through a common field called InterviewID
In InterviewDetail I have a textbox called Questiontype as well as combobox called InterviewDropdown.
The data in the dropdown varies based on the data on in the textbox. To make this happen, I have a next button to move to the next question. Whenever I click on next the following runs:
Dim ctlCombo As Control
Set ctlCombo = Forms!InterviewDetail!cmbInterviewDropdown
ctlCombo.Requery
The Row Source setting for my combobox is set to look up the required answers, again this is based on the value as per the textbox:
SELECT [queryAnswerOptions].[Answer] FROM queryAnswerOptions ORDER BY [Answer];
So the options are determined by my query called queryAnswerOptions
So as I cycle through my questions using my next and previous buttons, the dropdown options are updated based on the value of my textbox. This works perfectly when I open the subform from the navigation pane. However, when I open the main form and click on the next button my dropdown does not have any values. I've tried requerying the subform with no luck. I've also tried opening the subform in full screen from my main form but this also does not work. I also don't want to go that route as it does not work well with the overall flow of my form.
Any assistance will be greatly appreciated.
Problem with the query WHERE criteria parameter is when form is installed as a subform, the form reference no longer works because it must use the subform container control name. I always name container control different from the object it holds, such as ctrDetails.
Option is to put SQL statement directly in combobox RowSource instead of basing combobox RowSource on a dynamic parameterized query object - then it will work whether form is standalone or a subform.
SELECT Answer FROM InterviewAnswers WHERE QuestionID = [txtQuestionID] ORDER BY Answer;

MS Access Update Unbound Form with Bound Form

I have three forms in a database. Forms A, B, C. The parent form is A and the child form is B at a one to many relationship. As I put information into Form B I would like to click a button on different records and have Form C which is also located on Form A population with the correct information.
I created a query that controls Form C which reads with the following
[Forms]![frm_A1_SiteInformation]![frm_A1A_OutletInformation].[Form]![outletID]
This is to update the query with the new pass through key when I perform the docmd.Requery command.
This is where I get confused because I cannot get Form C to refresh with the correct information. It just keeps saying the first record.
DoCmd.Requery just requeries whatever object has focus. Could do:
Forms!FormA.FormCsubformcontainerName.SetFocus
DoCmd.Requery
Or just:
Forms!FormA.FormCsubformcontainerName.Requery
Or if you want to requery and put focus on FormC:
Forms!FormA.FormCsubformcontainerName.Form.Requery

find parent of a subform

I need to be able to tell what's opening an object where a control is sitting.
I have a procedure that refreshes comboboxes but I will never know whether the combobox is sitting on a form, a subform or inside another subform (2 levels of subforms)
Now, on opening of the form, I hardcode that info to a hidden control and it's passed to a procedure, but I was wondering if there's a way to do something like this, instead of me hardcoding.
it will be either or, depending on where the combobox is sitting
VMainForm = Me.Parent.Name
VMainForm = Me.Name
VMainForm = Me.Parent.Name '(? not even sure how to do this one,
'this would be where the combo is sitting inside another subform,
' so, there's Main!Subform1#Subform2)
Even better solution would be to get the full path to the subform.
So, if I'm on the 2nd subform, I'd get mainform!subform1!subform2
If I'm on a subform, I'd get mainform!subform1
If I'm on main, I'd get mainform.
And you didn't even try?
VMainFormName = Me.Parent.Name
This will, of course, fail if not a subform. So either catch that error or loop the Forms collection for having the form of the control.

How to reference events on objects in a subform from inside the main form

Imagine a form that, for demontration purposes, contains only a subform and a textbox that need to mirror each other.
I tried to accomplish this by setting the Control Source on the textbox to the value of the field in the subform and this worked to make them mirror each other, but the textbox isn't editable so this is an unsuitable solution.
The next thing I decided to try was using the AfterUpdate event on both controls to run code that sets the value of the other control.
This is easy for the textbox:
'Set value of Notes field on subform whenever value of the corresponding textbox
Private Sub Notes_Textbox_AfterUpdate()
Me.subform.Form![Notes] = Me.Notes_Textbox.Text
End Sub
However, this isn't as simple for the subform field. I don't know how to reference an event on a field in a subform in a way that will still allow me to reference controls outside that subform.
As a demonstration, I need a way to do this:
Private Sub subform_Notes_AfterUpdate()
Me.Notes_Textbox.Text = Me.subform.Form![Notes]
End Sub
I can access the subform fields AfterUpdate event within the scope of the subform but if I do that, then I can't access the Textbox because it is in the main form, not the subform.
So I either need a way to define an event function on a field in a subform from within the scope of the main form, or a way to make the textbox part of the subform while maintaining the subforms ability to open in datasheet view.
Not sure I get the whole problem (how does datasheet view come into play?), but this should do what you want:
(in the subform)
Private Sub Notes_AfterUpdate()
Me.Parent!Notes_Textbox.Value = Me![Notes].Value
End Sub
Edit from comment: By far the easiest solution would be a continuous form instead of a datasheet in a subform.
You can have two textboxes, a one-liner in the Details section, and a large one in the form header or footer. Both have the same control source and are automatically updated when the other one is edited. No code required.
If it must be a subform-datasheet, you can use a similar approach: bind the main form to the same recordsource, and in On Current of the subform, move the main form to the same current record.

pass value within one form in ms-access

hey i am a total noob and only found ways to pass a value between two forms.
this is the form i am working with: form.
in this form (event.form) there is a subform (Fundort_kurz-Subform) which lists data. when i click one row inside the subform the value for "objid" appears in textbox "text501" by "=[Fundort_kurz-Subform].[Form]![Objid]". I automatically want the value from field "text501" to be passed to field "Objid_3" which is meant to write the value into the table.
Not sure what you are doing, but use the OnCurrent event of the subform:
Me.Parent!Objid_3.Value = Me!text501.Value
And do rename your controls to something meaningful.