Basically Im a vba programmer and Iam facing a serious problem with the checkbox controls of the VBA.I have to select and deselect the checkboxes as per user selection. I believe this is a general question for vb.net also.
some sample code
sub chk3_Click()
if userform.chk3.value = true then
userform.chk4.value = true
userform.chk2.value = true
end if
end sub
sub chk4_click()
if userform.chk4.value = true then
userform.chk3.value=true
userform.chk1.value=true
end if
end sub
This is the sample code, I have to turn on the other checkboxes based on user selection of the checkboxes.but the problem Im facing is when it executes the statement
userform.chk4.value = true in the sub chk3_click
it is invoking the sub chk4_click sub and again finding the
userform.chk3.value=true in sub chk4_click(), invoking the sub chk3_click
I am unable to understand how to solve it .
I have tried with various events mousedown, mouseup and change in value also after update but none worked.These events are crashing the tool I dont understand why but they are crashing so I just ignored to use those events.
Finally I have used a flag which is defined globally in the workbook and using the if condition I have did it but its looking to bad style of coding. Could anyone help me with these ?
This is what I have done to resolve the problem . It works but I dont think its good style of programming.
dim i as integer
sub ch3_click()
if i = 0 then
i = 1
if userform.chk3.value=true then
userform.chk4.value =true
userform.chk2.value=true
end if
i = 0
end if
end sub
sub chk4_click
if i = 0 then
i = 1
if userform.chk4.value = true then
userform.chk3.value=true
userform.chk1.value=true
end if
i = 0
end if
end sub
Any help greatly appreciated.
That is actually a very valid way to approach the problem, you just need to have a more descriptive name and type for i, such as
Then change your click event code to something like:
Even better is if your version of VBA supports Try/Finally, which I believe it does, you can make sure the flag is always cleared even if you have errors with the following version of the code: