Make Table Yes/No Field

I want to run a make table query that groups on a field called code and then creates a new field which is a Yes/No field as a tick box.

Is this poss??

Here is the code I have so far (very simple I know)

SELECT tblStoreDevOrders.Code, No AS Selected INTO tblCapexCodes
FROM tblStoreDevOrders
GROUP BY tblStoreDevOrders.Code, No;


Carl.
LVL 4
Carl2002Asked:
Who is Participating?
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:

place this codes in a module

CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"
Dim f1 As DAO.Field, pt As DAO.Property
Set f1 = CurrentDb.TableDefs("tblCapexCodes").Fields("ColName")
Set pt = f1.CreateProperty("DisplayControl", dbInteger, acCheckBox)
CurrentDb.TableDefs("tblCapexCodes").Fields("ColName").Properties.Append pt
0
 
Rey Obrero (Capricorn1)Commented:
after running the make table query, you need to alter the table and add/ change the property for the check box

currentdb.execute "Alter table tblCapexCodes Add column ColName dbboolean"

0
 
rockiroadsCommented:
Yes/No is a specific datatype, I dont think u can. I thought it was possible only to create basic types like string, number and date, though I may be wrong.

Only way I can think of is if u run some vba that opens and modifies your table

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Rey Obrero (Capricorn1)Commented:
currentdb.execute "Alter table tblCapexCodes Add column ColName yesno"
0
 
rockiroadsCommented:
ha, well i guess cap has given u the vba answer then that I said u can do. I wasnt gonna bother posting that since the SQL route from cap seemed a much quicker/easier way for you.

Ashes very soon, u gonna watch it?
0
 
Rey Obrero (Capricorn1)Commented:
the sql alone will not make the field a check box, just mere YesNo ,T/F
0
 
Carl2002Author Commented:
CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"

I get a constraint error on this pieve of code.
0
 
Rey Obrero (Capricorn1)Commented:
is tblCapexCodes a local table or linked?
0
 
Carl2002Author Commented:
its a local table it might help if I post the rest as Its a subform

Private Sub Command9_Click()
Dim strSource As String
Dim f1 As DAO.Field, pt As DAO.Property
DoCmd.OpenQuery "QryStoreDevOrders"

With Me.tblCapexCodes_Subform.Form
    strSource = .RecordSource
    .Painting = False
    .RecordSource = ""
    DoCmd.OpenQuery "QryCapexCodes"
    DoCmd.DeleteObject acTable, "tblCapexCodes"
    CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"

Set f1 = CurrentDb.TableDefs("tblCapexCodes").Fields("ColName")
Set pt = f1.CreateProperty("DisplayControl", dbInteger, acCheckBox)
CurrentDb.TableDefs("tblCapexCodes").Fields("ColName").Properties.Append pt
    CurrentDb().Execute "Create tblCapexCodes"
    .RecordSource = strSource
    .Painting = True
End With

DoCmd.SetWarnings False

End Sub
0
 
rockiroadsCommented:
Since the SQL route is probably better, try this


alter table tblCapexCodes alter column Selected YesNo

0
 
rockiroadsCommented:
Carl, u can use DISTINCT in queries also, u dont need to use GROUP BY (info only)

SELECT DISTINCT tblStoreDevOrders.Code, No AS Selected INTO tblCapexCodes
FROM tblStoreDevOrders
0
 
Rey Obrero (Capricorn1)Commented:

With Me.tblCapexCodes_Subform.Form
    strSource = .RecordSource
    .Painting = False
    .RecordSource = ""
    DoCmd.OpenQuery "QryCapexCodes"
    DoCmd.DeleteObject acTable, "tblCapexCodes"    'delete the table

    CurrentDb().Execute "Create tblCapexCodes"       'create the table

    CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"         'alter the table

Set f1 = CurrentDb.TableDefs("tblCapexCodes").Fields("ColName")
Set pt = f1.CreateProperty("DisplayControl", dbInteger, acCheckBox)
CurrentDb.TableDefs("tblCapexCodes").Fields("ColName").Properties.Append pt
   
    .RecordSource = strSource
    .Painting = True
End With
0
 
rockiroadsCommented:
Carl. look at your code

'THIS IS YOUR SELECT QUERY???
    DoCmd.OpenQuery "QryCapexCodes"

'WHY DO U DELETE THE TABLE
    DoCmd.DeleteObject acTable, "tblCapexCodes"

'U CANT UPDATR COLUMNS ON A TABLE THAT DONT EXIST!!!
    CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"

0
 
Rey Obrero (Capricorn1)Commented:
oops

With Me.tblCapexCodes_Subform.Form
    strSource = .RecordSource
    .Painting = False
    .RecordSource = ""
       DoCmd.DeleteObject acTable, "tblCapexCodes"    'delete the table
   
     DoCmd.OpenQuery "QryCapexCodes"     '<<  is this the query that creates the table?


   

    CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"         'alter the table

Set f1 = CurrentDb.TableDefs("tblCapexCodes").Fields("ColName")
Set pt = f1.CreateProperty("DisplayControl", dbInteger, acCheckBox)
CurrentDb.TableDefs("tblCapexCodes").Fields("ColName").Properties.Append pt
   
    .RecordSource = strSource
    .Painting = True
End With
0
 
rockiroadsCommented:
Im guesing its more like


    DoCmd.DeleteObject acTable, "tblCapexCodes"
    DoCmd.OpenQuery "QryCapexCodes"
    CurrentDb.Execute "alter table tblCapexCodes alter column Selected YesNo"

the first time u run it, it may fail since tblCapexCodes dont exist
0
 
Rey Obrero (Capricorn1)Commented:
Private Sub Command9_Click()
Dim strSource As String
Dim f1 As DAO.Field, pt As DAO.Property
DoCmd.OpenQuery "QryStoreDevOrders"     '<<what does this query do?


    For Each tbl In CurrentDb.TableDefs
        If tbl.Name = "tblCapexCodes" Then
            CurrentDb.Execute "Drop table [" & tbl.Name & "]"
        End If
    Next

With Me.tblCapexCodes_Subform.Form
    strSource = .RecordSource
    .Painting = False
    .RecordSource = ""

    DoCmd.OpenQuery "QryCapexCodes"     '<<  is this the query that creates the table?


   

    CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"         'alter the table

Set f1 = CurrentDb.TableDefs("tblCapexCodes").Fields("ColName")
Set pt = f1.CreateProperty("DisplayControl", dbInteger, acCheckBox)
CurrentDb.TableDefs("tblCapexCodes").Fields("ColName").Properties.Append pt
   
    .RecordSource = strSource
    .Painting = True
End With

0
 
Carl2002Author Commented:
 DoCmd.OpenQuery "QryCapexCodes"     '<<  is this the query that creates the table?


YES.
0
 
Carl2002Author Commented:
This code seems to work, however as CAP has previuosly stated it does not change to a checkbox I need a checkbox.


0
 
Carl2002Author Commented:
Private Sub Command9_Click()
Dim strSource As String

DoCmd.OpenQuery "QryStoreDevOrders"

With Me.tblCapexCodes_Subform.Form
    strSource = .RecordSource
    .Painting = False
    .RecordSource = ""
    'DoCmd.OpenQuery "QryCapexCodes"
    DoCmd.DeleteObject acTable, "tblCapexCodes"
   
    'CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"
    CurrentDb().Execute "QryCapexCodes"
    CurrentDb.Execute "alter table tblCapexCodes alter column Selected YesNo"

   
    .RecordSource = strSource
    .Painting = True
End With

DoCmd.SetWarnings False






End Sub
0
 
Rey Obrero (Capricorn1)Commented:
you might as well try it this way

Private Sub Command9_Click()
Dim strSource As String
Dim f1 As DAO.Field, pt As DAO.Property
DoCmd.OpenQuery "QryStoreDevOrders"     '<<what does this query do?


    For Each tbl In CurrentDb.TableDefs
        If tbl.Name = "tblCapexCodes" Then
            CurrentDb.Execute "Drop table [" & tbl.Name & "]"
        End If
    Next

    DoCmd.OpenQuery "QryCapexCodes"     '<<  is this the query that creates the table?
 

    CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"         'alter the table

Set f1 = CurrentDb.TableDefs("tblCapexCodes").Fields("ColName")
Set pt = f1.CreateProperty("DisplayControl", dbInteger, acCheckBox)
CurrentDb.TableDefs("tblCapexCodes").Fields("ColName").Properties.Append pt

With Me.tblCapexCodes_Subform.Form
    strSource = .RecordSource
    .Painting = False
    .RecordSource = ""
   
    .RecordSource = strSource
    .Painting = True
End With

end sub
0
 
rockiroadsCommented:
So Carl, like I mentioned
u is deleting that table son after u created it

so try this way

'Delete existing table
    DoCmd.DeleteObject acTable, "tblCapexCodes"

'Create it
    DoCmd.OpenQuery "QryCapexCodes"

'Alter column selected
    CurrentDb.Execute "alter table tblCapexCodes alter column Selected YesNo"
0
 
rockiroadsCommented:
I posted the SQL quite while back that altered the column Selected, did u not see it?
0
 
Carl2002Author Commented:
Yes I saw it rocki but it did not change to checkbox
0
 
Rey Obrero (Capricorn1)Commented:
Carl2002
try my  post, it will show the check box
0
 
rockiroadsCommented:
Really, I just tried it and it did for me, mind u I ran it manually

I ran the make table script manually then the alter table script manually

let me try the vba route
0
 
rockiroadsCommented:
this is the test code I used (my tables obviously!!!) and it has changed it to a yesno field


    DoCmd.RunSQL "SELECT Transno, No AS Selected INTO tblCarl2002 FROM Transco"
   
    DoCmd.RunSQL "ALTER TABLE tblCarl2002 ALTER COLUMN Selected YesNo"

0
 
Carl2002Author Commented:
Right this works

Private Sub Command9_Click()
Dim strSource As String
Dim f1 As DAO.Field, pt As DAO.Property
DoCmd.OpenQuery "QryStoreDevOrders"     '<<what does this query do?


With Me.tblCapexCodes_Subform.Form
    strSource = .RecordSource
    .Painting = False
    .RecordSource = ""

    DoCmd.OpenQuery "QryCapexCodes"     '<<  is this the query that creates the table?
 

    CurrentDb.Execute "Alter Table tblCapexCodes Add column Selected yesno"         'alter the table

Set f1 = CurrentDb.TableDefs("tblCapexCodes").Fields("Selected")
Set pt = f1.CreateProperty("DisplayControl", dbInteger, acCheckBox)
CurrentDb.TableDefs("tblCapexCodes").Fields("Selected").Properties.Append pt


   
    .RecordSource = strSource
    .Painting = True
End With

End Sub


What is now going wrong is the form based on the tblcapexcodes shows the word No when I want it to show a checkbox that a user can check and uncheck.

Is there a way of displaying this?
0
 
rockiroadsConnect With a Mentor Commented:
so going back to your func, lets see
your earlier code, u had vba to open table (not sql) and that may have something to do with it



Private Sub Command9_Click()

    Dim strSource As String

'You run this query, I dont know what it does though
DoCmd.OpenQuery "QryStoreDevOrders"

With Me.tblCapexCodes_Subform.Form
    strSource = .RecordSource
    .Painting = False
    .RecordSource = ""

'DELETE TABLE IF IT EXISTS - YOU DONT NEED IT REALLY SINCE U HAVE SETWARNINGS OFF
    DoCmd.SetWarnings False

'CREATE QUERY
    DoCmd.OpenQuery "QryCapexCodes"

'ALTER COLUMN    
    DoCmd.RunSQL "ALTER TABLE tblCapexCodes ALTER COLUMN Selected YesNo"

    .RecordSource = strSource
    .Painting = True
End With

DoCmd.SetWarnings False

End Sub
0
 
Rey Obrero (Capricorn1)Commented:
revise your query, drop the field Selected and just add it on the alter table statement

SELECT tblStoreDevOrders.Code INTO tblCapexCodes
FROM tblStoreDevOrders
GROUP BY tblStoreDevOrders.Code;

0
 
Carl2002Author Commented:
Thanks Guys

I do have another question which I'll post separately
0
 
rockiroadsCommented:
Was the initial recordsource of the form pointing to tblCapexCodes


I dont understand why u are recreating the table each time.

Why dont u just create the table tblCapexCodes
and base your form on this

then on your click event, clear contents
eg.

Private Sub Command9_Click()

    'You run this query, I dont know what it does though - do u need it still?
    DoCmd.OpenQuery "QryStoreDevOrders"


'NEW LOOK
    DoCmd.SetWarnings False

'Clear Contents
    DoCmd.RunSQL "DELETE FROM tblCapexCodes"

'Insert new contents
    DoCmd.RunSQL "INSERT INTO tblCapexCodes (Code, Selected) SELECT DISTINCT Code, No FROM tblStoreDevOrders"

    DoCmd.SetWarnings True

End Sub



also change your command button name from command9 to something more meaningful
0
 
Carl2002Author Commented:
Rocki

the form frmcapexcodes is a subform in the main form "frmBootsStoreDevOrders"

I agree I need to go through and tidy up the code but for the time being I'm happy it works and I can get certain people off my back for an hour or two.

command9 is also pretty useless, just like the strike force of leicester city.
0
 
rockiroadsCommented:
Carl, doesnt matter what type of form it is, after seeing your code, its just easier to clear table contents and repopulate. Not sure why u feel a table recreation was necessary.

at least u have a strike force!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.