Removing focus from subForm on a mainForm in Access - ms-access

I have a really frustrating problem. Basically I have a mainForm with 3-4 subForms all in datasheet view. What is a generic way to REMOVE focus from subForm after a record is selected and set it to a control on mainForm?
I tried:
OnCurrent Event:
Me.Parent.Form.SetFocus
Me.Parent.[Control].SetFocus
and it works half the time. The problem is, however, when user selects the record by clicking into some field in the subForm. Then the highlightedfields are either A) the field in the subForm, B) the field in the mainForm. Decided seemingly at random.
How do I CANCEL/REMOVE the focus from the subForm completely before setting the new focus elsewhere?

That would be to move focus to a control on the parent form:
Me.Parent!SomeControl.SetFocus

A workaround for this peculiar problem that solved it for me in the end was to make the field in the subForm look like a hyperlink (ie. user can "click" on it).
Then make an OnClick Event which sets the correct focus, that is:
Public Sub [Field]_Click()
On Error GoTo Goto_Err
'some other code
Me.Parent.[Control].SetFocus
Goto_Exit:
Exit Sub
Goto_Err:
MsgBox Error$
Resume Goto_Exit
End Sub
This workaround is not ideal as the user has to click the field itself, rather than simply move to an another record by pressing up/down arrow.

Related

Hiding the Navigation Pane cause a problem when exiting the database

I just found something strange with MS-Access 2016. I have a database with 2 forms: one hidden, one for a menu. The autoexec will open the hidden form and that one will open the menu form.
I unchecked some options, under Current Database e.g. [Display Navigation Pane] and [Allow Full Menus].
I have an exit button/command on the menu form. On Click, it calls DoCmd.Quit. But before exiting, I am getting an MS-Access window asking for the parameter that is normally when the form open, not when it is closing.
After doing a lot of testing and I mean a lot, I realised that the problem is related to the fact that I am disabling the [Display Navigation Pane]. If I keep the navigation pane, but instead I unchecked all Object Type, then everything works correctly.
So, my question is why disabling the navigation pane can create something like that and is there a way to fix it?
Yes, my solution works, but I don’t like it since users can open and close the navigation pane using the shutter bar; nothing shows but it is still distracting and users can call for support.
The problem is not related to the fact that the first form is hidden, but because it is open. If I add a DoCmd.Close acForm, “F_Parameter” before my DoCmd.Quit then it works.
It is easy to reproduce the problem:
Create a new database
Create two forms:
F_Parameter
F_Menu
Create a Macro named Autoexec:
OpenForm
Form Name: F_Parameter
Window Mode: Hidden
In form F_Parameter create an event On Load
Private Sub Form_Load()
DoCmd.OpenForm “F_Menu”
End Sub
In form F_Menu create a Button named Btn_Exit with an event On Click
Private Sub Btn_Exit_Click()
DoCmd.Quit
End Sub
In form F_Menu create an event On Load
Private Sub Form_Load()
MsgBox “Hello”
End Sub
go File, Options, Current Database and
uncheck Display Navigation Pane and
uncheck Allow Full Menus
Save, exit and open the database:
You will get a “Hello” because the F_Menu form open, now click on the exit and you will get another “Hello”. This second one should not be there because the form should be closing not opening.
I was able to reproduce the issue described. I don't think it has anything to do with the Navigation Pane.
This is happening because while the DoCmd.Quit event is occurring, the hidden F_Parameter form is becoming "unhidden" in order for Access to close it. This causes the F_Parameter Form.Load event to run, which is essentially opening F_Menu, thus causing the Form.Load event on F_Menu to run.
I was able to solve this by first explicitly closing F_Parameter, then quitting:
DoCmd.Close ObjectType:=acForm, ObjectName:="F_Parameter", Save:=acSaveNo
DoCmd.Quit

MS Access 2016: When TabControl tab regains focus, requery listbox

I have a Tab Control element containing 2 tabs: Tab1 and Tab2. Tab2 contains a ListBox element whose RowSource queries the data that gets submitted in Tab1.
Say the user opens this form containing these 2 tabs. They are originally on Tab1. in the Form_Load event for Tab2, it queries the data it needs to populate its ListBox. Now they switch tabs to Tab1. They enter some data and submit a new record to the corresponding table (the same table that the Tab2 ListBox uses as its RowSource). When the user switches back to Tab2, they don't see this newly added record.
What I have done so far is that when the user clicks anywhere in the ListBox, it calls Requery, and then the record will show up. This isn't enough as it's not intuitive for the user to have to click on the listbox to update its contents.
I have tried putting this Requery code in a Form_Load, Form_Activate, GotFocus, but nothing works. I know you can detect the TabControl clicks, but how can I access the forms and the elements that they contain?
Any ideas on how to requery this listbox whenever the tab gains control?
If you want to add code for a specific tab on a tabcontrol, the most convenient way to do that is to write code in the TabCtl_Change event. That occurs when the tab control is changed. E.g.:
Private Sub MyTabCtl_Change()
If MyTabCtl.Pages(MyTabCtl.Value).Name = "Tab2" Then 'The tab control just changed to your page
'Requery that list box
End If
End Sub
Note that, if you're not using a tab control at all, but a navigation control (which it does sound like, tab controls don't necessarily have subforms on them, and don't have Form_Load events, forms do)
You're most likely looking for the On Activate trigger.
Then, in the code just use Form.Requery or Form.Recalc depending on which you're actually looking to do.
Alternatively, you could use the same control the user is using to submit records to trigger a requery. Although, that would require a specific Form to be called into focus.

Control.Dropdown doesn’t work

I have a form with a combo box cboListPeople. The GotFocus event is cboListPeople.Dropdown. For a fraction of a second I think I can see the dropdown but it immediately closes. Focus remains with cboListPeople and when clicked, the dropdown appears as expected. I have tried loading the form at another control thus:
Private Sub Form_Load()
Me.Role.LimitToList = True
DoCmd.GoToControl “OfficerLookup”
cboListPeople.SetFocus
End Sub
The result is the same problem as initially outlined above.
Its hard to tell what's going on from what you've described, but if you're seeing it momentarily, it sounds like you might have a conflict in your code somewhere. Do you have any macros or expression builder events in place? If so, there's a possibility of conflicting between these events and your VBA code.

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.

Access 2007 Split form VBA: acNewRec on open prevents tabbing through form - acts like the first field is not 'selected'

I hope someone can help me out, or at least help figure out a workaround.
I'm using Access 2007's split form feature, and have the code below run on the Form_Open event, as well as after two button_click events. The code works fine when run after the button_click events, but when it runs on the form_open event, it causes problems.
If form is opened and user enters text in the first field, he/she cannot use Tab or mouse to select the next form field. The user is stuck on the first form field until pressing Esc to cancel the data entry. In order to successfully enter data in the first form field when the form is opened, a user must first select another form field, then re-select the first form field then enter text in first form field. After this nonsense, the user CAN select next form field with Tab or mouse. This must be performed once every time the form is launched. The same VBA code on the button_click events works fine.
Noteworthy: When the form is first opened, NONE of the form fields in the datasheet section of the form appear 'Selected'. When a user begins to enter data in the first form field, the 'new record' marker (*) moves to the second row as it should, but the first row does not show the data being input. This behavior is odd.
After performing the clear field, click another field, click back to first field workaround described above, the datasheet shows properly selected fields and Data as it is input.
Any ideas? Is this a bug? Is there an easy workaround, such as performing the field-select workaround via VBA at form open?
Any help is MUCH appreciated.
Code:
DoCmd.ApplyFilter , "([Contractor].[CheckOutStamp] Is Null)"
DoCmd.GoToRecord , "", acNewRec
Link to mdb:
https://docs.google.com/leaf?id=0B-jx09cwIQDsYWM2MzMzMDQtYjUzNi00N2E5LWFjYTktNzFiYWYzMDZiYWU1&hl=en&authkey=CPPmoMEF
Some thoughts:
Try moving it from OnOpen to OnLoad. The events in OnOpen can happen before the data is actually loaded, where as OnLoad happens after that is already done.
Also, you might want to just set the form's Filter property to [Contractor].[CheckOutStamp] Is Null and set the FilterOn to Yes, and set the form to DataEntry, which means it defaults to a new record upon open, and doesn't load any of the older records. Once it's open, you can change the form's edit/add mode to whatever you like.