MS Access = referencing a control that is within a sub form - ms-access

So I have a form, with a subform control that acts as a navigation scheme for my Main Form. Within the sub form, there is a sub form2 that actually contains the controls for data entry.
So the Main Form is almost just like a design shell around the sub2 within sub1 structure. So in subform1 there is 10 clickable buttons that open 10 different forms in the subform2 window. The subform2 contains the controls.....so here is my issue/question:
So if I have a click event on the subform2 where i am trying to open the next form (subform2 form) within the subform1 child control, which is within the Main Form, what is the vb syntax for handling this?
I am not sure if i am asking this correctly.
I tried:
Forms!SubForm1.MyChild.sourceobject = "NextSubForm2"
but this only works if the Main Form is not open....like if I only had the SubForm1 open.
One more thing, this is code behind the form of the SubForm2 (2 being the sub within the sub within the form).

The main form from a subform's point of view is:
Me.Parent
You can build on this:
Me.Parent.Subform1ControlName.Form.AControl
See also: http://www.mvps.org/access/forms/frm0031.htm
So:
Me.Parent.Subform1ControlName.SourceObject = "SomeForm"

Related

How to pass value from subform to mainform ms access

I have a form with a subform (Datasheet view form). When i click a row in subform, i want pass that row value to text box in main form . In subform even wrote this
Private Sub Form_Click()
MsgBox (Noloan)
End Sub
But i don't know how to pass that subform form_click no_loan to mainform.textbox. Thanks for your help.
Additional data:In my db , i have a table which consist field No,NoLoan,Name. NoLoan Field value as i wanna clicked .From that table i create a subform/datasheet view ,subform name is T_Loan1. Then I create Main Form, name is FindLoan2. In that main/parent form i create text box, called Text7, and put T_Loan1 in subform object at footer.
I put this
=[Forms]![FindLoan2]![subformTLOAN].[Form]![Noloan]
in my Text7 Control and It's work.
Thank's for all attention.
Goto to the subform and click on the field which has the row value
Select Field property - click on events tab --> On Click --> Code Builder
Put below code in the Click event where Form_frmMain is your form name and Me.ID is the field.
Private Sub ID_Click()
Forms!FindLoan2.Text7 = Me.ID
End Sub
The best way is to write code that ignores the forms!names. If you copy the form, or change the sub form, or even drop the sub form into a different form, then all your code breaks.
To reference the textbox in the parent form from code, use:
me.Parent!Text7 = me!id
I am not sure which version of MS Access you are using, but it is also possible to use Tempvars to store values for use elsewhere if you have a complicated application which makes referencing another form/field/control difficult.
To create:
Tempvars.Add "TempVarName", TempvarValue
To use:
Tempvars!TempVarName
To remove:
Tempvars.Remove "TempVarName
I try to limit my use of them, as it becomes hard to manage. I am always careful about when I set the values and I ensure that they are removed after I am finished with them to avoid confusion or issues with values persisting when they shouldn't.

Switch between subforms in Navigation Form

I have been searching for hours on what seems like an easy question, so alas, I will ask here. Working in MS Access with a navigation form, attempting to switch between navigation buttons/tabs in VBA to search for assets I wish to add to the project.
I have a Navigation Form with a number of navigation buttons to include btnUpdateProject and (hidden) btnSearch. btnUpdateProject has a subform named ProjectIndex with a btnSearchForAsset button (so I can search for assets I may wish to add to the project). I have VBA code:
Private Sub btnSearchForAsset_Click()
Me.Parent!btnSearch.Visible = True
Me.Parent!btnSearch.SetFocus
End Sub
The btnSearch becomes visible, per first command; however, the focus remains on btnUpdateProject. How do I get the btnSearchForAsset to be the focus? Much thanks for any help!
First of all you have to navigate to the form page and than setfocus on button.
Me.TabCtl1.Pages(1).SetFocus ; This will set focus on the Navigation page
Me.Parent!btnSearch.Visible = True
Me.Parent!btnSearch.SetFocus

Access Value of a Control

On access, I have a navigation form (Stage) with a dropdown list (Modifiable84). How can I collect the value of these controls in each tabs (tabX, tabY ...) of my navigation form?
I've tried in a variable: X_var = Modifiable84.Value but I've got an error.
If you want to refer to a control on a specific subform, you need to use the subform name. E.g. X_var = Form1.Form.Modifiable84.Value where Form1 is the subform control name, not the form name.
Note that if you're using a navigation control, the form needs to be loaded.

Populate textboxes with value from listbox selection on child form

I realise this question has been asked before and I have tried numerous suggestions, but I guess I am just too much of a novice when it comes to VBA coding.
Here is my scenario:
I have an Access 2007 "Application" with a few forms. On the main form, I have 2 text boxes. For simplicity let's call them textbox1 and textbox2.
textbox1 is used to enter a style code. a button on the main form then opens another in modal/dialog mode and runs a query with the style code as the where clasue. the modal popup gives me a list box which is populated from the database based on the query that was passed. I then need to select one of the products in the list and upon closing the popup, populate textbox2 with the brand (column2 in the list from the popup) number.
Please remember that I am a novice. Any help would be greatly appreciated.
I would put a button on the second form, and in the _Click() function, put something like this
If ListBox1.ListIndex = -1 Then
MsgBox "Nothing Selected!"
Exit Sub
End If
UserForm1.TextBox1.Text = ListBox1.List(ListBox1.ListIndex)
Unload Me
Where ListBox1 is the list box containing the content you need the user to select from, where UserForm1 is the name of the calling form, and TextBox1 is the name.
Explanation
The ListIndex property of the list box returns the index of the selected list item. If nothing is selected, it returns a -1.
To reference an item on one from from another, you reference the form, the object and the property.
In The example I gave, the form is UserForm1, the object is TextBox1 and the Property is Text. While typing, intellisense should auto complete the object and properties once you type the period.

VBA get previous active control from parent form

I am usgin Access 2010 and I have a form that has a sub form, let's call it sform. I want to print the parent form's active control When I hit a button in sform. I tried
debug.print Me.parent.ActiveControl
But it prints the name of the sub form's container control, while I wanted the one that was active before it. Is that even possible?
Thanks
The active control on the parent form would be the sub form. To figure out what control had the focus before the sub form you may have to do something like the following:
Add a text box to the form called txtFieldThatHadLastFocus or whatever and hide it.
Then in the On Lost Focus event of all your controls on the parent form add:
Private Sub txtMyField_LostFocus()
txtFieldThatHadLastFocus.Value = "txtMyField"
End Sub
Then within the sub form you can figure out what control had focus before the sub form via:
Forms!ParentFromName!txtFieldThatHadLastFocus.value