Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

URGENT: Best Coding Practise  - # 1

Posted on 2006-04-23
2
Medium Priority
?
154 Views
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
            Else
               chkFields(8).Value = vbChecked
            End If
        Case 8
            If chkFields(8).Value = vbChecked Then
               chkFields(7).Value = vbUnchecked
            Else
               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 ?

MTIA
0
Comment
Question by:dwe0608
2 Comments
 
LVL 9

Expert Comment

by:pradapkumar
ID: 16518478
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.
0
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 1500 total points
ID: 16518766
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



0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

580 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