You can hide or display a Microsoft Access subform in various ways, by using an event of the form or a form control, using a command button or using a toggle button.
In many cases, your computer monitor's screen estate may be at a real premium, with many elements and form controls vying for position. In this case, the simplest and most convenient solution may be to hide objects or controls until they are required.
This article shows a simple approach of hiding a Microsoft Access Sub Form if the object is not required.
Here we are showing an example that uses the On Current event of the main form, and the After Update event of a checkbox on the main form to show or hide the subform, dependant on whether the checkbox is checked or not. The example below shows how the subform is visible if the Active Member checkbox is checked:
Main form displaying the Payments subform, if the Active Member checkbox is checked.
To enable us to show or hide the sub form, we need to apply some code to the On Current event of the main form - this will show or hide the sub form if the Active Member checkbox is checked as we scroll through the main form. We will also need to add code to the After Update event of the Active Member checkbox, as this will then show or hide the sub form depending on the selection made in the checkbox.
The code used in the On Current event of the main form is:
Private Sub Form_Current() 'On current event of main form 'Check if Active checkbox is selected 'then show or hide subform If Me.Active = True Then Me.sfrmPaymentsSubform.Visible = True Else Me.sfrmPaymentsSubform.Visible = False End If End Sub
The code used in the After Update event of the checkbox (Active) in the main form is:
Private Sub Active_AfterUpdate() 'After Update of checkbox 'Check if Active checkbox is selected 'then show or hide subform If Me.Active = True Then Me.sfrmPaymentsSubform.Visible = True Else Me.sfrmPaymentsSubform.Visible = False End If End Sub
If we uncheck the Active Member checkbox, the After Update event of the checkbox is fired and the subform's visible property is set to False, and the subform is hidden from display.
Below, you will see the same record in the form as displayed above, however you will note that the Active checkbox is unchecked, meaning that the subform is now hidden:
The main form, now not displaying the subform as the Active checkbox is not checked.
Remember, that you must check both of the events (On Current and After Update), to ensure that the property is set depending on whether it is a new record being displayed or a record that is being added or edited.