Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox

Status
Not open for further replies.

sk1

Programmer
Oct 10, 2001
44
US
I have two checkboxes (one for pass and other for fail) placed on a vb 6 app form attached to an access 97 database numeric column value 1 or 0..
what i want is when user click's on one of the check boxes the other has to be unchecked.(more work like an option button)
How do I accomplish this.. which event of checkbox can I use. Check_click event seems to be firing when the form loads..

Yes my user wants check boxes and not option buttons that is why I have to use check boxes..
thanks for any help..
 
In the click event of each check box enter code like this

Private Sub chkbox1_Click()
If chkbox1.Value = vbChecked Then
chkbox2.Value = vbUnchecked
End If
end sub

Private Sub chkbox2_Click()
If chkbox2.Value = vbChecked Then
chkbox1.Value = vbUnchecked
End If
end sub

 
sk1,
your question:
check_click event seems to be firing when the form loads...

I had this problem with click events on form load when a record from the database was filled in to the screen. If the record contained a TRUE (1) value for any of the check boxes, VB generates a click event when the data information is filled in for that check box during form load(a check mark gets put in the box).

I created an intVariable that is zero at the start of form load. During the form load event I call a subroutine to fill in the text boxes, etc. from the recordset. A check box_click event checks to see if the variable is zero (use this in both your check box click events). If it is, it ignores the click. At the end of the form load event I set the intVariable to 1. Now when a click event occurs it is from a user and not from data being filled in to the check boxes at form load.

Hope this helps
Best regards,
dartronix4
 
more Easy is


Private Sub chkbox1_Click()
chkbox2.Value = not chkbox1.Value
end sub

Private Sub chkbox2_Click()
chkbox1.Value = not chkbox2.Value
end sub

this will make one checkbox selected everytime

 
dhir25....

If you are reading a datbase at program startup, as I assume sk1 is, when the db checkbox field fills in a true (1) value to the form's checkbox, it will generate a checkbox_click event. We are trying to fill in data from a database where the state of the form's check boxes have been previously saved to the database.

sk1 says that the checkbox_click event keeps firing at form load. My proposal addresses that part of his question.

Thanks for the response
Best regards
dartronix4
 
dhir25
Are you sure about
chkbox2.Value = not chkbox1.Value?
I ask because I get an "Invalid Property Value".
The Values are vbChecked=1 and vbUncheckd=0, not True and False.

Not (1) <> 0, and Not (0) = -1 Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Unfortunately, making programatic assignments to check boxes will always trigger the Click event, and can cause you to bounce indefinately between the two. I would suggest doing the following:

Dim IgnoreEvent as boolean

Sub FormLoad

IgnoreEvent = False

End Sub

Sub chkbox1_click

if (IgnoreEvent = False) then
IgnoreEvent = true
if (chkbox1.value = vbchecked) then
chkbox2.value = vbunchecked
else
chkbox2.value = vbchecked
endif
doevents
IgnoreEvent = false
end if

End sub

Sub chkbox2_click

if (IgnoreEvent = False) then
IgnoreEvent = true
if (chkbox2.value = vbchecked) then
chkbox1.value = vbunchecked
else
chkbox1.value = vbchecked
endif
doevents
IgnoreEvent = false
end if

End Sub

BTW - why not use a Radio Button ?

Good Luck
 
You could Try This, Works for me
Create a control array of checkboxes named chkBox. Place this code in the control array's Click event:
Private Sub chkBox_Click(Index As Integer)
Dim I As Integer
Static ChkBoxChg As Boolean

If ChkBoxChg = False Then 'CheckBox is clicked
ChkBoxChg = True
For I = 0 To chkBox().UBound
chkBox(I).Value = IIf(I = Index, 1, 0)
iMyInteger = Index + 1 ' Captures The Box Checked & Puts it in iMyInteger, use if need to pass index variable
Next I
ChkBoxChg = False 'Reset next click
End If

NOTE: iMyInteger needs to be global
 
Thank you all for great suggestions..
I tried Morningstarr's code and it seems to be working.
In the database I have fields declared as not yes/no field but numeric(integer) 1/0. 1 being fail 0 being pass.
So my code is like this..
I have array of check1 and check2 checkboxes

Private Sub Check1_Click(Index As Integer)
For i = 0 To 90
If Check1(i).Value = vbChecked Then
Check2(i).Value = vbUnchecked
End If
Next i
End Sub

Private Sub Check2_Click(Index As Integer)
For i = 0 To 90
If Check2(i).Value = vbChecked Then
Check1(i).Value = vbUnchecked
End If
Next i

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top