find parent of a subform - vba

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.

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;

MSAccess VBA: Pass values from a subform to a seperate form

I have a form frmDetail that contains several locked fields I want populated only when another form has been filled out, in the name of having some kind of standardization in my data. This other form frmSignOut is used to enter in a date and location that will populate the fields on frmDetail. frmSignOut also contains a subform subULookup that looks up users from a different table using an identifier number. The resulting last name, first name and phone # should also be passed to frmDetail. I also hope to combine first and last name somehow into a Last,First format.
My approach so far has been to open frmSignOut modally with acDialog and I inserted Visible=False into the On_Click event on frmSignOut. Then I try to reference my subform fields and set them to the desired fields on frmDetail. I finish by refreshing and then closing the dialog form.
Private Sub CmdSignOut_Click()
DoCmd.OpenForm("frmSignOut"),,,,,acDialog
If CurrentProject.AllForms("frmSignOut").isLoaded=True Then
Set Forms!frmSignOut!SubULookup.PhoneNbrTxt=Me.ContactNbrTxt
DoCmd.Close (acForm), ("frmSignOut")
Me.Refresh
End If
End Sub
I have only been able to get this far, trying to pull the first field PhoneNbrTxt. I can get frmSignOut to open, I put in data and when I click my 'close' command button I get run-time error 438: Object doesn't support this property or method.
The line highlighted is where I try to reference my subform field. I also have a 'Cancel' button added to frmSignOut that just closes the form, I have no errors there.
Again I'm completely new to Access without much prior experience in anything related, I'd appreciate any knowledge you guys can throw at me.
EDIT: I've been able to successfully pull the value to Me.ContactNbrTxt by adjusting my code to the below.
Me.ContactNbrTxt = Forms!FrmSignOut!SubULookup.Form!PhoneNbrTxt
It looks like the missing part was the Form! right before the control name, along with formatting this correctly and dropping Set.
Im still trying to work out how to combine first and last name before also pulling those to frmDetail, if anyone can help there.

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.

Macro in form stops working after opened with vba

Please help me explain, why this happens, after that the solution should be easy :)
I have two forms, showing different data.
Form_1: there is a combo box (with names in it), where you can choose which company you wanna see, and an after-update macro searches the record (an [ID] field), and shows the information. (To be more complicated, this [ID] field is hidden, and used for subforms, where the actual infos appear.)
Form_2: this is a continuous form, each record is in connection with the companies shown in Form_1, but several record can belong to one company. There is a button for every record to open Form_1 with the information connected to it. The vba code of the button is:
Private Sub Button_Click()
DoCmd.OpenForm "Form_1", , , "[ID] = " & Me![ID]
End Sub
In the code, the same [ID] field is used, as described above: hidden and used for subforms.
Both forms are working as needed, I am happy with them.
But after Form_1 is opened from Form_2 with the button, the combo box remains empty (actually I don't need it to be filled), and if I wanna use it to search for other items, it doesn't work, as if the macro wasn't loaded. The list of names appear, I can click on any of them, but the [ID] field is not refreshed (and of course neither the subforms). I have to close the form, and open it again from the side-list.
Why does the macro stop working?
What should I change, to make it work?
Thanks for your help!
Form1 has the filter turned on to a specific key value, so attempts to find and reposition the form's current record will fail without explicitly resetting the filter.
The Where condition of the OpenForm command does not change the form's Record Source property, nor does it perform a simple search/reposition. Rather, it applies a form filter:
DoCmd.OpenForm "Form_1", , , "[ID] = " & Me![ID]
This state is indicated in several ways
On the Home ribbon (i.e. toolbar): The Toggle Filter button is active... is highlighted by a different color.
The form's navigation bar at the bottom of the form show "Filtered" highlighted with a little funnel icon.
The Access status bar shows "Filtered" on the right near other indicators.
Of course it's possible that all of those indicators are hidden, so you just need to be aware of what each command and parameter does.
Possible solutions:
Form1's ComboBox.AfterUpdate macro should turn off the filter before searching for a new ID value.
Form2's Button_Click event opens the form without applying a filter and instead runs code that does the same thing as the ComboBox.AfterUpdate method--searches and repositions the form's record rather than filtering it.
This can be achieved in multiple ways and is largely beyond the scope of this answer, but a hint is to make a Public method in the Form1 module that performs the search. Both the ComboBox.AfterUpdate method and the other button call that same public method so they have the same behavior.

How to determine which subform has focus

I have a Form that contains several subforms. On one of the subforms, I need to run an update query if you click off to another subform (you finished with the controls, if anything changed it must update). This part works great. The trouble I am having is I don't want to bog everything down by running the update (its a loop of update queries) every time you click on any subform.
If you are on a very specific subform (PageIndex 2) then it should run the updates. Below is the code I have to determine which tab/page/subform I am on. PlanSpecs is the name of the Page.
The problem is that no matter what tab I am current on, if always says "I'm on that subform." Any idea why that might be, or any suggestions as to what I can do instead?
If (Me.PlanSpecs.PageIndex = 2) Then
MsgBox ("I'm on that subform")
Else
MsgBox ("Different Subform, move along")
End If
I don't think you can refer to them by that index, unless they're in a tab control, then you'd have the PageIndex property.
You can however do it like this,
If (Screen.ActiveControl.Parent.Name = "SubFormNameHere") Then
MsgBox ("I'm on that subform")
Else
MsgBox ("Different Subform, move along")
End If
But this code may need additional conditions, for instance if you add a tab control to that subform, then it'll return the name of the TabControl and not the parent(form).
For additional reference