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
12 2667 MMcCarthy 14,534
Recognized Expert Moderator MVP
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
-
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
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") .
MMcCarthy 14,534
Recognized Expert Moderator MVP
Sorry just noticed an error in my previous code, replace - Set daotblCwS = daodb.CreateTableDef("tblCwS")
with - Set daotblCwS = daodb.TableDef("tblCwS")
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)
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: -
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 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.
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
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?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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
|
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...
| |
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
|
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;
|
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
_
|
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.
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |