I want to go to a control in a subform in order to be able to enter data.
I can make a DoCmd.OpenForm but then I get an extra copy of the subform on top of the main form. I would rather go directly to the subform. I have tried a lot of options and the following I thought was the most promising
Me.Subform.SetFocus
Me.Subform.Form.mycotrol.SetFocus
DoCmd.GoToControl "mycontrol"
This piece of code does not stop at the Subform to let me enter data. I have also tried Me!Subform with the same result. I have thought of adding a Stop statement, but then I don't know how to resume execution when data has been entered to the subform.
I think there is something I have not understood. Can someone help me out?
Biørn Veirø
VBA is single threaded. It can't work with Access forms this way. You could do it with a modal popup that returns a value, like an InputBox. Access forms don't return a value this way.
If you really want to have a step by step using Access forms then you need to hook in to form events. For this specific case I think the event you want is TextBox.AfterUpdate
https://learn.microsoft.com/en-us/office/vba/api/access.textbox.afterupdate-event
You can have different events on each control object, allowing you to have code that runs after each input.
First set focus to the subform control, then the control in the form:
Me!YourSubformControlName.SetFocus
Me!YourSubformControlName.Form!YourControl.SetFocus
Related
I'm working on a fairly complex Access Database, trying to build forms with custom buttons for working with records. I'm using list boxes to display and navigate through records and all fields for existing records are disabled unless the user presses an edit button. The problem I'm having is that if I press the Edit or Add New button, enabling all of the fields, and then try to change the selection in a combo box, it does not update on the first try.
If I edit a text box first then the combo boxes work fine.
I've determined that the control's beforeUpdate and afterUpdate events are not firing on the first try but the action triggers the form_Dirty event and then it works as expected. I tried setting Me.Dirty = True with the Edit button and that solved the problem but it causes problems with some of my other code and it seems like an unnecessary workaround if only I understood the actual cause of the problem. It also works as expected if I leave the Combo Box unbound, but I am trying to build a template that doesn't require too much work to build new forms off of and I would rather not go that route.
It must have something to do with some bit of code I'm using because I can start a basic form and the combo boxes work fine.
I've started a basic test form and am adding code from my template form bit by bit until the problem arises, but it's a tedious process. Any help would be appreciated.
What am I missing? Is there some way of getting the Combo Box events to fire before the form is dirty?
UPDATE:
I have templates for a basic form, a form with a single subform, and a form with multiple subs in a tab control.
After some more testing I discovered that this problem does not apply to the basic form which has no subforms. This template uses similar code to the others for new, edit, cancel, save and delete buttons and for preventing accidental changes, preventing Form_unload during an edit and so on. The main difference I can think of off the top of my head is that the templates with subforms use class modules and collections to hold various data and pass it between the main form and subforms. Not sure if or how this might relate to combo box functionality.
I built most of this last winter then got too busy over the summer to work on it. Just now picking it up again and I'm having to re-learn a lot of the details of how my code works.
you can try to use Me.CboName.Requery in your After Update event.
I figured it out. I was calling a procedure with a form refresh in it from the Form_Dirty event and for whatever reason the refresh at exactly the wrong time was causing the combo box Before_Update and After_Update events to be skipped and the combo box value to not change. It was also causing text entered into a textbox to overwrite existing text on the first keystroke.
When user updates textbox in subform want textbox in main form to update but I can’t seem to figure out how to call my subform textbox in the main form. I know it must be something simple...I just can’t seem to figure it out.
I tried
Me.subfrmAB.textbox1.Text
I tried
Me.subfrmAB.Form.textbox1.Text
Just not sure how to call it
Edit:
This is what I’m trying to do (on main form Form_Current event)
If IsNull(Me.subfrmAB.Form.Controls(subfrmtxtbx1)) Or IsNull(Me.subfrmAB.Form.Control(subfrmtxtbx2)) Or IsNull(Me.subfrmAB.Form.Control(subfrmtxtbx3))Then
Me.textboxAB.Text = “Phase 1”
End If
But it won’t trigger on the form current event.
I also tried
If IsNull(Me.subfrmAB.Form.Controls(subfrmtxtbx1)) Or IsNull(Me.subfrmAB.Form.Control(subfrmtxtbx2)) Or IsNull(Me.subfrmAB.Form.Control(subfrmtxtbx3))Then
Me.textboxAB.Value= “Phase 1”
End If
But then I get an error stating that Me.textboxAB.Value =0
Try:
Me.subfrmAB.Form.Controls("textbox1").Text
Plus, Andre has also provided a great reference for Forms and Subforms - The Access Web by Dev Ashish. I've used that to track down some of these form/subform reference issues.
MSAccess VBA:
Assume, in an arbitrary form, the focus is set to MyControl on that form.
How to "unset" the focus without giving the focus to another control?
I'm lokking for a code like
MyControl.UnsetFocus
In your circumstance, you probably want to just set focus back to the parent form. This meets the conditions of unsetting focus without giving another control focus, and tabbing or clicking will activate the focus / tab-navigation again from that form.
Here's an example:
Forms![MyForm].SetFocus
Note that per the documentation for SetFocus, if you attempt to SetFocus to a Form with child controls that have Enabled set, this will cause focus to automatically bounce to the first eligible child control per the documentation.
Form.SetFocus method (Access) # Microsoft Docs
The option to give focus to the parent form does work as proposed by meklarian
Alternatively, I recently had to do something similar but I wanted to update a textbox value and simply go back to whatever had focus before. This is another case where something like an "unsetfocus" would be awesome, but unfortunately doesn't exist.
Should you need to do something like this, the following works well
Screen.PreviousControl.SetFocus
I got a form that works amazingly by itself, but the second I attach it to my navigation form. I start to get prompted for user input since this line
[Forms]![frm_addReceiveReportInformation]![cbo_PurchaseOrderID]
no longer works due to the current form becoming subform in the navigation form which was explained in
ACCESS 2010 Navigation Form Query Property
I can't seem to figure a way out of using the !form since I absolutely need to retrieve the ID from a combo box to update another combo box.
I tried multiple ways of using the !forms but I can't seem to wrap my head around how to retrieve my information that I am seeking.
I got the 2 way navigation menu(vertical + horizontal tabs). Anyone got advice or has encounter this problem in the pass, who can direct me down the right path.
To access a field inside a form, which is attached to a navigation tab, you should use the following structure:
[Forms]![YourNavigationTab]![NavigationSubform].[Form]![YourField]
Note: tested in MS Access 2013
In order for queries that contain form references to work, the form must be fully loaded. I think the problem you are experiencing is that the query in the Row Source of the Part Code combo is being evaluated before the form is loaded and therefore you are being asked to input the parameter value.
You can work around this by leaving the Row Source property of the Part Code combo empty until it gets focus for the first time, something like:
Private Sub cboPartCode_GotFocus()
If Len(cboPartCode.RowSource) = 0 Then
cboPartCode.[RowSource] = "Your Query"
cboPartCode.Requery
End If
End Sub
I sometimes just put a button on the navigation form which opens the desired form as a standalone form. It is probably not the neatest solution but it does work.
I am trying to hide specific columns in an Access 2007 split form through code. I need the form to check certain conditions to see whether it needs to display a column or not. I have code in the form's 'Activate' event to hide the column like this:
txtControl.ColumnHidden = True
This code works in the "Open" event, but if I hide the column on Activate, it won't display these changes until I close the form and open it again. I have tried calling the form's refresh, repaint, and requery methods, but this doesn't work. Please help!
Edit: Ideally, I need this event to occur whenever the focus switches to this form.That's why I'm using the Activate event rather than the Open event.
Try setting it in either the form's Current or Load events. You will also probably need to requery the control after setting that property: Me.TextControl.Requery Current is called every time a form's record is changed, the form is repainted or requeried. Load, as its name suggests, is called once, after the form has opened when the form loads its records. These have always been more reliable for me than using Activate, which really has to do with more the focus of the form, not really what you want.
I've had a problem like this before working in Access 2002. I was able to solve the problem with a subform by setting the subform source object equal to itself and then running the requery.
Me.SubForm.SourceObject = Me.SubForm.SourceObject
Me.SubForm.Requery
See if this technique works for your particular situation.