Trouble with SetFocus property and SubForms in Access - vba

I have a MainForm with 4 other subforms on it. These subforms are visible and invisible based on user selection on the main form. The visible/invisible works like a charm but I am having trouble with setting the focus on these subforms. what I am doing:
on change event of a combobox makes a subform visible, then I wanted to set my focus on that form but I kept getting the runtime 2110 error. I noticed this was because all the fields on my main form are required fields and access could not transfer the focus unless these fields were filled up (setfocus worked when all the fields were filled in the main form). Now to fill up all the fields before going to the subform is counterintuitive because its not how the data to be entered will be flowing.
So my question is ----- How do I let my focus transfer to my subform before having to fill out all the required fields in the mainform!?
All help is highly appreciated as always! thanks!

IMHO that is not possible if the main form is bound to a recordsource. Whenever Access moves the focus away from a form it's default behaviour is to save the changes if there are any. It will therefore always want a valid record (i.e. no changes or valid changes).
If the subform record has a recordsource with a foreign key relationship to the recordsource of the main form you will also need the PK of the main form to have been created - which usually is a serial generated on save. If you do not have a record, you do not have an PK to pass on as FK.
Alas, if there is NO relationship between the main form and the subforms (i.e. you don't need the PK), a possibility would be to make the main form unbound and save it's contents via a button click in order to allow users to enter half of the data before switching to the subforms.
Of course you would have to handle the case of users inserting data in the subforms and then not finishing the fields on the main form.

Related

in Access, Bound Combo Boxes do not update unless the form is already Dirty

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.

Ms access database is updated when the subform on Mainform is updated via vba

I have very strange problem I am working with MS access 2013 a application called "failure Analysis system" through which user can enter a different data for different system.
I have cread a main form to enter a data lets say "Tb_Mainform" and in this Mainform I have also one subform called "tb_subform". whenever user change one combox in Mainform the subform is updated using
Me.tb_suform.form.recordsoure = query
Me.tb_subform.requery
until now everything is gud subform is also updated. when i click on subform to navigate, it updated the table with half unfilled data.
How can i prevent this. I am opening the main form by using
DoCmd.OpenForm "tb_Mainform", WindowMode:=acDialog, DataMode:=acFormAdd, OpenArgs:=C_ID
PS: Main form and subform using the same database table.
Below is the link with the same problem as mine. it is 6 year old post but i think after that something has to be change in Access until now.
How do I prevent clicks on a subform causing updates on the main form
I have created an unbound form. It contain subforom where I am saving the records manually in vba.
comment by ChrisPadgham helped me a lot

Ms Access 2007 ComboBox

I am using Access for a quick and dirty (ADP) interface for an SQL (Express 2012) database so data entry can begin before the MVC web app interface is complete.
There is one field I want to be varchar, I would like this field to either allow the user to type in a value or select from a distinct list of values previously used in that field.
I have that part down, but the problem is when it happens, I have to refresh the recordset to see the new item in the list, so if they choose add a new record, then the last item added is not visible in the list.
So I can get the distinct list, populate the box, allow for new entry, and save that to the DB, do I have to write a code behind to repopulate the recordset, do I need to write a code behind to maintain the list paralleled to the recordset, or is there just a property I am missing?
Thanks
(Added screen showing event)
As suggested, using the on current event on the form and the after update solved the problem.
Clicking the form section detail selected the detail sections property page not the form. Selecting the form from the drop down on the property page displayed the events I was suggested to use.
Many thanks to those contributing.
As the first suggestion of this was from Remau, with assist in locating that event from hansup, I will mark remau's post as answer. Thank you to both.
Don't requery the form, just requery the combo. The best bet is probably the current event which will work if more than one person is doing data entry. It will also work if people are editing the table as well as entering data. Events that only fire when a record is added will not pick up changes to the combo contents.
Private Sub Form_Current()
Me.MyCombo.Requery
End Sub

How to edit a record through a form on MS Access?

I'm new to Microsoft Access and I'm having a trouble implementing something.
Basically, I have a form with a combo box, some text boxes, and a command button.
What I want to do is have the user select a record from the combo box, which then populates the textboxes (I've already managed to do this). Once the data has loaded into the textboxes, I want the user to be able to edit the info in the textboxes, then press the command button so that it updates that record in the table.
This looks like the same odd form where you had trouble with delete. It sounds like you are working from the wrong end. Create a form based on a table or query, then add a combobox to find records on the form based on the selection. There are wizards to guide you through. You will then have a standard Access form where everything works as expected. When textboxes are changed, the data will be updated as soon as you move to another record, there is no need for save, it is the default for Access.
If you wish to use non-standard unbound controls, you will need to update with SQL or stored queries.

Hiding columns in a datasheet

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.