Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALTER TABLE: Add a new boolean column with default value and checkbox

What is the correct Access DDL query to add a Boolean datatype column to a table?

So far I've seen examples like the following...

ALTER TABLE MyTable ADD MyNewColumName BIT

but they do not seem to be 100% correct since

  1. Access does not apply the checkbox control to the newly added column, and
  2. the allowed values for that column seem to be 0 and -1
like image 615
OrElse Avatar asked Mar 09 '11 12:03

OrElse


2 Answers

In access the Yes/No data type is a logical field that can display yes/no, true/false, or on/off. When you look at VBA code the true and false constants are equivalent to -1 and 0.

If you use this field to populate a check box it will function properly.

You may be able to Change your alter statement to use "YESNO" as such:

ALTER TABLE mytable ADD mynewcolumn YESNO

That should give you the desired check box in the access table column.

like image 162
Patrick Avatar answered Sep 27 '22 15:09

Patrick


A DAO example.

''Requires reference to Microsoft DAO 3.6 Object Library
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim db As Database
Dim strSQL As String


Set db = CurrentDb

''Create a table ...
strSQL = "Create Table tblLTD (TheYesNoCheck YesNo, TheYesNoCombo YesNo)"
db.Execute strSQL

''It is now in the table collection, so ...
Set tdf = db.TableDefs("tblLTD")

''Change the way the YesNo fields display.
''A Checkbox
Set fld = tdf.Fields("TheYesNoCheck")
Set prp = fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
fld.Properties.Append prp

''A combobox
Set fld = tdf.Fields("TheYesNoCombo")
Set prp = fld.CreateProperty("DisplayControl", dbInteger, acComboBox)
fld.Properties.Append prp
''We will need a format
Set prp = fld.CreateProperty("Format", dbText, "Yes/No")
fld.Properties.Append prp

From: http://wiki.lessthandot.com/index.php/Add_a_Display_Control_(Checkbox,_Combobox)_to_a_YesNo_Field

like image 37
Fionnuala Avatar answered Sep 27 '22 16:09

Fionnuala