Need to Automatically Edit Access TextBox in SubForm after SetFocus - ms-access

I trying to use TabControls for subforms in an Access Form.
I have a subform embedded on a page of a TabControl. I meed to move from the last data entry field in the Main Form to the first data entry field in the Subform.
When I SetFocus to the Control in the subform, it does not allow editing without clicking on the TextBox. Is there a command for immediately enabling editing?
Private Sub ReadDate_AfterUpdate()
Dim myControl As Control
Set myControl = Forms!Data_Input!BOD_Data_Subform!textBoxToEdit
myControl.SetFocus
End Sub
Thanks in advance for any suggestions.

Two steps required to select a control on a subform from a main form
Do the following in order:
Select the subform control.
Select the appropriate control on the
subform.
Example code
Private Sub ReadDate_AfterUpdate()
Dim myControl As Control
' Select the subform control first
Forms!Data_Input!BOD_Data_Subform.SetFocus
Doevents
' Select the control on the subform.
Forms!Data_Input!BOD_Data_Subform.Form!textBoxToEdit.SetFocus
End Sub
* Edits *
I suspect answer would work if there were not TabControls in Main Form. It is a two-part process but in this case the first step is:
Forms!Data_Input!subDataTabControl.Pages(0).SetFocus
Where subDataTabControl is name of the Tab Control and 0 give page index.

Related

VBA update subform recordsource

Quick and easy one (Probably) for you.
My home form has a lot of sub forms within tab controls. Some of these sub-forms have their own sub-form based off a query and that query has criteria input control on the parent form.
As you can imagine upon loading the home form I would have to enter all the parameters in pop-up boxes as the subforms/queries are also loading.
My work-around for this is setting those query sub-forms recordsource to ""
Then once the user control that applies the where condition is updated it changes their record source to the query.
Only, my code (Below) doesn't work. And After half hour of staring at it I can't figure out why, it produces no errors it just does nothing.
All fields/records just have #NAME?
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = qry_SpkrOnboard
Me.Requery
End Sub
I also tried the following:
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = qry_SpkrOnboard
Me.Refresh
End Sub
Again no results, nothing... All fields/records just have #NAME?
Hopefully I've missed something simple otherwise I'll have to rethink the entire form designs
RecordSource is a string:
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = "qry_SpkrOnboard"
End Sub
To refer to a control on the subform:
Value = Me![SubformControlNAME].Form![txt_EventID].Value

How to partially clear userform in ms access

I have a bound userform with lot of fields in it, I use submit button to let user make entries in shared form. But whenever user clicks on submit button it requery whole form. However, there are around 4-5 fields which should not be cleared on submit button, they should retain the value and rest of the fields should get cleared on every submit button click.
Can this be done in access and how ? below is the simple code is use to submit and requery the record.
Private Sub SaveBtn_Click()
RunCommand acCmdSaveRecord
Me.Requery
Me.DataForm.Requery
End Sub
When you have a "Bound Form" in Access, this means that the Access Form is tied to the underlying data source, which is specified by, and stored in, the "Record Source" property for the form. In the "SaveBtn_Click()" method you provided above, this code does the following things:
RunCommand acCmdSaveRecord - Saves the current record in the form.
Me.Requery - Requeries the entire form.
Me.DataForm.Requery - Requeries the subform named "DataForm" on your main form.
So when you "Requery" the entire form (depending on how the "Record Source" and other form property settings are setup), the requery operation in some cases moves the cursor to the new record in the data source (...its the default settings for the drag and drop form designer tools in later versions of Access), and I suspect that is why you see the form being "cleared" when you call "Requery." A couple of items I would point out, just for clarity:
Note 1: "Requery" actually saves the record, so explicitly calling "RunCommand acCmdSaveRecord" should not be necessary if you're going to call "Requery" too.
Note 2: Depending on how the Record Source (and other) form properties are set, Requery in some cases just saves and refreshes the currently selected record, but it doesn't sound like that is how your form is working (based on what you said above), so I'm assuming that is not the case in your form.
Note 3: The subform will also be requeried when you call "Requery" for the main form, so that line of code may also be redundant here. The reason to call "Me.DataForm.Requery" is if you only want to requery the subform and NOT requery the entire main form.
Note 4: The "DataForm" subform (on your main form) will have a separate data source for it's own "Record Source" property, which is separate from the parent (main) form, so it is important to be aware of that fact, depending on which field values you want to save.
So, that said, there are a couple of options I might suggest, depending on exactly how you want your form to behave:
If you want to keep some of the field values and use those for the NEW RECORD when you hit the requery button, you could always save them off in a variable and then set those controls again after requerying from your variables. Just create a seperate variable for each value you want to save, copy the TextBox control values into each of variables respectively, call requery on the form, and then copy those values back into your TextBox controls after you requery. That code would be something like the following (depending on the exact names of your TextBox controls, I used the fictitious name "txtMyTextBox" for this example):
Private Sub SaveBtn_Click()
Dim vValue1 as Variant
vValue1 = txtMyTextBox
Me.Requery
txtMyTextBox = vValue1
End Sub
Or, if you're just trying to create "Default Values" for certain controls, TextBox controls have a "DefaultValue" property you can set to any value you would like to use for the default. Those can be set from the Property Sheet on the right side of the Access window when the form is opened in Design mode or Layout mode and the TextBox control is selected. (Press F4 to open the Property Sheet if it's not already open).
But, if you really want to Requery and then go back to the same record you were previously on, you could try the following code:
Private Sub SaveBtn_Click()
Dim iRecord as Integer
iRecord = Me.CurrentRecord
Me.Requery
DoCmd.GoToRecord , , acGoTo, iRecord
End Sub
Anyway, I hope this all makes sense and you find it helpful. Please let me know if you have any further questions about this answer.

Access Subform linked to selection in subform doesn't update

I have a masterform that contains four subforms.
The first subform Activity Subform_DatasheetView is directly linked to the masterform Compliance and looks up records based on selections in three dropdowns.
The second subform CommentDatasheet_Subform shows the records associated with the record selected in the first subform.
The third subform Activity Subform_Detail shows details from the selected recrod in the first subform. The fourth subform works the same way for the third subform.
All of these subforms work, in so far as they show the details I want, but only after I manually select each subform and then refresh them.
Question: How can I get my subforms to automatically update/refresh? I have only seen the event option "On click" in the main form.
You can create an event on your subform to have the masterform refresh the other subforms when a record is changed, like so:
Subform_DatasheetView Form Code
Public Event RecordChanged()
Private Sub Form_Current()
RaiseEvent RecordChanged()
End Sub
Compliance Form Code
Dim WithEvents m_Subform_DatasheetView As Form_Subform_DatasheetView
Private Sub Form_Load()
m_SubForm_DatasheetView = Me.Subform_DatasheetView
End Sub
Private Sub m_Subform_DatasheetView_RecordChanged()
CommentDatasheet_Subform.Requery()
Subform_Detail.Requery()
End Sub

Requerying a subform with unbound controls

I have a subform with unbound image controls that are being populated through code. I am using this subform to display search results when a SEARCH button is clicked. The problem is that I cannot refresh/requery the subform to reflect the current search status, except when I close and open again the form (subform) as a standalone/popup.
I tried this but failed to requery:
With Me.F_Person_Thumbnails_control
.SourceObject = Forms.F_Person_Thumbnails
End With
I will appreciate any help.
Joseph
Try accessing your unbound subform from the parent form.
' To Requery
me.mySubForm.form.requery
' To set a TextBox
me.mySubForm.form.controls("MyTextBox").value = "Test"
You can also access fields from your subform using the form.parent object, although I personally consider this a messier approach - since you are generating errors if the subform is opened by itself. Using the above approach lets you re-use the subform in other places in your database, with different logic (if required). Accessing parent object:
Me.MyTextBox.Value = Me.Parent.Form.Controls("IDNumber").Value

MS Access - Write to Table Immediately After Changing Value in Form

Is there a way to make Access write values immediately to a table when changing values in a form? Right now, I have to change a value in the form, and then click off the field onto the subform before the value is written to the corresponding table.
Sounds like no big deal, but certain controls require current data to give valid options. If the user doesn't know enough to click into the subform then the data they view could be out-of-date.
The behavior in your requirement are asking for is by default how access functions. In most typical scenarios your master form is going to be the parent table, and as a result before you can add child records to such a relational setup, then the parent record would have to be written and saved to disk.
And the reverse while not a requirement is also the default operation for access.
In other words it's not clear why when the focus moves from your parent form to a sub form, that the parent form's record is not been written to the table.
So something in your setup is incorrect here, or you'll have to expand on the behavior you're witnessing.
By default changing the focus from the main form to a sub form will cause the main form record to be written to the table, and changing the focus from a sub form tool main form as a general rule should also cause a sub form record to be written to the table.
If you are matching a combo box, you can use that as your link master field, that is, the name of the control itself. Your subform should update as soon as you select a ticket with the combo.
Link Master Fields: Combo1
Link Child Fields: Field1
You can force the save of the record when a user moves off a field in the Lost_Focus event.
Private Sub MyField_LostFocus()
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End Sub
I was also facing the same problem, but in another scenario. I am not having sub-form, but a datasheet view of the same table on a split screen, which was not updating immediately after save button. On pressing DUPLICATE button, instead of getting duplicate data of latest record, I was getting pointer shifted to first record, and then the data from the first record was getting duplicated.
By inserting
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
before data duplicating code, I am now getting the data updated immediately with record pointer stick to the current record, and the same last record gets duplicated as required.
Sharing the experience.
Thanks ChrisPadgham
Just use this in your form model:
Private Sub {your textbox name her}_AfterUpdate()
Me.Refresh
End Sub
Essentially: in the AfterUpdate event of every desired control execute a acCmdSaveRecord command.
So:
In the design view of the form click on your control.
Menu > Design > Property Sheet (to bring up the property sheet for your control)
Property Sheet > Event [tab] > After Update. Choose "[Event Procedure]". Click on the elipsis "..." to take you through to the Code-Behind-Form VBA dev environment.
Supply your code as follows ...
Private Sub [Control Name]_AfterUpdate()
DoCmd.RunCommand acCmdSaveRecord
End Sub
E.g.
Private Sub cboFinancialYear_AfterUpdate()
DoCmd.RunCommand acCmdSaveRecord
End Sub
Repeat the above for every relevant control on the form.
The acCmdSaveRecord avoids an unecessary visual refresh of the current form, as when using Me.Refresh (which #medjahed mohamed has suggested).