Using AfterUpdate event on a control in one subform to requery a control on another subform - ms-access

I have an Access 2010 database set up with using the native Navigation Form interface feature.
Using this very helpful table, I've been able to use vba on multiple ocassions to reference forms throughout my database. However I seem to be having difficulty in this one instance.
On the Navigation form I have a number of main tabs. Clicking on one of the main tabs brings up a number of sub-tabs that have various forms attached to them.
On one of these sub-tabs, I have a main form (main) attached to it. There is a subform (subform1) within that main form containing a control text box (control1). Within subform1 there is another form (subform2) containing an unbound text box (control2) displaying a value that is calculated using a TempVar.
I want to use the AfterUpdate event on control1 to requery subform2 in order to update the calculation in control2, or simply requery control2.
Using Refresh and/or Requery on the AfterUpdate event for control1 does not do anything to control2.
Currently I have a button on subform2 that users can click which refreshes the form and updates the value in control2. The user can also hit F5 and refresh all the forms with the same result.
My goal is to take away the manual aspect and have the AfterUpdate event for control1 to update control2.
Any help would be much appreciated as I haven't been able to find anything online that works and I've been kicking tires at the problem for over a month now.
**** UPDATE **** I did not correctly describe the layout of my forms. subform1 and subform2 are actually both nested under main. subform2 is NOT nested under subform1 as I originally described above.

Wow. Now I feel like an idiot. Tackling my problem again with the update I provided above allowed me to solve it.
All I needed to do was requery subform2 by referencing it this way:
[Forms]![HOME]![NavigationSubform].[Form]![subform2].[Form].Requery
Where "HOME" is the name of my Navigation Form.
For anyone with the same issue that hasn't renamed their Navigation Form I believe the syntax would be:
[Forms]![Navigation Form]![NavigationSubform].[Form]![subform2].[Form].Requery
Thank you to #Erik von Asmuth and #June7 for your patience and help.

Related

MS-Access 2007 - How to programatically access sub form columns data on click event

I have an unbound form that includes a subform.
The subform is unbound and gets populated when the user clicks on a push button on the main form.
I want to be able to grammatically handle the click even on the sub form and get the data in a specific column. How can I do that? The same thing one would do with VB.NET/C#.NET if you know what I mean.
When I use the properties tab of the subform, I get an expression builder. That does not get me into a sub/function/form or module VBA code editor.
Any help is appreciated.
Edit - Something that worked!
Thanks for the help I got from the answers below.
One way to refer to column in a selected row in a subform is by using this expression:
Me!ChildFormName.Form!ColumnNameInSubForm
EX:
ME!Sales.Form!SalesmanID
Additional Reference here...
A problem with this approach is that the available events On Enter and On Exit don't behave like "click" event does. One needs to focus out of the sub form (by clicking on another control) for either to be triggered!
Look again. The Properties' sheet has a tab, Events. Select any event and select "Event Procedure" from the dropdown and click the ellipsis - that opens the code editor.
Refer to sub-form control form main-form event handler (VBA Sub):
Me!Subform1.Form!ControlName
Me is self reference to the main-form, Subform1 is the control containing the sub-form, Form is a reference to the sub-form, and ControlName is a reference to the field on the sub-form. ! is a short way to refer to a control in a form's contrls collection.
A longer way to write the above would be: Me.contrls("Subform1").Form.Contrls("ControlName")
Refer to a main-form control form a sub-form event handler (VBA Sub):
Me.Parent!ControlName
Me is self reference to the sub-form, Parent is a reference to the main-form, and ControlName is a reference to the field on the main-form.
A longer way to write the above would be: Me.Parent.Contrls("ControlName")
Please see more on the topic in this link.

Setting focus on a sub form

I have two sub forms embedded in control (the one that looks like a set of folders allowing you to change from tab to tab.. Sorry I don't know the name in English)..
Both sub forms are embedded on one sheet, I would like that immediately after executing a routine, I wrote on one of the subforms, that the set focus then goes to the other sub form.
For some strange reason I can't get it to work. The sub form and field I'm trying to go to are frmObjectives03 and Effective_date.
I tried:
Forms!frmObjectives03!Effective_date.Form.SetFocus
but it doesn't work.
I would appreciate any insight.
Thank you,
The control you speak of is a Tab control.
Because frmOjective03 is opened as a subform, it is not part of the Forms collection. Instead you can use Forms!MainForm!frmObjectives03.Form!Effective_date.SetFocus from anywhere, or Me!frmObjectives03.Form!Effective_date.SetFocus from within the main form or Me.Parent.Form!frmObjectives03.Form!Effective_date.SetFocus from the sibling subform.
If these are not working for you, please check the name of the subform control in your main form (because it may be Child17 instead of frmOjective03). Open the main form in design view, click on the subform ONCE and look in the Properties->Other->name.

Access 2010 - What Control Name do I use to Requery this Subform?

I am now learning MS Access and I have run into a problem. I have found similar questions but after trying them I still can't figure this out.
I have added a button on a form that updates a table behind a subform and I now need that subform to display the new data. Can anyone tell me what to use as the Control Name or if I'm even on the right track to get what I want?
My main form is "EnterEmployeeSales" and my subform is "RetailSalesSubform".
Here are some screen shots of what I'm trying to do:
EDIT: I figured it out. I was making it TOO COMPLICATED!
I kept trying to enter stuff like "Forms!Yadda!Yadda" but all I needed to enter was the subform control's name only into the Requery "Control Name" field on my screen shot above. Imagine that! Here is a screenshot on how to determine the subform control's name (for other loser noobs like me):
There are two parts to a subform, the subform control and the form contained. It is important to use the name of the subform control to requery, not the name of the form contained. So:
Forms!MyMainForm!MySubformControlName.Form.Requery
Or when writing code in the form module:
Me.MySubformControlName.Form.Requery
An advatage of using Me in the form module is that intellisense will give you the name of the subform control.
More information: http://access.mvps.org/access/forms/frm0031.htm
The macro is one way to do it. VBA can also do this. In your case in the "onclick" event you could put the code:
Docmd.Requery "ServiceSalesSubform"
It does the same thing but sometimes it is nice to have everyting in VBA code. When there is a mix of code and macros it can get confusing to tell what is happening and when.

MS Access: How do forms communicate values to each other?

I have a form (FORM-A) that requires the user to select a vehicle.
The user should click on a button on FORM-A that say's select vehicle.
A selection form (FORM-B) should open where the user can select a vehicle.
The selected value should be communicated back to FORM-A.
How would you accomplish this in MS Access 2010?
FORM-B is a continuous form that contains a picture of the vehicle and some other information.
From what I understand from your question, you want formB to open a kind of pop-up. When the pop-up is closed, its result is put somehere in the calling form.
Solution proposal:
a) open FormB using syntax docmd.openform "formB", windowmode:=acDialog.
This will prevent execution of the next lines until formB is closed or hidden.
b) in the OK button of FormB, just hide the form, do not close it.
c) when code resumes in formA, you can now
check if formB is still open. If not, it's been cancelled
read the value in hidden formB (still open), then close formB
Otherwise, you could also have formB to update a control in formA before closing. But I don't like that approach because then formB is not reusable, and it creates an unnecessary dependency between formB and formA.
I am not sure why you would need a separate form for this - just have the first textbox be a listing of all the records of vehicles in the database, where you would select one, and the rest of the vehicle information is auto-populated from the vehicle table, but not copied into your parent table. Of course, I am not sure of your table structure either, so there might be a reason for this method that isn't apparent to me.
The benefits of the method above is that if you add more vehicles, your selection box is automatically updated - and you keep the forms you have to load to a minimum (always a good performance move)
You can reference the forms in this manner forms!formName!controlName.
Once you see how this works you will be able to fool with it to get it working with your existing setup. Let’s use 3 controls a text box on Form-A, an image on Form-B and a text box on Form-B. The text box on Form-A will be named txtVehicle, the image on Form-B will be named imgVehicle and the text box on Form-B will be named txtVehicleName.
You can set the name of a control within properties. When you click on imgVehicle it will put the value from txtVehicleName into txtVehicle.
You will have to do a little coding - it's easy though if you have not done it before. Under properties for the image you will see events. If you click on the "On Click" event you will get a drop down list. One of the choices will be [Event Procedure] - choose that. A little button with 3 dots on it will also show up at the end of the row. Click it and you should be taken to a code window with some code like this in it.
Private Sub imgVehicle_Click()
End Sub
Here is where you put your code. Something like this should work. This is it in its most simplistic form.
Private Sub imgVehicle_Click()
Forms!Form-A!txtVehicle=forms!Form-B!txtVehicleName
End Sub
Now although that will work, there are a few things that we should be doing in this method that we are not. We should reference Form-B directly since we are in it, we should verify that Form-A is in fact open.
Private Sub imgVehicle_Click()
If currentproject.allforms(“Form-A”).isloaded then
Forms!Form-A!txtVehicle=me!txtVehicleName
End if
End Sub
Hope that helps
You can create an instance of formB within formA and control it. Below is the VBA code for formA. When clicking on a button on formA, you create a new instance of formB and give it focus. At the same time, you can set properties for its controls. You can use this approach to set the right picture in your control on form B. Hope this helps.
Example:
Option Compare Database
Dim fB As Form_FormB
Private Sub btnA_Click()
Set fB = New Form_FormB
fB.SetFocus
fB.tbxB.Text = "Some text sent from A to B!"
End Sub
If you want both forms to be visible all the time, I suggest using a subform with the list of all the vehicles or just details for the one that the user selected.

How to get Access 2007 to run VBA subroutines when scrolling through records in a form

I have a series of "on Change" Enabled/Disabled subroutines in the VBA for a form.
When scrolling through the form to view records however, only the main routine runs--the subroutines don't. As a result, questions appear disbaled that have responses displayed.
How do I get the subroutines to run when a record is viewed/changed?
The OnChange only fires when the relevant object is updated. If you want code to run whenever the record is scrolled, put it in the form's OnCurrent event.
I too have found Access form events to be a bit hard to work with/unpredictable. One thing that worked for me is to use a label or textbox and set the Control Source to a formula or a call to my own VBA function to get what I need done. Your control could also be invisible if it is not needed for the GUI. I doubt this is best practice but this "simulated" event worked for me.
If you need conditional presentation in a list, a continuous form and events are not going to do the job. In some cases CONDITIONAL FORMATTING will do the job, but only for appearance -- it can't do things like show/hide controls or alter the rowsource of dropdown lists or requery them.
Conceptually it would be impossible to do so. Form-level events fire for the record. Scrolling through the form does not move the record pointer.
I avoid using continuous forms for editing precisely because of this issue. Instead, I bind a detail subform to the PK value of the record selected in the continuous form, and then the detail form can do all the conditional formatting it needs to.