How to code a checkbox in VBA Ms Access

Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc used Ask the Experts™
Dear Experts
I still need your expert guidance on the issue below:
(1)      I have form bound to customer table, the type of customers we have range from students, trading customers, hotels or tourist & patients. Since the only difference is in terms of data capturing and the nature of customers in question.

So, we want the same form to be used to across the board BUT with option to select the type of data applicable, for example see below:
•      Company (This may be used for general trading)
•      StudentName (This could be for schools)
•      TouristName (this could be for hotels)
The above three controls must be made optional with a check box so that only customers applicable in that business must be VISIBLE if they are NOT checked.
With the use of VBA how do I use a check box to hide those controls that are not required to be updated depending to the business nature AND unhide after update. The issue here is how to code the checkbox (Option39) vs control name (studentName) etc, for example something in this line.
Afterupdate option39()
Me.StudentName .Visible = False
Exist Sub
End If

Open in new window

The advantage of the suggested system will save us from creating independent tables per each business category. The good news here is that even if I’m sitting at 120 tables, 350 queries, 1 macro & 120 Modules, no single object failed the MS access performance analyser test. If you look at the actual application performance you might think that it has only 4 tables and a few queries, its extremely fast. The application covers the following:

(1)      Schools
(2)      Tourism
(3)      General trading
(4)      Retail trading
(5)      Hospitals and
(6)      insurance
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

The normal solution; Use sub-forms for the different sub-classes of your customers. Just load the necessary sub-form, when the main form is loaded.

The advantage of the suggested system will save us from creating independent tables per each business category.
There is a 99% chance, that this is not a correctly normalized data model.

The model here to chose is the super-sub-class model. You have one main table, which has the common column for all classes and an additional column as discriminator, which marks the class type.

Each sub-class, which has columns, which are not part of the common columns requires a depended table (FK by using the ID and the discriminator column) to store the per class type information.

For each of those sub-classes you create one sub-form, which is then loaded into the main form.
Owner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Will a single customer fall into more than one of those categories?  
1.  If not, I would only have one field "Category" to record the category the customer falls into, not 4 checkboxes.
2   If so, would you display all of the controls which relate to both of those categories?

Generally, when I want to hide (or disable) some elements of a form based on some value(s), I will create a separate subroutine in order to institute this logic, and will call that subroutine in the AfterUpdate event of each of the controls that would cause that hide/reveal operation to fire.  I would also use the Form_Current to call this procedure.

Then, there are several ways you could implement the process:
1.  You could use the Tag property of each control on the form to enter the Category(s) that each control is visible in.  Some examples:
Tag=students, trading customers
Tag= students, trading customers, hotels or tourist & patients

With this technique, you would then loop through all of the controls on the form and compare the Tag property to the values in the Category checkboxes, something like:

Dim bVisible as boolean
For each Ctrl in me.Controls
    if me.chkStudent AND instr(ctrl.tag, "Student") >0 Then
        bVisible = true
    elseif me.chkPatient and instr(ctrl.tag, "Patient") > 0 Then
        bVisible = true
'    elseif
'    ... add more conditions here
        bVisible = false
     ctrl.Visible = bVisible

Open in new window

Open in new window
2.  The other way would be to simply code the visibility of each control, something like:

Private Sub ControlVisibility

     Dim bStudent as boolean
     Dim bPatient  as boolean
     Dim bCompany as boolean

     bStudent = NZ(me.chkStudent, 0)
     bPatient = NZ(me.chkPatient, 0)
     bCompany = NZ(me.chkCompany, 0)

     'these should be visible for students and patients
     me.lblField1.Visible = bStudent OR bPatient
     me.txtField1.Visible = bStudent OR bPatient

     'these should only be visible for students
     me.lblField2.Visible = bStudent
     me.txtField2.Visible = bStudent

     'these should only be visible for companies
     me.lblField2.Visible = bCompany
     me.txtField2.Visible = bCompany

End Sub

Open in new window

Either way you do this, it is time consuming to setup but easier to maintain than having a separate input form for each category.
Neil FlemingConsultant and developer
The sample code you have is basically right. A checkbox has an "_Afterupdate" event.

Code below is adapted from a form I use -- clicking the "expenses" checkbox in the example hides a dropdown called "RateName" and then runs a form filter routine "doFilter" which is elsewhere in the code. To make controls visible again after an update, I would attach the code to do that to the "on current" event for the form itself. Or do it from your "update" button

Private Sub ChExpenses_AfterUpdate()
RateName.Visible = False
End Sub

Open in new window

Does this help?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial