473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating A Table With More Properties Than Fieldname, Length, Type

72 New Member
I have a database, where I need to create a table if it doesn't exist. This is my code at the moment:

Expand|Select|Wrap|Line Numbers
  1. Sub CreateCwS() 
  2.  
  3.     Dim daotbl As DAO.TableDef 
  4.  
  5.     If Not daotbl.Name = tblCwS Then 
  6.  
  7.         DoCmd.SetWarnings False 
  8.  
  9.         Dim daodb As DAO.Database 
  10.         Dim daotblCwS As DAO.TableDef 
  11.         Dim daofldCNum As DAO.Field 
  12.         Dim daofldSNum As DAO.Field 
  13.         Dim daofldCCanEnt As DAO.Field 
  14.         Dim daoiCNum As DAO.Index 
  15.         Dim daoiSNum As DAO.Index 
  16.         Dim daoiCCanEnt As DAO.Index 
  17.  
  18.         Set daodb = CurrentDb() 
  19.         Set daotblCwS = daodb.CreateTableDef("tblCwS") 
  20.  
  21.         Set daofldCNum = daotblCwS.CreateField("CNum", dbLong) 
  22.         Set daofldSNum = daotblCwS.CreateField("SNum", dbText, 5) 
  23.         Set daofldCCanEnt = daotblCwS.CreateField("CCanEnt", dbLong) 
  24.  
  25.         daotblCwS.Fields.Append daofldCNum 
  26.         daotblCwS.Fields.Append daofldSNum 
  27.         daotblCwS.Fields.Append daofldCCanEnt 
  28.         daodb.TableDefs.Append daotblCwS 
  29.  
  30.         DoCmd.RunSQL "INSERT INTO tblCwS SELECT tblCen.CNum, tblSub.SNum FROM tblCen, tblSub;" 
  31.  
  32.     End If 
  33.  
  34. End Sub 
  35.  
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:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE
  2.  tblCwS (CNum n (5 , 0)
  3.   NOT NULL 
  4.   CHECK Between 10000 And 79999 And Len([CNum])=5
  5.   ERROR Must be 5 digits Long, lie between 10000 And 79999, And unique
  6.   DEFAULT 10000
  7.  
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
Apr 25 '07 #1
12 2667
MMcCarthy
14,534 Recognized Expert Moderator MVP
Once the table has been created close the recordset and then reopen as follows:
Expand|Select|Wrap|Line Numbers
  1. Dim idxTbl As Index
  2. Dim idxFld As Field
  3.  
  4. Set daotblCwS = daodb.CreateTableDef("tblCwS") 
  5.  
  6. 'set the tables primary key
  7. Set idxTbl = daotblCwS.CreateIndex("PrimaryKey")
  8. idxTbl.Primary = -1
  9. idxTbl.Unique = -1
  10. Set idxFld = idxTbl.CreateField("CNum")
  11. idxTbl.Fields.Append idxFld
  12. daotblCwS.Indexes.Append idxTbl
The following is sample code which will give examples of how to set the properties on the fields.

Expand|Select|Wrap|Line Numbers
  1. 'Format a single field to have two decimal places
  2. Set fld = tbl.Fields("MyNumberField")
  3. Set fFormat = fld.CreateProperty("Format", dbText, "Fixed")
  4. fld.Properties.Append fFormat
  5. Set fFormat = fld.CreateProperty("DecimalPlaces", dbByte, 2)
  6. fld.Properties.Append fFormat
  7.  
  8. 'Format the datetime field to be a medium time
  9. Set fld = tbl.Fields("MyDateField")
  10. Set fFormat = fld.CreateProperty("Format", dbText, "Medium Time")
  11. fld.Properties.Append fFormat
  12.  
Apr 25 '07 #2
smiler2505
72 New Member
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
Apr 25 '07 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
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") .
Apr 25 '07 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
Sorry just noticed an error in my previous code, replace
Expand|Select|Wrap|Line Numbers
  1.  Set daotblCwS = daodb.CreateTableDef("tblCwS") 
with
Expand|Select|Wrap|Line Numbers
  1.  Set daotblCwS = daodb.TableDef("tblCwS") 
Apr 25 '07 #5
smiler2505
72 New Member
How do I open and close record sets?
(I thought it was
Expand|Select|Wrap|Line Numbers
  1. Dim rstCwS As RecordSet
  2. Set rstCwS  = daodb.daotblCwS
  3. rstCwS.Close
  4. rstCwS.OpenRecordset
  5.  
but it's not)
Apr 25 '07 #6
MMcCarthy
14,534 Recognized Expert Moderator MVP
Try this ...
Expand|Select|Wrap|Line Numbers
  1. Dim rstCwS As RecordSet
  2. Set rstCwS  = daodb.OpenRecordset("daotblCwS")
  3.  
  4.   ' you procedures here
  5.  
  6. rstCwS.OpenRecordset
  7.  
Have a look at this basic tutorial:

Access VBA DAO recordset loop using two recordsets
Apr 25 '07 #7
ADezii
8,834 Recognized Expert Expert
I have a database, where I need to create a table if it doesn't exist. This is my code at the moment:

Expand|Select|Wrap|Line Numbers
  1. Sub CreateCwS() 
  2.  
  3.     Dim daotbl As DAO.TableDef 
  4.  
  5.     If Not daotbl.Name = tblCwS Then 
  6.  
  7.         DoCmd.SetWarnings False 
  8.  
  9.         Dim daodb As DAO.Database 
  10.         Dim daotblCwS As DAO.TableDef 
  11.         Dim daofldCNum As DAO.Field 
  12.         Dim daofldSNum As DAO.Field 
  13.         Dim daofldCCanEnt As DAO.Field 
  14.         Dim daoiCNum As DAO.Index 
  15.         Dim daoiSNum As DAO.Index 
  16.         Dim daoiCCanEnt As DAO.Index 
  17.  
  18.         Set daodb = CurrentDb() 
  19.         Set daotblCwS = daodb.CreateTableDef("tblCwS") 
  20.  
  21.         Set daofldCNum = daotblCwS.CreateField("CNum", dbLong) 
  22.         Set daofldSNum = daotblCwS.CreateField("SNum", dbText, 5) 
  23.         Set daofldCCanEnt = daotblCwS.CreateField("CCanEnt", dbLong) 
  24.  
  25.         daotblCwS.Fields.Append daofldCNum 
  26.         daotblCwS.Fields.Append daofldSNum 
  27.         daotblCwS.Fields.Append daofldCCanEnt 
  28.         daodb.TableDefs.Append daotblCwS 
  29.  
  30.         DoCmd.RunSQL "INSERT INTO tblCwS SELECT tblCen.CNum, tblSub.SNum FROM tblCen, tblSub;" 
  31.  
  32.     End If 
  33.  
  34. End Sub 
  35.  
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:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE
  2.  tblCwS (CNum n (5 , 0)
  3.   NOT NULL 
  4.   CHECK Between 10000 And 79999 And Len([CNum])=5
  5.   ERROR Must be 5 digits Long, lie between 10000 And 79999, And unique
  6.   DEFAULT 10000
  7.  
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 programmaticall y 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.
Apr 26 '07 #8
MMcCarthy
14,534 Recognized Expert Moderator MVP
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 programmaticall y 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
Apr 26 '07 #9
smiler2505
72 New Member
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

Expand|Select|Wrap|Line Numbers
  1.  Dim daodb As DAO.Database
  2.  Dim daotdfCwS As DAO.TableDef
  3.  Dim tdfCwS As TableDef
  4.  Dim daofldCNum As DAO.Field
  5.  Dim daofldSNum As DAO.Field
  6.  Dim daofldCCanEnt As DAO.Field
  7.  Dim idxPK As Index
  8.  Dim fldPK As Field
  9.  Dim fldCNum As Field
  10.  Dim fldSNum As Field
  11.  Dim fldCCanEnt As Field
  12.  Dim fmtCNum As Property
  13.  Dim fmtSNum As Property
  14.  Dim fmtCCanEnt As Property
  15.  Dim rstCwS As DAO.Recordset
  16.  
  17.  Set daodb = CurrentDb()
  18.  Set daotdfCwS = daodb.CreateTableDef("tblCwS")
  19.  
  20.  Set daofldCNum = daotdfCwS.CreateField("CNum", dbLong)
  21.  Set daofldSNum = daotdfCwS.CreateField("SNum", dbText, 5)
  22.  Set daofldCCanEnt = daotdfCwS.CreateField("CCanEnt", dbLong)
  23.  
  24.  daotdfCwS.Fields.Append daofldCNum
  25.  daotdfCwS.Fields.Append daofldSNum
  26.  daotdfCwS.Fields.Append daofldCCanEnt
  27.  daodb.TableDefs.Append daotdfCwS
  28.  
  29.  Set rstCwS = daodb.OpenRecordset("tblCwS")
  30.  
  31.  rstCwS.OpenRecordset
  32.  
  33.  Set tdfCwS = daodb.TableDefs("tblCwS")
  34.  Set idxPK = tdfCwS.CreateIndex("PrimaryKey")
  35.  
  36.  idxPK.Primary = -1
  37.  idxPK.Unique = -1
  38.  
  39.  Set fldPK = idxPK.CreateField("CNum")
  40.  
  41.  idxPK.Fields.Append fldPK
  42.  tdfCwS.Indexes.Append idxPK
  43.  
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?
Apr 26 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2715
by: gonzalo briceno | last post by:
I have been using phplib for a while and I really like the framework except for form creation. Maybe it is me but I in my opinion there isn't a good way to create forms or should I say, everything else is so well done that the way you create forms seems to be too cumbersome, in particular making it so that a pull down menu selects a value...
3
8450
by: Jon Smyth | last post by:
I am trying to create a database programmatically using vb5 with the DAO 3.6 object. I'm using the following segment, but I'm not having any luck. I can't seen to find much help either. Any offerings here? Set db = CreateDatabase(dbFileName, dbLangGeneral, dbVersion30) db.Execute "CREATE TABLE Food_Table (Category Text(50), " & _ "Name...
9
8878
by: Paul | last post by:
Hi all Arggghhh........... The problem.....I want the user to be able to create an excel document and name particular cells in the document where they want the data to be placed and then save this out of an XML file or Excel Template file. Next I need to convert a dataset to xml and try and transform this data into the users xml...
1
8670
by: Zlatko Matić | last post by:
Hello. I tried to create table from the query recordset, but it seems that I can't set FieldType and FieldSize by variable...What is wrong? qryrs is QueryDef recordset (pass-through query)... Set tbl = db.CreateTableDef(IME_TABLICE) For I = 0 To qryrs.Fields.Count - 1
1
3392
by: Dixie | last post by:
I wish to add some fields to an existing table in code. I am using the following code from rkc. CurrentDb.Execute ("ALTER TABLE MyTable ADD MyNewField Text 25") This works , but I need to also set the Required, Allow Zero Length and Indexed attributes. I have tried but keep getting a syntax error. Also, can I set the default value of a field...
9
3061
by: Patrick.O.Ige | last post by:
I have a code below and its a PIE & BAR CHART. The values now are all static but I want to be able to pull the values from a database. Can you guys give me some ideas to do this? Thanks Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Declare your object variables
3
3475
by: droesler | last post by:
I have a function that receives a form name and field name. someFunc(formName, fieldName) { var fieldVal = document.forms.elements.value; } This triggers the 'has no properties error. An alert message shows the correct values are being passed in. If I hardcode var fieldVal = document.realFormName.realFieldName.value;
2
1718
by: sparks | last post by:
last month I started on changing the format in a LOT of tables. changing Long Integer and Singles to double. I got that working but I have one more question.. Sub AlterFieldType(TblName As String, FieldName As String, DataType As _
7
3638
by: mlthomas007 | last post by:
Hi, A client asked for a code book (all fields, descriptions, tables, etc.) from our Access database. Has anyone had to do this? It seems to me there must be a way to extract all this information from Access. Any assistance would be wonderful. Thanks.
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.