Hi, You will find, attached to this message, a tutorial on how to manage binary value or controls. Let say that you have an array of 100 checkboxes on a form and each checkbox value is stored in a database. Instead of having 100 fileds, with this solution, you could have only one field.
Hopefully, that will be useful to you. Here is the Text version:
Assume you have a form with 4 checkboxes, one label, one textbox and 2 buttonsControl Name / Control Type
chkTest(1) / CheckBox
chkTest(2) / CheckBox
chkTest(3) / CheckBox
chkTest(4) / CheckBox
lblValue / Label
txtValue / TextBox
cmdDecode / Command Button
cmdExit / Command ButtonCODE
‘Each time a checkbox is selected or unselected, calculate the base 2 value
Private Sub chkTest_Click(Index AS Integer)
Dim iCount AS Integer ‘Generic Counter
‘Browse each checkboxes
For iCount = 1 TO chkTest.Count
If chkTest(iCount).Value = vbChecked Then
‘The box is selected, calculate the Base 2 value
lblValue.Caption = lblValue.Caption + (2^iCount)
End If
Next iCount
End Sub
‘Decode the value provided in the textbox
Private Sub cmdDecode_Click()
Dim iCount AS Integer ‘Generic Counter
Dim iValue AS Integer ‘Hold the value to decode
‘Insure a zero value in the label caption
lblValue.Caption = “0”
‘If the value in the textbox is invalid, exit the sub, this is a simple check,
‘you should not the the user input alpha in the textbox
If txtValue.Text=”” OR Val(txtValue.Text)<0 Then Exit Sub
‘Assing the value
iValue = Val(txtValue.Text)
‘Browse each checkboxes (make sure all boxes are unselected)
For iCount = 1 TO chkTest.Count
chkTest(iCount).Value = vbUnchecked
Next iCount
‘Browse each checkboxes in reverse order
For iCount = chkTest.Count To 1 Step –1
‘If the difference between the current iValue and the 2^iCount is positive
If iValue - (2 ^ iCount) >= 0 Then
‘The current box index was selected
chkTest (iCount).Value = vbChecked
‘Remove the current ‘base 2 power’ from iValue
iValue = iValue - (2 ^ iCount)
End If
Next iCount
End Sub
‘Quit the application
Private Sub cmdExit_Click()
Dim sMsg As String
sMsg = “Do you really want to quit the application?”
If Msgbox(sMsg, vbQuestion + vbYesNo,”Base 2 Example”) = vbYes Then
End
End If
End Sub
This post has been edited by Nevermalchik: 24 Aug, 2006 - 10:36 AM