如果只是建表我想用ADO和DAO是都可以实现的 我有一些例子,帖出来希望对你有所帮助 如果用DAO的话: Dim dbAccess As Database Dim fdAccess As DAO.Field Dim tbAccess As TableDef Set dbAccess = ws.OpenDatabase(TxtDatabaseSel) Set tbAccess = dbAccess.CreateTableDef(TableName) Set fdAccess = tbAccess.CreateField("no", dbLong, 50) tbAccess.Fields.Append fdAccess Set fdAccess = tbAccess.CreateField("lx", dbLong, 50) tbAccess.Fields.Append fdAccess 如果是ADO我想可以这样写吧,有一些参数我就不详细写了,我只写我 以前做过的一些例子了 dim conn as connection dim cmd as command Set conn = New Connection conn.Open "PROVIDER=MSDASQL;driver={SQL Server};server=cc;uid=cc;pwd=;database=scc;" Set cmd = New Command cmd.ActiveConnection = conn cmd.CommandType = adCmdText cmd.CommandText = "create table table1(field1 varchar(10) not null,)" cmd.execute
Sub CreateDatabaseX() Dim wrkDefault As Workspace Dim dbsNew As DATABASE Dim prpLoop As Property ' Get default Workspace. Set wrkDefault = DBEngine.Workspaces(0) ' Make sure there isn't already a file with the name of ' the new database. If Dir("NewDB.mdb") <> "" Then Kill "NewDB.mdb" ' Create a new encrypted database with the specified ' collating order. Set dbsNew = wrkDefault.CreateDatabase("NewDB.mdb", _ dbLangGeneral, dbEncrypt) With dbsNew Debug.Print "Properties of " & .Name ' Enumerate the Properties collection of the new ' Database object. For Each prpLoop In .Properties If prpLoop <> "" Then Debug.Print " " & _ prpLoop.Name & " = " & prpLoop Next prpLoop End With dbsNew.CloseEnd Sub
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.CloseEnd Sub
HungryBoy(饥饿的男孩) :你的程序要引用什么?
Microsoft DAO 3.6 Object Library这句是删除你刚刚创建的表: dbsNorthwind.TableDefs.Delete "Contacts"你用的时候先把这句去掉!
我有一些例子,帖出来希望对你有所帮助
如果用DAO的话:
Dim dbAccess As Database
Dim fdAccess As DAO.Field
Dim tbAccess As TableDef
Set dbAccess = ws.OpenDatabase(TxtDatabaseSel)
Set tbAccess = dbAccess.CreateTableDef(TableName)
Set fdAccess = tbAccess.CreateField("no", dbLong, 50)
tbAccess.Fields.Append fdAccess
Set fdAccess = tbAccess.CreateField("lx", dbLong, 50)
tbAccess.Fields.Append fdAccess
如果是ADO我想可以这样写吧,有一些参数我就不详细写了,我只写我 以前做过的一些例子了
dim conn as connection
dim cmd as command
Set conn = New Connection
conn.Open "PROVIDER=MSDASQL;driver={SQL Server};server=cc;uid=cc;pwd=;database=scc;"
Set cmd = New Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = "create table table1(field1 varchar(10) not null,)"
cmd.execute
Dim dbsNew As DATABASE
Dim prpLoop As Property ' Get default Workspace.
Set wrkDefault = DBEngine.Workspaces(0) ' Make sure there isn't already a file with the name of
' the new database.
If Dir("NewDB.mdb") <> "" Then Kill "NewDB.mdb" ' Create a new encrypted database with the specified
' collating order.
Set dbsNew = wrkDefault.CreateDatabase("NewDB.mdb", _
dbLangGeneral, dbEncrypt) With dbsNew
Debug.Print "Properties of " & .Name
' Enumerate the Properties collection of the new
' Database object.
For Each prpLoop In .Properties
If prpLoop <> "" Then Debug.Print " " & _
prpLoop.Name & " = " & prpLoop
Next prpLoop
End With dbsNew.CloseEnd Sub
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.CloseEnd Sub
dbsNorthwind.TableDefs.Delete "Contacts"你用的时候先把这句去掉!