Surely there is an easier way, Lots of Check Boxes and with same code (1 Viewer)

Chief

Registered User.
Local time
Today, 14:41
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:41
Joined
May 7, 2009
Messages
19,169
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).
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:41
Joined
Feb 28, 2001
Messages
27,001
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.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,245
I solved a somewhat similar problem in my Search Form product, where I have a text box linked to an option group.

In your case, it appears that you have a checkbox and a text box, married to each other, connected as it were by their names being similar. You have a checkbox IFA and a corresponding text box, IFA.

The controls names are identical in that they both contain IFA. You have another one IFC, and I assume you've got IFD IFE etc.

If that's the case and your names are consistently created in the same pattern then you could write some quite simple code which would carry out the operation on all of your checkboxes and text boxes.

I could probably write this code for you. However, I would need to see a copy of your form to check that I'm on the right track.

I would call the procedure from the onClick event of the checkbox by placing the procedures name in the event text box preceded with an equal sign. The reason for doing it this way is because you can quickly add this event to every single checkbox with a multiple select.

When the checkbox on click event is activated, it calls the generic procedure and the generic procedure extracts the name from the selected control and filters out the IFA part, if B part if C part, etc, and then looks for a text box with the same name structure.

You can then write the code that will operate on both the selected checkbox and its associated text box, which will be different in every case.
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,245
I would call the procedure from the onClick event of the checkbox by placing the procedures name in the event text box

See the Nifty Tip "Set the Control Source of a Text Box to a Function" on my blog HERE:-

 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,245
the generic procedure extracts the name from the selected control and filters out the IFA part, if B part if C part, etc, and then looks for a text box with the same name structure.

There's an example of how I associate two controls together to use them like a team or like one integrated control on my website here:-

CallCalled Class Module​

The complete class module is at the bottom of this blog.

This line of code passes the control you clicked on in your case checkbox into a control variable which you can use in your code:-
Code:
Set ctrlActiveControl = Screen.ActiveControl                'Get the Control that is Active on the Form.

This line of code gets the associate control in your case the main control will be a checkbox and the associated control will be a text box
Code:
strAssociateCtrl = fGetAssociateCtrl(prpCallingForm, prpCallingCtrlName, 3)

And this is the function that does that work "fGetAssociateCtrl"
Code:
Private Function fGetAssociateCtrl(frmCallingForm As Form, strActiveCtrlName As String, intPrefixLen As Integer) As String
'Looks for any Control with the same name, makes sure there's only one control, and returns that control.

Dim strNamePart As String
strNamePart = Right(strActiveCtrlName, Len(strActiveCtrlName) - intPrefixLen)   'The active control name without the prefix
                                                                                'Length of prefix determined by:- intPrefixLen
                                                                                'Prefix Removed (Naming Convention)
Dim strFoundControlName As String

Dim Ctrl As Control
Dim X As Integer

    For Each Ctrl In frmCallingForm
        Select Case Ctrl.ControlType
            Case acComboBox, acTextBox ', acLabel ', acCommandButton ', acCheckBox, acListBox, acOptionButton, acOptionGroup, acToggleButton

            If strActiveCtrlName <> Ctrl.Name Then 'Skip if it's the same Control
                If Right(Ctrl.Name, Len(Ctrl.Name) - intPrefixLen) = strNamePart Then
                    X = X + 1
                    strFoundControlName = Ctrl.Name
                End If
            End If

        End Select
    Next Ctrl

Dim strCtrlToUse As String

    Select Case X
        Case Is = 0     'No Associate Control Found
            strCtrlToUse = strActiveCtrlName
'////////// For Testing -
            If prpFlgDevMode Then MsgBox "NO ASS CTRL", , conAppName

        Case Is = 1     'One Associate Control Found
            strCtrlToUse = strFoundControlName
        Case Is > 1     'More than one Associate Control Found
            MsgBox "From Function: fGetAssociateCtrl in the Form: frmExample. More than " & _
            "one Control Found with the Same Name. The Control " & Chr(34) & strActiveCtrlName & Chr(34) & _
            " will be made the Principle Control. Correct the Conflicting names to Continue.", , conAppName

            strCtrlToUse = strActiveCtrlName

        Case Else
            MsgBox "From Function: fGetAssociateCtrl in the Form: frmExample. Unforeseen " & _
            "Error in Case Statement. The Control " & Chr(34) & strActiveCtrlName & Chr(34) & _
            " will be made the Principle Control. Please find the Fault before Continuing.", , conAppName

            strCtrlToUse = strActiveCtrlName

    End Select

fGetAssociateCtrl = strCtrlToUse

End Function      'fGetAssociateCtrl
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,245
If prpFlgDevMode Then MsgBox "NO ASS CTRL", , conAppName

"prpFlgDevMode" --- Is a flag that you can set as the developer so that you can switch on or off the message boxes which help you debug your code. You don't need it in this particular case.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,245
Else Dim iResponse As String iResponse = MsgBox("Are you sure you want to remove the IFA sent date?", vbYesNo)

Revisiting this thread I notice that you have a message box that asks the question "MsgBox("Are you sure you want to remove the IFA sent date?", vbYesNo)" this particular messagebox relates to the control:- "TxtIFA_Sent"... If this control happened to have a label, and the label contained the text:- "IFA Sent Date" then you could use the following code from my Call Called Class Module to extract the caption from the text box label and incorporate it into your message box. You would also be able to provide a message customised to each of the different text boxes.

You need both functions because you need to test to see if the label exists.

Code:
Private Function fHasLabel(oFormPassed As Form, strCtrlName As String) As Boolean
'This function Returns True if the control "Name" entered has an associated label.
'Used in "fGetLabel"

Dim Ctrl As Control

    For Each Ctrl In oFormPassed
      If Ctrl.ControlType = acLabel Then
          If Ctrl.Parent.Name = strCtrlName Then fHasLabel = True
       End If
    Next

End Function      'fHasLabel

Private Function fGetLabel(oFormPassed As Form, oCtrl As Control) As String
'Extract the caption from the Associate Controls label...

        If fHasLabel(oFormPassed, oCtrl.Name) Then
            fGetLabel = oCtrl.Controls(0).Caption
        Else
            fGetLabel = ""
        End If

End Function      'fGetLabel
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,245
Using the code snippets posted in this blog, I've come up with a solution which you can see in the the following YouTube video

 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,245
Reviewing the previous video on checkbox code, I realised there were a couple of omissions, which I hope I've corrected in this extra YouTube video

Simplify Checkbox Code Extra - Nifty Access​

 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:41
Joined
Jul 9, 2003
Messages
16,245
To help me explore this user's problem, I made a sample Form as you can see in the this video:-


It has some labels, checkboxes and textboxes. Looking at it you can see that the checkboxes are so tiny "out of proportion" to the rest of the controls, it looks awful. I have always been irritated by the little tiny checkboxes provided by Microsoft Access. They are practically un-editable, and you cannot make them physically bigger, *%$$, yuck! What can I do about it? An even a more interesting question:- Can I make a product to correct this Microsoft ugliness!

A while back I managed to create an Option Group which utilised command buttons with images on and made a set of very nice looking checkboxes bounded by an Option Group. It got me to thinking, what if I changed this? What if I remove most of the Command Buttons, leaving only one button? This way, I could take advantage of my existing Code and create an Option Group, but with a single button? Thereby upgrading the horrible Microsoft checkbox to one with a nice looking image... Yes I could! By making a couple of minor modifications to my existing code I now have a beautiful Checkbox!!! The "Nifty Checkbox"

Nifty Checkbox - Nifty Access​

 

cheekybuddha

AWF VIP
Local time
Today, 21:41
Joined
Jul 21, 2014
Messages
2,237
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

Top Bottom