URGENT: Best Coding Practise - # 1

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 ?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.