I can't find the right way to reference "Parent" - vba

On my Access main form I have a list box control (named "ActionsTaken") that shows a list of actions taken that are associated with the main form record. These are kept in a separate table linked to the main table. With a button I open a subform to add items to the list. After typing in the text I press a "Done" button which closes the subform with a macro. When the subform closes the deactivate event triggers an event procedure that validates the data, writes it to the Actions Table, and (hopefully) requeries the list control on the main form. Everything works if I use an explicit reference to the control on the main form for the requery, But this subform is called from several different main forms, so I want to refer to the control on the main form using "Parent." The syntax I think should work is:
Me.Parent!ActionsTaken.Requery
When the code executes I get a debug interrupt on the above line, and when I reset the code execution I get a pop up box with "There is no field named 'Me.Parent!ActionsTaken' in the current record." The control is definitely there (remember, an explicit reference to it works).
I suspect I don't understand how "Parent" should be referenced. I've found many other syntaxes with various combinations of dot and bang, and with the "Me" left off. Some give me different error messages, but none work.
I've found a few awkward work-arounds, but I'm really curious as to what's wrong.

A subform is a form sitting on another form (this is the 'parent') and opens when that other form (the 'parent') is opened and therefore is not a subform and does not have a parent.
Pass form name to second opened form via OpenArgs:
DoCmd.OpenArgs "secondformname", , , , , , Me.Name
Then the second form can reference the first:
Forms(Me.OpenArgs).ActionsTaken.Requery
Another approach is to open second form with acDialog which will suspend first form code execution until second form closes.
DoCmd.OpenArgs "secondformname", , , , , acDialog
Me.ActionsTaken.Requery

I have it working, thanks to June and others. I bound the "Child" form to a table even though it's not needed as this lets me indirectly pass the record number (ID) via the OpenForm command. I pass the "Parent" form name to the "Child" (Add New Action subform) with the OpenArgs argument of the OpenForm command:
strFormName = Me.Name
DoCmd.OpenForm "Add New Action subform", , _
"Comment Card ID Query", "[Comment Card].ID=[ID]" _
, , , Me.Name
When I am done with the "Child" I execute the following code:
Dim strParentName As String
strParentName = Me.OpenArgs
' Requery the Actions List Box on the Parent
[Forms](strParentName)!ActionsTaken.Requery
' Close the form
DoCmd.Close
I tried June's suggested syntax: Forms(Me.OpenArgs....) but I got errors from Access in Office 365. Using a string variable worked.
Thanks for all the help!

Related

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.

Open a form to a record selected in a subform

I want to open a form to the record selected in the subform of a different form. I've tried following what I’ve seen in other posts about this, but I still can't get it to work. I think I'm just missing some very basic detail but this is my first Access database and I can't figure out what it is. Thanks in advance for your assistance.
Details:
F_Detail - (This is a Single Form containing details on a project.)
F_List - (This is a Single Form containing a subform and a button.)
subF_List - (This is the subform contained in F_List. This subform is in Datasheet view)
Project_ID - (This is the primary key contained in subF_List and in F_Detail. It is the common criteria between the two. It is Short Text type.)
subF_List displays row after row of projects. F_Detail displays details about a single project at a time. From F_List I want to select a row in subF_List and then click the button to open F_Detail, and F_Detail will be displaying details of the project whose row was selected in subF_List when I pressed the button.
What I Have for the Button:
On Click > Event Procedure
Private Sub ProjectDetailButton_Click()
DoCmd.OpenForm "F_Detail",,,"Project_ID = " & Me.Project_ID
End Sub
Upon clicking the button, I get an error saying, "Compile error: Method or data member not found" and it highlights the .Project_ID at the end of my code.
I don't know what I'm doing wrong and would appreciate any help you can lend. Please let me know if I've left out any needed detail.
Use apostrophe delimiters for text field parameters. Use # for date/time, nothing for number.
"Project_ID = '" & Me.Project_ID & "'"
Me is alias for form or report the code is behind. To reference a field in its RecordSource:
Me!Project_ID
Code on main form referencing field on subform must reference through subform container control. I always name container different from object it holds, such as ctrProjects:
Me.ctrProjects!Project_ID
I name controls different from fields they are bound to, such as tbxProject:
Me.ctrProjects.Form.tbxProject

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.

MS Access: Unbound fields in a subform not showing on a report

I'm having a bit of trouble figuring out how to fix an issue with my MS Access database report output.
First:
There are three forms, one of which is for navigation purposes (HOME) that has two navigation buttons in a navigation control (which link to either of the two other forms) and on subform field I use much like an iframe. There are also two reports, one for each of the other forms.
Second:
Both forms have unbound text fields that need to be printed onto the report output. There is no need for these fields to be saved or put into a table. The values of these forms are printed without issue when the form is isolated (i.e.- modal, form view, etc.). The unbound fields are part of the main body of the form where controlled fields are located.
Only when a form is viewed in the "HOME" form in the subform box/iframe does the report show #Name? instead of their intended values. Also worth noting, the subform does not have the Link Master Fields or Link Child Fields options.
The code used on the print command object/button is as follows and is located on the footer of the non-HOME forms:
Private Sub cmdPrint_Click()
Dim strWhere As String
If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[CustomerID] = " & Me.[CustomerID]
DoCmd.OpenReport "TransferAgreement_EC", acViewPreview, , strWhere
End If
End Sub
Any suggestions? I tried searching here and Googled the issue, but I haven't quite found the solution to my problem. Thanks in advance for your input!
Okay. So, found the solution by just running a whole lot of experiments and this fixed it.
On the report itself, on the data field needs to call to both the navigation form and the subnavigation box, then point to the unbound field. For example, one field in the control source is:
=[Forms]![Home]![NavigationSubform].[Form]![GCSerial_1]
Home = The navigation form
NavigationSubform = The nagivation subform box (what I consider an "iframe")
Form = Just a call to to a form, not specific
GCSerial_1 = The unbound text field
In this way, the report will not print the unbound fields when it is not a subform of the navigation form. For example, by default my two other forms are modal pop ups. When I "form view" them as modal prompts, the unbound fields display #Name? when I click my print command button, which specifies a report (no longer) contingent on a subform and not an isolated form.
THANKS FOR THE HELP AND INSPIRATION! <3

Access 2007: Filtering a report's results using a drop-down box

My question is twofold.
I have around twenty assorted tables in a database. The table layouts are diverse; the one common thread is that all of them have a 'County' field.
I need to set up a series of reports which allow a user to select a county from a drop-down box, triggering the report to run and return only records attached to that particular county.
This is doable at the datasheet level using a filter-by-form, but that's pretty clunky and I have several tables/queries which will need this same county filter.
I may be halfway there with the following:
Create an unbound form.
Add a combo box.
Set the Row Source of the combo box to include the County field.
Set its Bound column to 1.
Set its Column Count property to 2.
Set the Column Width property to 0";1"
Name the Combo Box 'ChooseCounty'.
Add a Command Button to the form.
Code the button's click event as follows:
(Note: To write the code, in Form Design View select the command button. Display the button's property sheet.
Click on the Event tab.
On the On Click line, write:
[Event Procedure]
Click on the little button with the 3 dots that appears on that line.
When the code window opens, the cursor will be flashing between two already existing lines of code.
Between those lines, write the following code.)
Me.Visible = False
Close the Code window.
Name this form 'ChooseCounty'.
In the Query that is the Report's Record Source [County] field
criteria line, write:
forms!ChooseCounty!ChooseCounty
Next, code the Report's Open event:
(Using the same method as described above)
DoCmd.OpenForm "ChooseCounty", , , , , acDialog
Code the report's Close event:
DoCmd.Close acForm, "ChooseCounty"
When ready to run the report, open the report.
The form will open and wait for the selection of the Company.
Click the command button and then report will run.
When the report closes, it will close the form.
I can persuade the report to trigger the form, but only once - I can't seem to figure out where precisely the 'forms!ChooseCounty!ChooseCounty' needs to go. Perhaps someone can clarify or offer a more elegant way to do this?
I need to set up a large meta-report containing sub-reports on all of the tables - and, using the same drop-down 'choose a county' form, I need to have that choice cascade down through all the subreports. I don't have the faintest idea how to go about this. Suggestions welcome!
~ T
You seem to be asking two questions, the last of which is clear to me, but the first is not. The second one is in regard to how to cascade the filter to the subforms. You can do this in one of two ways:
put the form control reference as criterion in the recordsource of each subreport, OR
create a non-visible control on the report that has as it's controlsource "=Forms!ChooseCounty!ChooseCounty". Name that control "CountyFilter". Then, add CountyFilter to the link properties. If, for instance, you are linking the subreports on ID, you'd have:
LinkMaster: ID;CountyFilter
LinkChild: ID;County
(assuming, of course, that ID is your link field for the child reports, and that "County" is the name of the field in the child subreport).
Now, I'm wondering why you would have the County data not just in the parent record but in the child records -- that makes no sense. If you do have it, then the solution above will work.
If you don't, then I don't understand the question, as the whole idea behind subreports is that they are filtered by the parent record, so if the parent record is a person, and you filter by COUNTY, you're only going to get the child records in the subreport for that person, which by definition are already filtered by COUNTY because the parent has been filtered.
As to the earlier question, you write:
I can persuade the report to trigger
the form, but only once - I can't seem
to figure out where precisely the
'forms!ChooseCounty!ChooseCounty'
needs to go
You have two choices:
hardwire the recordsource of the report to use the form control reference, so the WHERE clause of your report would be "WHERE County=Forms!ChooseCounty!ChooseCounty" (and you should set this as a parameter of type text to insure that it gets processed correctly).
the better meethod is to set the recordsource in the report's OnOpen event.
After you open the form as a dialog, you'd have something like this:
Me.Recordsource = "SELECT * FROM MyTable WHERE County='" _
& Forms!ChooseCounty!ChooseCounty & "'"
And immediately after that line, you can close the form, since it's not needed any longer.
You will likely want an OnNoData event for the case where no records are returned. This is usually something simple like:
MsgBox "No records found!"
DoCmd.Close acReport, Me.Name
I hope this answers your questions, but if not, I'm happy to offer more explanation.