Not sure if this is appropriate for the code repository but anyway… here is one solution to a problem I couldn’t find a post relating to, and I thought it might be useful to others, especially novices like me.
*Note -it will modify all the relevant data in the RecordSource of the relevant form, so take care!*
Basically I wanted a checkbox in a form header which would check/uncheck all the “SelectThisRecord” checkboxes on a continuous form.
Here is the code I inserted into the “On Click” property of the “Select/Deselect all” checkbox on the form header (called “TickBox” in the below example):
Private Sub TickBox_Click()
Dim FormName As String
Dim FieldName As String
Dim TickBoxName As String
' Enter the name of the yes/no field to be toggled (in this example it is “SelectThisRecord”:
FieldName = "SelectThisRecord"
' Use VBA to find name of the tick box which will toggle all the FieldName values to "yes" or "no"
TickBoxName = Me.ActiveControl.Name
' Use VBA to find the name of the form
FormName = Me.Name
‘Call a Function which checks/unchecks the relevant boxes
Call TickOnOff(FormName, FieldName, TickBoxName)
End Sub
And here is the TickOnOff Function which is kept in a Module
Function TickOnOff(FormName As String, FieldName As String, TickBoxName As String)
Dim RecordSourceName As String
Dim Switch1 As String
Dim MySql As String
' Find the RecordSource of the continuous forms
RecordSourceName = Forms(FormName).RecordSource
' Find the current status of the controlling 'master' checkbox
Switch1 = Forms(FormName).Controls(TickBoxName).Value
' Create SQL which will update all relevant records to the same value as as the 'master' checkbox
MySql = "UPDATE " + RecordSourceName + " SET " + RecordSourceName + "." + FieldName + " = " + Switch1 + ";"
' Run the SQL
DoCmd.RunSQL MySql
' Update the form to show the new values
Forms(FormName).Controls(FieldName).Requery
End Function
*Note -it will modify all the relevant data in the RecordSource of the relevant form, so take care!*
Basically I wanted a checkbox in a form header which would check/uncheck all the “SelectThisRecord” checkboxes on a continuous form.
Here is the code I inserted into the “On Click” property of the “Select/Deselect all” checkbox on the form header (called “TickBox” in the below example):
Private Sub TickBox_Click()
Dim FormName As String
Dim FieldName As String
Dim TickBoxName As String
' Enter the name of the yes/no field to be toggled (in this example it is “SelectThisRecord”:
FieldName = "SelectThisRecord"
' Use VBA to find name of the tick box which will toggle all the FieldName values to "yes" or "no"
TickBoxName = Me.ActiveControl.Name
' Use VBA to find the name of the form
FormName = Me.Name
‘Call a Function which checks/unchecks the relevant boxes
Call TickOnOff(FormName, FieldName, TickBoxName)
End Sub
And here is the TickOnOff Function which is kept in a Module
Function TickOnOff(FormName As String, FieldName As String, TickBoxName As String)
Dim RecordSourceName As String
Dim Switch1 As String
Dim MySql As String
' Find the RecordSource of the continuous forms
RecordSourceName = Forms(FormName).RecordSource
' Find the current status of the controlling 'master' checkbox
Switch1 = Forms(FormName).Controls(TickBoxName).Value
' Create SQL which will update all relevant records to the same value as as the 'master' checkbox
MySql = "UPDATE " + RecordSourceName + " SET " + RecordSourceName + "." + FieldName + " = " + Switch1 + ";"
' Run the SQL
DoCmd.RunSQL MySql
' Update the form to show the new values
Forms(FormName).Controls(FieldName).Requery
End Function