VBA get previous active control from parent form - ms-access

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

Related

Toggle visibility of other fields on form based upon combobox selection - MS Access

Question...
How can I toggle the visibility of several other fields (checkboxes/textboxes) on form based upon the selection of a combobox item. The image below shows a listbox but either way, how do use vba code to turn on or off visibility of all the fields in the grey box. Basically, if the combobox selection is scheduled then visible=true. Else visible=false How can I code this???
Use combobox AfterUpdate event and probably form Current event as well. So build a procedure that can be called from both events, something like:
Sub Form_Current()
SetVisible
End Sub
Sub cbo1_AfterUpdate()
SetVisible
End Sub
Sub SetVisible()
Me.tbx1.Visible = Me.cbo1 = "scheduled"
Me.cbx1.Visible = Me.cbo1 = "scheduled"
End Sub
Alternative is to use Conditional Formatting for textboxes and comboboxes (sorry, not applicable to other controls) to Enable/Disable as well as set colors so appear not visible.

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.

Set focus to specific field in Access 2010

How do I set the focus on a particular field (in this case, the "Ref:" field which is located in the Form Header) when viewing a form in Form View?
You could use SetFocus in Form_Active
Private Sub Form_Activate()
yourField.SetFocus
End Sub
or you could change the order tab and put your field into first place of the list

MS Access = referencing a control that is within a sub form

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"

Access 2007 Setting Field to Selection in Dialog

I would like to set a field to a value selected from a grid in a dialog. I am using Access 2007.
In WinForms I would:
Create a child form
create a grid for the data
add a property for the selected item
In the parent form
add a button to open the form
on successful dialog result get the selected item from the property
update the object
persist on event
On parent edit
set selected value in child grid
Is something like this possible in Access 2007 forms? I have a Multiple Item form with the child records. Can I select one and return that to the parent? And on the other side, can I default the selected item on edit?
How do people approach this in Access?
Here is a pattern that works assuming the child form is modal.
In your parent form
Private Sub cmdOpenChild_Click()
DoCmd.OpenForm "ChildDialog", acNormal, , , , acDialog, "Info for child"
'This line will block further code execution until child form is hidden or closed.
MsgBox Forms.Item("ChildDialog").Controls.Item("SomePropertyOrControl").Value
DoCmd.Close acForm, "ChildDialog"
end sub
In the Child form have a close button that actually only hides the form.
Private Sub cmdClose_Click()
'hide the form instead of closing it to return control to caller.
Me.Visible = False
End sub