Database.CreateTableDef 方法 (DAO)
适用于:Access 2013、Office 2013
创建一个新的 TableDef 对象(仅适用于 Microsoft Access 工作区) .
语法
表达式 。CreateTableDef (Name、 Attributes、 SourceTableName、 Connect)
表达式 一个表示 Database 对象的变量。
参数
名称 |
必需/可选 |
数据类型 |
说明 |
---|---|---|---|
Name |
可选 |
Variant |
一个 Variant(String 子类型),用于唯一指定新的 TableDef 对象。 有关有效 TableDef 名称的详细信息,请参阅 Name 属性。 |
Attributes |
可选 |
Variant |
一个常量或常量组合,用于表示新的 TableDef 对象的一个或多个特性。 有关详细信息,请参阅 Attributes 属性。 |
SourceTableName |
可选 |
Variant |
一个 Variant(String 子类型),包含作为数据原始来源的外部数据库中表的名称。 源字符串将成为新的 TableDef 对象的 SourceTableName 属性设置。 |
Connect |
可选 |
Variant |
一个 Variant(String 子类型),包含有关打开数据库(在传递查询或链接表中使用的数据库)的源的信息。 有关有效的连接字符串的详细信息,请参阅 Connect 属性。 |
返回值
TableDef
说明
如果使用 CreateTableDef 方法时省略了一个或多个可选部分,则可以在将新对象追加到集合之前,使用适当的赋值语句设置或重置相应的属性。 追加对象后,可以改动此对象的某些(但不是所有)属性。 有关详细信息,请参阅各个属性主题。
如果 Name 引用一个已经是集合成员的对象,或者如果在要追加的 TableDef 或 Field 对象中指定无效的属性,则使用 Append 方法时将发生运行时错误。 另外,除非为 TableDef 对象定义了至少一个 Field,否则不能将 TableDef 对象追加到 TableDefs 集合。
若要从 TableDefs 集合中删除 TableDef 对象,请对集合使用 Delete 方法。
示例
此示例将在 Northwind 数据库中新建一个 TableDef 对象。
Sub CreateTableDefX()
Dim dbsNorthwind As Database
Dim tdfNew As TableDef
Dim prpLoop As Property
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
' Create a new TableDef object.
Set tdfNew = dbsNorthwind.CreateTableDef("Contacts")
With tdfNew
' Create fields and append them to the new TableDef
' object. This must be done before appending the
' TableDef object to the TableDefs collection of the
' Northwind database.
.Fields.Append .CreateField("FirstName", dbText)
.Fields.Append .CreateField("LastName", dbText)
.Fields.Append .CreateField("Phone", dbText)
.Fields.Append .CreateField("Notes", dbMemo)
Debug.Print "Properties of new TableDef object " & _
"before appending to collection:"
' Enumerate Properties collection of new TableDef
' object.
For Each prpLoop In .Properties
On Error Resume Next
If prpLoop <> "" Then Debug.Print " " & _
prpLoop.Name & " = " & prpLoop
On Error GoTo 0
Next prpLoop
' Append the new TableDef object to the Northwind
' database.
dbsNorthwind.TableDefs.Append tdfNew
Debug.Print "Properties of new TableDef object " & _
"after appending to collection:"
' Enumerate Properties collection of new TableDef
' object.
For Each prpLoop In .Properties
On Error Resume Next
If prpLoop <> "" Then Debug.Print " " & _
prpLoop.Name & " = " & prpLoop
On Error GoTo 0
Next prpLoop
End With
' Delete new TableDef object since this is a
' demonstration.
dbsNorthwind.TableDefs.Delete "Contacts"
dbsNorthwind.Close
End Sub
此示例使用 CreateTableDef 和 FillCache 方法以及 CacheSize、 CacheStart 和 SourceTableName 属性来枚举链接表中的记录两次。 然后,它使用 50 条记录缓存枚举记录两次。 然后,该示例通过链接表显示未缓存和缓存的运行的性能统计信息。
Sub ClientServerX3()
Dim dbsCurrent As Database
Dim tdfRoyalties As TableDef
Dim rstRemote As Recordset
Dim sngStart As Single
Dim sngEnd As Single
Dim sngNoCache As Single
Dim sngCache As Single
Dim intLoop As Integer
Dim strTemp As String
Dim intRecords As Integer
' Open a database to which a linked table can be
' appended.
Set dbsCurrent = OpenDatabase("DB1.mdb")
' Create a linked table that connects to a Microsoft SQL
' Server database.
Set tdfRoyalties = _
dbsCurrent.CreateTableDef("Royalties")
' Note: The DSN referenced below must be set to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
tdfRoyalties.Connect = _
"ODBC;DATABASE=pubs;DSN=Publishers"
tdfRoyalties.SourceTableName = "roysched"
dbsCurrent.TableDefs.Append tdfRoyalties
Set rstRemote = _
dbsCurrent.OpenRecordset("Royalties")
With rstRemote
' Enumerate the Recordset object twice and record
' the elapsed time.
sngStart = Timer
For intLoop = 1 To 2
.MoveFirst
Do While Not .EOF
' Execute a simple operation for the
' performance test.
strTemp = !title_id
.MoveNext
Loop
Next intLoop
sngEnd = Timer
sngNoCache = sngEnd - sngStart
' Cache the first 50 records.
.MoveFirst
.CacheSize = 50
.FillCache
sngStart = Timer
' Enumerate the Recordset object twice and record
' the elapsed time.
For intLoop = 1 To 2
intRecords = 0
.MoveFirst
Do While Not .EOF
' Execute a simple operation for the
' performance test.
strTemp = !title_id
' Count the records. If the end of the
' cache is reached, reset the cache to the
' next 50 records.
intRecords = intRecords + 1
.MoveNext
If intRecords Mod 50 = 0 Then
.CacheStart = .Bookmark
.FillCache
End If
Loop
Next intLoop
sngEnd = Timer
sngCache = sngEnd - sngStart
' Display performance results.
MsgBox "Caching Performance Results:" & vbCr & _
" No cache: " & Format(sngNoCache, _
"##0.000") & " seconds" & vbCr & _
" 50-record cache: " & Format(sngCache, _
"##0.000") & " seconds"
.Close
End With
' Delete linked table because this is a demonstration.
dbsCurrent.TableDefs.Delete tdfRoyalties.Name
dbsCurrent.Close
End Sub