URGENT: Best Coding Practise  - # 1

Posted on 2006-04-23
Last Modified: 2010-05-01

I have a series of checkboxes on a form - I need to test the value of one of two fields on the click event ... I currently do it as follows :

Private Sub chkFields_Click(Index As Integer)
    Select Case Index
        Case 7
            If chkFields(7).Value = vbChecked Then
               chkFields(8).Value = vbUnchecked
               chkFields(8).Value = vbChecked
            End If
        Case 8
            If chkFields(8).Value = vbChecked Then
               chkFields(7).Value = vbUnchecked
               chkFields(7).Value = vbChecked
            End If
    End Select

End Sub

Is there a simpler (ie shorter coding) way of achieving this ?

What is the best coding practise to achieve the result ?

Question by:dwe0608
    LVL 9

    Expert Comment

    If you wish to test only two check boxes those which you are having, Then try to use Option button(Radio button) with a frame control to group them logically.
    for example you have only 2 options to select 4 a particular problem like sex add a frame and draw 2 option buttons in it. then use it without coding like above. Because user only will be permitted to select either one of the option button from a group without any extra coding.
    LVL 19

    Accepted Solution

    Firstly I agree if you only want to have 1 option selected at a time then an Option Control is a much better route.
    Secondly, by changing the values of the checkboxes in the click() sub you are calling the click() sub again. To demonstrate this add a button to your form and put a message box in the click() sub then in the button put something like myCheck.value = vbChecked. By changing the value its the same as clicking the text box. Thats the first thing to point out. This only applies when you change the value of the check box. So for example changing the value of an unchecked box to unchecked will not cause the click() function to be raised, which makes sense.

    With that being said perhaps you can see how your code could cause a few problems :) Try out your code with some message boxes in there to see jsut exactly how many times it is called when you probably thought in reality it was only being called once and more than likely not even doing what you had expected!

    So with that being said. The only way around your problem is to set some kind of flag to prevent the click() function from being called while you are making changes to other boxes.

    So to keep only 1 check box checked at a time without causing a stack over flow try this:

    Private Sub chkFields_Click(Index As Integer)
    Static noClick As Boolean
    If noClick Then Exit Sub 'check the flag

    noClick = True 'set the falg

    Dim iCount As Integer
    For iCount = chkFields.LBound To chkFields.UBound
       If iCount <> Index Then chkFields(iCount).Value = vbUnchecked
    Next iCount

    noClick = False 'rest the flag so the user can click again
    End Sub


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now