Solved

Make Table Yes/No Field

Posted on 2006-11-21
33
1,753 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:Carl2002
  • 13
  • 11
  • 9
33 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17986829
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17986840
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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17986853
currentdb.execute "Alter table tblCapexCodes Add column ColName yesno"
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 400 total points
ID: 17986906

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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17986916
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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17986942
the sql alone will not make the field a check box, just mere YesNo ,T/F
0
 
LVL 4

Author Comment

by:Carl2002
ID: 17986944
CurrentDb.Execute "Alter Table tblCapexCodes Add column ColName yesno"

I get a constraint error on this pieve of code.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17986954
is tblCapexCodes a local table or linked?
0
 
LVL 4

Author Comment

by:Carl2002
ID: 17986962
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17986972
Since the SQL route is probably better, try this


alter table tblCapexCodes alter column Selected YesNo

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17986976
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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17986982

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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17986993
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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17987002
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17987008
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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17987048
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Author Comment

by:Carl2002
ID: 17987077
 DoCmd.OpenQuery "QryCapexCodes"     '<<  is this the query that creates the table?


YES.
0
 
LVL 4

Author Comment

by:Carl2002
ID: 17987091
This code seems to work, however as CAP has previuosly stated it does not change to a checkbox I need a checkbox.


0
 
LVL 4

Author Comment

by:Carl2002
ID: 17987095
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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17987102
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17987107
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17987112
I posted the SQL quite while back that altered the column Selected, did u not see it?
0
 
LVL 4

Author Comment

by:Carl2002
ID: 17987116
Yes I saw it rocki but it did not change to checkbox
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17987141
Carl2002
try my  post, it will show the check box
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17987144
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17987159
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
 
LVL 4

Author Comment

by:Carl2002
ID: 17987174
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
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 100 total points
ID: 17987204
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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 17987213
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
 
LVL 4

Author Comment

by:Carl2002
ID: 17987228
Thanks Guys

I do have another question which I'll post separately
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17987255
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
 
LVL 4

Author Comment

by:Carl2002
ID: 17987288
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 17987342
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now