Checkbox - algorithm to convert n entries to a number

R

Rick

My database requires some fields to be multiple choice and I have chosen to
use checkboxes. Rather than create fields in the table to store each
individual checkbox would someone have an algorithm that would generate a
unique number based on the pattern of (true) checkboxes?
Say I have in Sources of Income: 10 checkboxes (such as Employment,
Disability income, Social Assistance, etc) each using a checkbox. If
checkboxes 1, 5,6 and 8 are true I would like to store one number (long
integer) that would be unique to that pattern.
Thank you for your assistance.
.... Rick
 
T

Tim Ferguson

Rather than create fields in the table to store each
individual checkbox would someone have an algorithm that would generate a
unique number based on the pattern of (true) checkboxes?

Question: why? Apart from making yourself a whole load of work, and slowing
down the application, and introducing a large nest for bugs to live in,
what advantage are you looking for?

Method: if you still think it's a good idea, then just allocate a value of
1 to the first one, 2 to the next, then 4, 8, 16 and so on up to 16,384.
You can isolate individual values with (it's best to use an Enum or at
least a set of Global Const values for clarity):

If dwMyBitField AND 32 > 0 Then
' etc etc

and switch them on and off with OR and NOT and so on. But it's still asking
for trouble...

HTH


Tim F
 
J

John Vinson

My database requires some fields to be multiple choice and I have chosen to
use checkboxes. Rather than create fields in the table to store each
individual checkbox would someone have an algorithm that would generate a
unique number based on the pattern of (true) checkboxes?
Say I have in Sources of Income: 10 checkboxes (such as Employment,
Disability income, Social Assistance, etc) each using a checkbox. If
checkboxes 1, 5,6 and 8 are true I would like to store one number (long
integer) that would be unique to that pattern.
Thank you for your assistance.
... Rick

As Tim says, this is a Bad Idea. This kind of bitpacking made sense in
the 1960's but not now.

You have a many to many relationship between People and
SourcesOfIncome. Model it as a many to many relationship, with three
tables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top