- Local time
- Today, 23:23
- Joined
- Feb 19, 2013
- Messages
- 17,005
I needed a means to quickly view boolean values in a combo or listbox (I didn't want to use a subform). However since these lists are text, checkboxes cannot be shown, instead you get true/false.
However with a small modification to the recordsource, you can get checkboxes
Better still, with a couple of lines of code you can make the listbox updateable (works for a combo as well, but the dropdown functionality gets in the way). Not so easy to demonstrate with images, please see the attached db
the rowsource to the first example is simply
for the second example we make use of the chrW and iif functions
the same rowsource for the third example, but code in the listbox afterupdate event to manage updates
as an alternate to the checkbox, just use a tick and leave the false's blank
Note that for booleans, 0 is false, any other number is true (not just -1). so you might create your boolean with a comparison or using an exists subquery (although these would not be updateable). For example in relation to showing whether an invoice has been created you could use an exists subquery to display a tick or cross rather than displaying the invoice number or date. Or indicate that membership has been paid (or not)
edit: small change to the last listbox to make it updateable
However with a small modification to the recordsource, you can get checkboxes
Better still, with a couple of lines of code you can make the listbox updateable (works for a combo as well, but the dropdown functionality gets in the way). Not so easy to demonstrate with images, please see the attached db
the rowsource to the first example is simply
Code:
SELECT PK, fName, fSelected FROM Table1
for the second example we make use of the chrW and iif functions
Code:
SELECT PK, fName, chrw( iif(fSelected,9745,9949)) AS Selected FROM Table1
the same rowsource for the third example, but code in the listbox afterupdate event to manage updates
Code:
Private Sub UDListbox_AfterUpdate()
Const sqlStr = "UPDATE Table1 SET fSelected = Not [fSelected] WHERE PK="
With UDListbox
CurrentDb.Execute sqlStr & .ItemData(.ListIndex - .ColumnHeads)
.Requery
End With
End Sub
as an alternate to the checkbox, just use a tick and leave the false's blank
Note that for booleans, 0 is false, any other number is true (not just -1). so you might create your boolean with a comparison or using an exists subquery (although these would not be updateable). For example in relation to showing whether an invoice has been created you could use an exists subquery to display a tick or cross rather than displaying the invoice number or date. Or indicate that membership has been paid (or not)
edit: small change to the last listbox to make it updateable
Attachments
Last edited: