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
Related
I'm trying to have a code that would save the last modified date/user in a main form when a change has been made in the record including all the subforms I have.
Currently, This code works perfectly only for records in the main form but not for records in subforms. How would I include the last modified date/user for the subforms?
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
Me.LastModifiedDate = Now
Me.LastModifiedUser = Environ$("username")
End If
End Sub
Do you mean that you want to update the LastModified information on the main form if any subform data changes? If so, I would start by looking at adding similar code to the subform's before update event:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
Me.Parent.LastModifiedDate = Now
Me.Parent.LastModifiedUser = Environ$("username")
End If
End Sub
I haven't tested this though, and you might start getting messages about data having been changed by another user if you update multiple subform records before saving the main form record.
In Access 2016, I have a main form, ClientInfoForm, which contains a subform, EditTransactions_subform. The subform is in datasheet view. When the user clicks a record in the subform, I want to populate fields in the main form. In the subform's Click event, I currently have:
Public Sub Form_Click
Forms![ClientInfoForm]![Amendment] = Forms![ClientInfoForm]![EditTransactions_subform].Form![Amendment]
End Sub
I am not sure if this is the right code to execute what I want. The name of the field is [Amendment] in both the subform and the main form.
Thanks in advance for your help!
Howard
This sounds back-to-front. The normal use of main form/sub-form is to navigate through your records on the main form and for each main record the sub-form will show all related child records. You sound as though you are always showing all your records from the child table in the sub-form and as you navigate through these you want the information in the main form to change.
If this is really what you want to do, and assuming these are bound forms, then you will need to make sure that there is no link between the Main and Sub-Form (on the sub-form properties clear the entries from Link Child Fields and Link Master Fields). The in the On Current event of the sub-form you could try something like this (although it's completely untested):
Dim rst As Recordset
Set rst = Me.Parent.RecordsetClone
rst.FindFirst "Amendment = '" & [Amendment] & "'" 'assumes [Amendment] is String, otherwise lose the surrounding quotes
Me.Parent.Bookmark = rst.Bookmark
rst.Close
Set rst = Nothing
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.
I'm wanting to get a record count of my subform each time it is filtered in some way. The text box containing the record count value will be held on the main form.
The record count method I'm familiar with is:
Trim([FORM/SUBFORM ADDRESS].RecordsetClone.RecordCount)
This works fine where the subform is being filtered by changes to Record Source's SQL via VBA.
More recently though I've added the ability for the user to also filter on the subform columns using command buttons running acCmdFilterMenu, i.e. these type of menus:
I've put the record count method in a module so it could be called globally between form and subform events:
Public Sub InboxCount()
Forms!Home.txtInboxCount = Trim(Forms!Home!tblJob_Search_sub.Form.RecordsetClone.RecordCount)
End Sub
I'm then calling the record count in the subform's OnApplyFilter event:
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
MsgBox "Filter Applied"
InboxCount
End Sub
The test message "Filter Applied" fires so it is running the code in that event, it just seems that the record count method I'm using doesn't work when the subform is filtered using acCmdFilterMenu... it does work when I filter the subform using commands that change the Record Source's SQL though.
How might I get the subform's record count when the subform is filtered using acCmdFilterMenu?
Create a textbox (you can hidde it with Visible=False) in the footer section of the subform and set it's sourceObject to =Count(*)
Let's name it txtRecordCount.
In your parent form, create another textbox that references the previous textbox. Set the SourceObject to =[NameOfSubformControl].Form![txtRecordCount]
I have an Access database with a Form that, once the EmployeeID is double-clicked, then it opens another form that contains a subform with employee information. I obtain the EmployeeID from the original form with this code...
myID = CInt(Me.OpenArgs)
I use this string on my secondary form and the subform contained within, however, it is not picking up the EmployeeID. The main form has this code for the Double-Click event...
Private Sub EmployeeID_DblClick(cancel As Integer)
Dim myID As Variant
myID = Me.EmployeeID
DoCmd.OpenForm "subformEmployeeInfo",,,,,,myID
DoCmd.OpenForm "frm_EmployeeInformation",,,,,,myID
End Sub
When I step through the code I notice that my ID is there on the OpenForm Command but when it switches to the subform code I get an "Invalid Use of Null" error.
With a subform, you can refer to the Parent OpenArgs,:
ID= Me.Parent.OpenArgs
These are the OpenArgs of the form on which the subform resides.