Creating A Table With More Properties Than Fieldname, Length, Type | Member | | Join Date: Apr 2007
Posts: 72
| |
I have a database, where I need to create a table if it doesn't exist. This is my code at the moment: -
Sub CreateCwS()
-
-
Dim daotbl As DAO.TableDef
-
-
If Not daotbl.Name = tblCwS Then
-
-
DoCmd.SetWarnings False
-
-
Dim daodb As DAO.Database
-
Dim daotblCwS As DAO.TableDef
-
Dim daofldCNum As DAO.Field
-
Dim daofldSNum As DAO.Field
-
Dim daofldCCanEnt As DAO.Field
-
Dim daoiCNum As DAO.Index
-
Dim daoiSNum As DAO.Index
-
Dim daoiCCanEnt As DAO.Index
-
-
Set daodb = CurrentDb()
-
Set daotblCwS = daodb.CreateTableDef("tblCwS")
-
-
Set daofldCNum = daotblCwS.CreateField("CNum", dbLong)
-
Set daofldSNum = daotblCwS.CreateField("SNum", dbText, 5)
-
Set daofldCCanEnt = daotblCwS.CreateField("CCanEnt", dbLong)
-
-
daotblCwS.Fields.Append daofldCNum
-
daotblCwS.Fields.Append daofldSNum
-
daotblCwS.Fields.Append daofldCCanEnt
-
daodb.TableDefs.Append daotblCwS
-
-
DoCmd.RunSQL "INSERT INTO tblCwS SELECT tblCen.CNum, tblSub.SNum FROM tblCen, tblSub;"
-
-
End If
-
-
End Sub
-
That code does create a table, but it only creates a table with field names, lengths, and types. How do I create a table with more field properties set using VBA? In Visual FoxPro, there is the possibility of using the following code, made using with the help of this site: -
CREATE TABLE
-
tblCwS (CNum n (5 , 0)
-
NOT NULL
-
CHECK Between 10000 And 79999 And Len([CNum])=5
-
ERROR Must be 5 digits Long, lie between 10000 And 79999, And unique
-
DEFAULT 10000
-
How can I get the same result using VBA? Or, how can I use foxpro in access? And how do I create a caption for a table using code?
Thanks in advance,
George
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,886
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
Once the table has been created close the recordset and then reopen as follows: -
Dim idxTbl As Index
-
Dim idxFld As Field
-
-
Set daotblCwS = daodb.CreateTableDef("tblCwS")
-
-
'set the tables primary key
-
Set idxTbl = daotblCwS.CreateIndex("PrimaryKey")
-
idxTbl.Primary = -1
-
idxTbl.Unique = -1
-
Set idxFld = idxTbl.CreateField("CNum")
-
idxTbl.Fields.Append idxFld
-
daotblCwS.Indexes.Append idxTbl
The following is sample code which will give examples of how to set the properties on the fields. -
'Format a single field to have two decimal places
-
Set fld = tbl.Fields("MyNumberField")
-
Set fFormat = fld.CreateProperty("Format", dbText, "Fixed")
-
fld.Properties.Append fFormat
-
Set fFormat = fld.CreateProperty("DecimalPlaces", dbByte, 2)
-
fld.Properties.Append fFormat
-
-
'Format the datetime field to be a medium time
-
Set fld = tbl.Fields("MyDateField")
-
Set fFormat = fld.CreateProperty("Format", dbText, "Medium Time")
-
fld.Properties.Append fFormat
-
| | Member | | Join Date: Apr 2007
Posts: 72
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
That is exactly what I want! I found an alternative method in SQL and was just about to post it but your way is equally good...do you know of any websites that give a list of the property values accepted by access? are they all in the help documentation provided?
Using Access '03 on xp
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,886
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type Quote:
Originally Posted by smiler2505 That is exactly what I want! I found an alternative method in SQL and was just about to post it but your way is equally good...do you know of any websites that give a list of the property values accepted by access? are they all in the help documentation provided?
Using Access '03 on xp Don't know one off-hand but have a look at the properties list in table design. This would be a good guideline. Leave the spaces between the words out (e.g. "DefaultValue").
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,886
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
Sorry just noticed an error in my previous code, replace - Set daotblCwS = daodb.CreateTableDef("tblCwS")
with - Set daotblCwS = daodb.TableDef("tblCwS")
| | Member | | Join Date: Apr 2007
Posts: 72
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
How do I open and close record sets?
(I thought it was -
Dim rstCwS As RecordSet
-
Set rstCwS = daodb.daotblCwS
-
rstCwS.Close
-
rstCwS.OpenRecordset
-
but it's not)
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,886
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
Try this ... -
Dim rstCwS As RecordSet
-
Set rstCwS = daodb.OpenRecordset("daotblCwS")
-
-
' you procedures here
-
-
rstCwS.OpenRecordset
-
Have a look at this basic tutorial:
Access VBA DAO recordset loop using two recordsets |  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,219
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type Quote:
Originally Posted by smiler2505 I have a database, where I need to create a table if it doesn't exist. This is my code at the moment: -
Sub CreateCwS()
-
-
Dim daotbl As DAO.TableDef
-
-
If Not daotbl.Name = tblCwS Then
-
-
DoCmd.SetWarnings False
-
-
Dim daodb As DAO.Database
-
Dim daotblCwS As DAO.TableDef
-
Dim daofldCNum As DAO.Field
-
Dim daofldSNum As DAO.Field
-
Dim daofldCCanEnt As DAO.Field
-
Dim daoiCNum As DAO.Index
-
Dim daoiSNum As DAO.Index
-
Dim daoiCCanEnt As DAO.Index
-
-
Set daodb = CurrentDb()
-
Set daotblCwS = daodb.CreateTableDef("tblCwS")
-
-
Set daofldCNum = daotblCwS.CreateField("CNum", dbLong)
-
Set daofldSNum = daotblCwS.CreateField("SNum", dbText, 5)
-
Set daofldCCanEnt = daotblCwS.CreateField("CCanEnt", dbLong)
-
-
daotblCwS.Fields.Append daofldCNum
-
daotblCwS.Fields.Append daofldSNum
-
daotblCwS.Fields.Append daofldCCanEnt
-
daodb.TableDefs.Append daotblCwS
-
-
DoCmd.RunSQL "INSERT INTO tblCwS SELECT tblCen.CNum, tblSub.SNum FROM tblCen, tblSub;"
-
-
End If
-
-
End Sub
-
That code does create a table, but it only creates a table with field names, lengths, and types. How do I create a table with more field properties set using VBA? In Visual FoxPro, there is the possibility of using the following code, made using with the help of this site: -
CREATE TABLE
-
tblCwS (CNum n (5 , 0)
-
NOT NULL
-
CHECK Between 10000 And 79999 And Len([CNum])=5
-
ERROR Must be 5 digits Long, lie between 10000 And 79999, And unique
-
DEFAULT 10000
-
How can I get the same result using VBA? Or, how can I use foxpro in access? And how do I create a caption for a table using code?
Thanks in advance,
George If you are creating Tables, Fields, Indexes, Primary Keys, and other Database Schema, you should definately consider using ADOX (Microsoft ADO Extensions For DDL and Security). This technology was designed specifically for programmatically creating and manipulating Objects, especially when it comes to Objects relating to your Database's Security and Structure. It contains additional Objects that work with the core ADO Objects. Just thought that this would be a good point to mention here.
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,886
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type Quote:
Originally Posted by ADezii If you are creating Tables, Fields, Indexes, Primary Keys, and other Database Schema, you should definately consider using ADOX (Microsoft ADO Extensions For DDL and Security). This technology was designed specifically for programmatically creating and manipulating Objects, especially when it comes to Objects relating to your Database's Security and Structure. It contains additional Objects that work with the core ADO Objects. Just thought that this would be a good point to mention here. Not a bad suggestion.
You will find the syntax for ADOX code on Allen Brownes website. http://allenbrowne.com/func-ADOX.html#CreateTableAdox | | Member | | Join Date: Apr 2007
Posts: 72
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
Thanks I'll have a look at that
The property method given earlier works great, but I'm getting an error when setting the primary key -
Dim daodb As DAO.Database
-
Dim daotdfCwS As DAO.TableDef
-
Dim tdfCwS As TableDef
-
Dim daofldCNum As DAO.Field
-
Dim daofldSNum As DAO.Field
-
Dim daofldCCanEnt As DAO.Field
-
Dim idxPK As Index
-
Dim fldPK As Field
-
Dim fldCNum As Field
-
Dim fldSNum As Field
-
Dim fldCCanEnt As Field
-
Dim fmtCNum As Property
-
Dim fmtSNum As Property
-
Dim fmtCCanEnt As Property
-
Dim rstCwS As DAO.Recordset
-
-
Set daodb = CurrentDb()
-
Set daotdfCwS = daodb.CreateTableDef("tblCwS")
-
-
Set daofldCNum = daotdfCwS.CreateField("CNum", dbLong)
-
Set daofldSNum = daotdfCwS.CreateField("SNum", dbText, 5)
-
Set daofldCCanEnt = daotdfCwS.CreateField("CCanEnt", dbLong)
-
-
daotdfCwS.Fields.Append daofldCNum
-
daotdfCwS.Fields.Append daofldSNum
-
daotdfCwS.Fields.Append daofldCCanEnt
-
daodb.TableDefs.Append daotdfCwS
-
-
Set rstCwS = daodb.OpenRecordset("tblCwS")
-
-
rstCwS.OpenRecordset
-
-
Set tdfCwS = daodb.TableDefs("tblCwS")
-
Set idxPK = tdfCwS.CreateIndex("PrimaryKey")
-
-
idxPK.Primary = -1
-
idxPK.Unique = -1
-
-
Set fldPK = idxPK.CreateField("CNum")
-
-
idxPK.Fields.Append fldPK
-
tdfCwS.Indexes.Append idxPK
-
In the last line given here, it stops with the Run-time error 3211 (The database engine could not lock 'tblCwS' because it is already in use by another person or process.)
How can I fix that?
|  | Administrator | | Join Date: Aug 2006 Location: Dublin, Ireland
Posts: 10,886
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
Thanks I'll have a look at that
You need to close the previous tabledef and don't open the recordset. See the following: -
Dim daodb As DAO.Database
-
Dim daotdfCwS As DAO.TableDef
-
Dim tdfCwS As TableDef
-
Dim daofldCNum As DAO.Field
-
Dim daofldSNum As DAO.Field
-
Dim daofldCCanEnt As DAO.Field
-
Dim idxPK As Index
-
Dim fldPK As Field
-
Dim fldCNum As Field
-
Dim fldSNum As Field
-
Dim fldCCanEnt As Field
-
Dim fmtCNum As Property
-
Dim fmtSNum As Property
-
Dim fmtCCanEnt As Property
-
Dim rstCwS As DAO.Recordset
-
-
Set daodb = CurrentDb()
-
Set daotdfCwS = daodb.CreateTableDef("tblCwS")
-
-
Set daofldCNum = daotdfCwS.CreateField("CNum", dbLong)
-
Set daofldSNum = daotdfCwS.CreateField("SNum", dbText, 5)
-
Set daofldCCanEnt = daotdfCwS.CreateField("CCanEnt", dbLong)
-
-
daotdfCwS.Fields.Append daofldCNum
-
daotdfCwS.Fields.Append daofldSNum
-
daotdfCwS.Fields.Append daofldCCanEnt
-
daodb.TableDefs.Append daotdfCwS
-
-
daotdfCwS.Close
-
-
Set tdfCwS = daodb.TableDefs("tblCwS")
-
Set idxPK = tdfCwS.CreateIndex("PrimaryKey")
-
idxPK.Primary = -1
-
idxPK.Unique = -1
-
Set fldPK = idxPK.CreateField("CNum")
-
idxPK.Fields.Append fldPK
-
tdfCwS.Indexes.Append idxPK
-
-
tdfCwS.Close
-
| | Member | | Join Date: Apr 2007
Posts: 72
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
there was no close property for tabledef's, but I deleted the record set and it works great!
| | Member | | Join Date: Apr 2007
Posts: 72
| | | re: Creating A Table With More Properties Than Fieldname, Length, Type
Thanks for the help. I've made the code to make the table (shown below), but I'm not sure how to check if a table exists. I'm going to start a new thread about that. -
Sub CreateCwS()
-
-
DoCmd.SetWarnings False
-
-
Dim daodb As DAO.Database
-
Dim daotdfCwS As DAO.TableDef
-
Dim daofldCNum As DAO.Field
-
Dim daofldSNum As DAO.Field
-
Dim daofldCCanEnt As DAO.Field
-
Dim daoidxCNum As DAO.Index
-
Dim daoidxSNum As DAO.Index
-
Dim daoidxCCanEnt As DAO.Index
-
Dim daofldiCNum As DAO.Field
-
Dim daofldiSNum As DAO.Field
-
Dim daofldiCCanEnt As DAO.Field
-
Dim fldCNum As Field
-
Dim fldSNum As Field
-
Dim fldCCanEnt As Field
-
Dim fmtCNum As Property
-
Dim fmtSNum As Property
-
Dim fmtCCanEnt As Property
-
-
Set daodb = CurrentDb()
-
Set daotdfCwS = daodb.CreateTableDef("tblCwS")
-
-
-
-
Set daofldCNum = daotdfCwS.CreateField("CNum", dbLong)
-
-
daofldCNum.DefaultValue = "10000"
-
daofldCNum.ValidationRule = "Between 10000 And 79999 And Len([CNum])=5"
-
daofldCNum.ValidationText = "Must be 5 digits long, lie between 10000 and 79999, and unique"
-
daofldCNum.Required = True
-
-
-
Set daofldSNum = daotdfCwS.CreateField("SNum", dbText, 5)
-
-
daofldSNum.DefaultValue = "111111"
-
daofldSNum.ValidationRule = "Len([SNum])=5"
-
daofldSNum.ValidationText = "Must be 5 digits long"
-
daofldSNum.Required = True
-
daofldSNum.AllowZeroLength = False
-
-
-
Set daofldCCanEnt = daotdfCwS.CreateField("CCanEnt", dbLong)
-
-
daofldCCanEnt.DefaultValue = "0"
-
daofldCCanEnt.ValidationText = "Please enter number of candidates"
-
daofldCCanEnt.Required = True
-
-
-
-
daotdfCwS.Fields.Append daofldCNum
-
daotdfCwS.Fields.Append daofldSNum
-
daotdfCwS.Fields.Append daofldCCanEnt
-
daodb.TableDefs.Append daotdfCwS
-
-
-
-
Set fldCNum = daotdfCwS.Fields("CNum")
-
-
Set fmtCNum = fldCNum.CreateProperty("Format", dbText, "General Number")
-
fldCNum.Properties.Append fmtCNum
-
Set fmtCNum = fldCNum.CreateProperty("DecimalPlaces", dbByte, "0")
-
fldCNum.Properties.Append fmtCNum
-
Set fmtCNum = fldCNum.CreateProperty("InputMask", dbText, "00000")
-
fldCNum.Properties.Append fmtCNum
-
Set fmtCNum = fldCNum.CreateProperty("Caption", dbText, "Centre number")
-
fldCNum.Properties.Append fmtCNum
-
-
Set daoidxCNum = daotdfCwS.CreateIndex("CNum")
-
-
daoidxCNum.Required = True
-
-
Set daofldiCNum = daoidxCNum.CreateField("CNum")
-
-
daoidxCNum.Fields.Append daofldiCNum
-
daotdfCwS.Indexes.Append daoidxCNum
-
-
-
Set fldSNum = daotdfCwS.Fields("SNum")
-
-
Set fmtSNum = fldSNum.CreateProperty("InputMask", dbText, "00000")
-
fldSNum.Properties.Append fmtSNum
-
Set fmtSNum = fldSNum.CreateProperty("Caption", dbText, "Subject Reference Code")
-
fldSNum.Properties.Append fmtSNum
-
Set fmtSNum = fldSNum.CreateProperty("UnicodeCompression", dbBoolean, True)
-
fldSNum.Properties.Append fmtSNum
-
-
Set daoidxSNum = daotdfCwS.CreateIndex("SNum")
-
-
daoidxSNum.Required = True
-
-
Set daofldiSNum = daoidxSNum.CreateField("SNum")
-
-
daoidxSNum.Fields.Append daofldiSNum
-
daotdfCwS.Indexes.Append daoidxSNum
-
-
-
Set fldCCanEnt = daotdfCwS.Fields("CCanEnt")
-
-
Set fmtCCanEnt = fldCCanEnt.CreateProperty("Format", dbText, "General Number")
-
fldCCanEnt.Properties.Append fmtCCanEnt
-
Set fmtCCanEnt = fldCCanEnt.CreateProperty("DecimalPlaces", dbByte, "0")
-
fldCCanEnt.Properties.Append fmtCCanEnt
-
Set fmtCCanEnt = fldCCanEnt.CreateProperty("Caption", dbText, "N° of candidates entered")
-
fldCCanEnt.Properties.Append fmtCCanEnt
-
-
Set daoidxCCanEnt = daotdfCwS.CreateIndex("CCanEnt")
-
-
daoidxCCanEnt.Required = True
-
-
Set daofldiCCanEnt = daoidxCCanEnt.CreateField("CCanEnt")
-
-
daoidxCCanEnt.Fields.Append daofldiCCanEnt
-
daotdfCwS.Indexes.Append daoidxCCanEnt
-
-
-
-
DoCmd.RunSQL "INSERT INTO tblCwS SELECT tblCen.CNum, tblSub.SNum FROM tblCen, tblSub;"
-
-
-
End Sub
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|