Solved

Create table column as CheckBox

Posted on 2012-04-12
10
364 Views
Last Modified: 2012-04-12
I have the below which does create the table, but I need to have the "Assign" column show up as a checkbox.  However, I don't seem to be able to get this done with what I have so far.

Public Sub CreateAssignTables()
On Error GoTo ErrorHandler
Dim tdf As DAO.TableDef
Dim pt As DAO.Property

'First create for exitance of tables and delete
If fntDoesObjectExist("tblTEMPAssignVar", "Table") Then DoCmd.DeleteObject acTable, "tblTEMPAssignVar"
If fntDoesObjectExist("tblTEMPAssignCon", "Table") Then DoCmd.DeleteObject acTable, "tblTEMPAssignCon"

'Create each table locally
Set tdf = CurrentDb.CreateTableDef("tblTEMPAssignVar")

With tdf
      ' The CreateField method will set a default Size if one is not specified
      .Fields.Append .CreateField("DeptName", dbText, 50)
      .Fields.Append .CreateField("UserID", dbText, 50)
      .Fields.Append .CreateField("TaskName", dbText, 50)
      .Fields.Append .CreateField("TaskID", dbText, 50)
      .Fields.Append .CreateField("Assign", dbBoolean)
End With

Set pt = tdf.Fields("Assign").CreateProperty("DisplayControl", dbBoolean, acCheckBox)
  tdf.Fields("Assign").Properties.Append pt
 
CurrentDb.TableDefs.Append tdf
CurrentDb.TableDefs.Refresh

Set tdf = Nothing

Exit_ErrorHandler:
    Exit Sub
ErrorHandler:
    MsgBox "Error Number; " & Err.Number & vbCrLf & _
           "Description: " & Err.Description & vbCrLf & _
           "Procedure: CreateAssignTables", vbOKOnly
    Resume Exit_ErrorHandler
End Sub
0
Comment
Question by:ssmith94015
  • 7
  • 3
10 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
change this

Set pt = tdf.Fields("Assign").CreateProperty("DisplayControl", dbBoolean, acCheckBox)

with

Set pt = tdf.Fields("Assign").CreateProperty("DisplayControl", dbInteger, acCheckBox)

or

Set pt = tdf.Fields("Assign").CreateProperty("DisplayControl", dbInteger, 106)
0
 

Author Comment

by:ssmith94015
Comment Utility
I used Set pt = tdf.Fields("Assign").CreateProperty("DisplayControl", dbInteger, acCheckBox)

but it errors out on the next line where I append the property
tdf.Fields("Assign").Properties.Append pt
0
 

Author Comment

by:ssmith94015
Comment Utility
Is there a library reference I should be setting?
0
 

Author Comment

by:ssmith94015
Comment Utility
The error I get is Error Number 3219 Invalid Operation

Sandra
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
use this  codes


'Create each table locally
Set tdf = CurrentDb.CreateTableDef("tblTEMPAssignVar")

With tdf
      ' The CreateField method will set a default Size if one is not specified
      .Fields.Append .CreateField("DeptName", dbText, 50)
      .Fields.Append .CreateField("UserID", dbText, 50)
      .Fields.Append .CreateField("TaskName", dbText, 50)
      .Fields.Append .CreateField("TaskID", dbText, 50)
      .Fields.Append .CreateField("Assign", dbBoolean)
End With

CurrentDb.TableDefs.Append tdf     'moved here

Set pt = tdf.Fields("Assign").CreateProperty("DisplayControl", dbinteger, acCheckBox)
  tdf.Fields("Assign").Properties.Append pt
 
               'CurrentDb.TableDefs.Append tdf ' move up
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:ssmith94015
Comment Utility
Now it stops and the Set pt line.  I don't think this can be done in code no matter what I have found on line to say that it can.  I get an Object invalid or no longer set error.

I have the correct library references and all, but I am getting ready to simply give up.

Sandra
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
don't panic..

try this codes


Public Sub CreateAssignTables()
On Error GoTo ErrorHandler
Dim tdf As DAO.TableDef, db as Dao.database
Dim pt As DAO.Property

'First create for exitance of tables and delete
If fntDoesObjectExist("tblTEMPAssignVar", "Table") Then DoCmd.DeleteObject acTable, "tblTEMPAssignVar"
If fntDoesObjectExist("tblTEMPAssignCon", "Table") Then DoCmd.DeleteObject acTable, "tblTEMPAssignCon"

'Create each table locally

set db=currentdb  
Set tdf =db.CreateTableDef("tblTEMPAssignVar")

With tdf
      ' The CreateField method will set a default Size if one is not specified
      .Fields.Append .CreateField("DeptName", dbText, 50)
      .Fields.Append .CreateField("UserID", dbText, 50)
      .Fields.Append .CreateField("TaskName", dbText, 50)
      .Fields.Append .CreateField("TaskID", dbText, 50)
      .Fields.Append .CreateField("Assign", dbBoolean)
End With

 
db.TableDefs.Append tdf

Set pt = tdf.Fields("Assign").CreateProperty("DisplayControl", dbBoolean, acCheckBox)
  tdf.Fields("Assign").Properties.Append pt

db.TableDefs.Refresh

Set tdf = Nothing
Set db= nothing
Exit_ErrorHandler:
    Exit Sub
ErrorHandler:
    MsgBox "Error Number; " & Err.Number & vbCrLf & _
           "Description: " & Err.Description & vbCrLf & _
           "Procedure: CreateAssignTables", vbOKOnly
    Resume Exit_ErrorHandler
End Sub
0
 

Author Closing Comment

by:ssmith94015
Comment Utility
Got it to work with the below changes.  You gave me the idea for dbinteger and acCheckbox, but I realized thta it was using CreateTableDefs, not just TableDefs in the statement we well as moving it to the correct spot in the code<br /><br /><br />Set pt = CurrentDb.TableDefs("tblTE<wbr />MPAssignVa<wbr />r").Fields<wbr />("Assign")<wbr />.CreatePro<wbr />perty("Dis<wbr />playContro<wbr />l", dbInteger, acCheckBox)<br />        CurrentDb.TableDefs("tblTE<wbr />MPAssignVa<wbr />r").Fields<wbr />("Assign")<wbr />.Propertie<wbr />s.Append pt
0
 

Author Comment

by:ssmith94015
Comment Utility
Yes, I was panicing!  Thank you for your help, this has been a thorn in my side for a good part of the date.

Sandra
0
 

Author Comment

by:ssmith94015
Comment Utility
Below is the final full version of the code that works.

Public Sub CreateAssignTables()
On Error GoTo ErrorHandler
Dim tdf As DAO.TableDef
Dim pt As DAO.Property
Dim dbs As DAO.Database
Set dbs = CurrentDb

'First create for exitance of tables and delete
If fntDoesObjectExist("tblTEMPAssignVar", "Table") Then DoCmd.DeleteObject acTable, "tblTEMPAssignVar"
If fntDoesObjectExist("tblTEMPAssignCon", "Table") Then DoCmd.DeleteObject acTable, "tblTEMPAssignCon"

'Create each table locally - Variable tasks
Set tdf = dbs.CreateTableDef("tblTEMPAssignVar")
With tdf
      ' The CreateField method will set a default Size if one is not specified
      .Fields.Append .CreateField("DeptName", dbText, 50)
      .Fields.Append .CreateField("UserID", dbText, 50)
      .Fields.Append .CreateField("TaskName", dbText, 50)
      .Fields.Append .CreateField("TaskID", dbText, 50)
      .Fields.Append .CreateField("Assign", dbBoolean)
End With
    dbs.TableDefs.Append tdf     'moved here
    dbs.TableDefs.Refresh
Set pt = dbs.TableDefs("tblTEMPAssignVar").Fields("Assign").CreateProperty("DisplayControl", dbInteger, acCheckBox)
         dbs.TableDefs("tblTEMPAssignVar").Fields("Assign").Properties.Append pt
Set tdf = Nothing
   

'Create each table locally - Constant tasks
Set tdf = dbs.CreateTableDef("tblTEMPAssignCon")
With tdf
      ' The CreateField method will set a default Size if one is not specified
      .Fields.Append .CreateField("DeptName", dbText, 50)
      .Fields.Append .CreateField("UserID", dbText, 50)
      .Fields.Append .CreateField("TaskName", dbText, 50)
      .Fields.Append .CreateField("TaskID", dbText, 50)
      .Fields.Append .CreateField("Assign", dbBoolean)
End With
    dbs.TableDefs.Append tdf
    dbs.TableDefs.Refresh
Set pt = dbs.TableDefs("tblTEMPAssignCon").Fields("Assign").CreateProperty("DisplayControl", dbInteger, acCheckBox)
         dbs.TableDefs("tblTEMPAssignCon").Fields("Assign").Properties.Append pt
Set tdf = Nothing


Exit_ErrorHandler:
    Exit Sub
ErrorHandler:
    MsgBox "Error Number; " & Err.Number & vbCrLf & _
           "Description: " & Err.Description & vbCrLf & _
           "Procedure: CreateAssignTables", vbOKOnly
    Resume Exit_ErrorHandler
End Sub
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 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

11 Experts available now in Live!

Get 1:1 Help Now