Surely there is an easier way, Lots of Check Boxes and with same code

Chief

Registered User.
Local time
Yesterday, 16:09
Joined
Feb 22, 2012
Messages
156
Hello,
Looking for an easier and cleaner way to put in some code.
If there's not, never mind ill keep copy and pasting. :-(

I have a form, multiple check boxes. when ticked puts in date in text box.
when unticked asks if your sure then removes date and tick.

I have sooooooo many Checkboxes that require this function.
Is there an easier/cleaner way to code these?

thank you
example of code

Code:
'------------------------------------------------------------
' Check Boxes - Fill in dates and remove dates
'
'------------------------------------------------------------
Private Sub ChkIFA_Sent_Click()

    If ChkIFA_Sent = True Then
        Form_JobDetailF.TxtIFA_Sent = Date
        TxtIFA_Sent.Enabled = False
    Else
        Dim iResponse As String
            iResponse = MsgBox("Are you sure you want to remove the IFA sent date?", vbYesNo)
        Select Case iResponse
        Case vbYes:
            Form_JobDetailF.TxtIFA_Sent = ""
            TxtIFA_Sent.Enabled = True
        Case vbNo:
            ChkIFA_Sent = True
        End Select
    End If
End Sub

Private Sub ChkIFC_Complete_Click()

    If ChkIFC_Sent = True Then
        Form_JobDetailF.TxtIFC_Complete = Date
        TxtIFC_Complete.Enabled = False
    Else
        Dim iResponse As String
            iResponse = MsgBox("Are you sure you want to remove the IFC complete date?", vbYesNo)
        Select Case iResponse
        Case vbYes:
            Form_JobDetailF.TxtIFC_Complete = ""
            TxtIFC_Complete.Enabled = True
        Case vbNo:
            ChkIFC_Sent = True
        End Select
    End If
End Sub

Private Sub ChkSample_Sent_Click()

    If ChkSample_Sent = True Then
        Form_JobDetailF.TxtSample_Sent = Date
        TxtSample_Sent.Enabled = False
    Else
        Dim iResponse As String
            iResponse = MsgBox("Are you sure you want to remove the Sample sent date?", vbYesNo)
        Select Case iResponse
        Case vbYes:
            Form_JobDetailF.TxtSample_Sent = ""
            TxtSample_Sent.Enabled = True
        Case vbNo:
            ChkSample_Sent = True
        End Select
    End If
End Sub

And also in the Form Current

Code:
Private Sub Form_Current()
    'Checking CheckBoxes for true/false and dates
    If ChkIFA_Sent = True Then
        Form_JobDetailF.TxtIFA_Sent = Date
        TxtIFA_Sent.Enabled = False
    Else
        Form_JobDetailF.TxtIFA_Sent = ""
        TxtIFA_Sent.Enabled = True
    End If
    '_______________________________________________
    If ChkIFC_Sent = True Then
        Form_JobDetailF.TxtIFC_Complete = Date
        TxtIFC_Complete.Enabled = False
    Else
        Form_JobDetailF.TxtIFC_Complete = ""
        TxtIFC_Complete.Enabled = True
    End If
    '_______________________________________________
    If ChkSample_Sent = True Then
        Form_JobDetailF.TxtSample_Sent = Date
        TxtSample_Sent.Enabled = False
    Else
        Form_JobDetailF.TxtSample_Sent = ""
        TxtSample_Sent.Enabled = True
    End If
    '_______________________________________________
    
    End Sub
 
create a function in your form same as below.
remove all the click event code, instead
directly put this in the Click Event of each
CheckBox, example for checkbox ChkSample:

=CommonClick([ChkSample], [TxtSample_Sent], [Form_JobDetailF].[TxtIFC_Complete], "Are you sure you want to remove the IFA sent date?")

Code:
Public Function CommonClick( _
                ByRef TheCheckbox As CheckBox, _
                ByRef TextOnThisForm As TextBox, _
                ByRef TextOnAnotherForm As TextBox, _
                ByVal Message As String)
    If (TheCheckbox) Then
        TextOnAnotherForm = Date
        TextOnThisForm.Enabled = False
        Exit Function
    End If
    If MsgBox(Message, vbQuestion + vbYesNo) = vbYes Then
        TextOnAnotherForm = Null
        TextOnThisForm.Enabled = True
    Else
        TheCheckbox = True
    End If
End Function
you can even create a Class (it will only confuse you).
 
My question is whether you can reduce the whole process by making an abstract pattern and calling it as a subroutine.

Code:
Public Sub Common_Check( ckbX as Access.CheckBox, dtmY as Access.TextBox, strName as String )
    Dim iResponse As String
    If ckbX = True Then 
        dtmY = Date
        dtmY.Enabled = False
    Else
        iResponse = MsgBox("Are you sure you want to remove the " & strName & "?", vbYesNo)
        Select Case iResponse
        Case vbYes:
            dtmY = ""
            dtmY.Enabled = True
        Case vbNo:
            ' no action is required here
        End Select
    End If
End Sub

Then from each click routine, you have a potential one-liner:

Code:
    Common_Check  ChkFA_Sent, Form_JobDetail.TxtFA_Sent

Less typing required. Put that sub in a regular module (particularly if you have this kind of thing on more than one form.) Call it something else if you want a more descriptive name, but as far as typing issues, you could call it FRED for all that matters.

EDIT: Looks like Arnel beat me to it by a second or two.
 
Have a look at this similar thread.

It shows a method of wiring up your common click procedure to all the pertinent controls in the form's load event which means you don't have to write out the call in each of the individual control's click event handler.
 

Users who are viewing this thread

Back
Top Bottom