|
楼主 |
发表于 2010-9-1 19:19
|
显示全部楼层
原帖由 wach 于 2010-9-1 17:58 发表
谢谢楼主的分享!但我下载了前几个表(创建数据库,创建数据表等)全是空的EXCEL表没有宏没控件之类!
7楼附件的代码:
Public Sub 在已有的数据库中创建数据表方法1()
Dim myDb As DAO.Database
Dim myTbl As DAO.TableDef
Dim myData As String
Dim myTable As String
myData = ThisWorkbook.Path & "\学生成绩管理.mdb"
myTable = "期末成绩"
Set myDb = OpenDatabase(myData)
myDb.TableDefs.Delete myTable
Set myTbl = myDb.CreateTableDef(myTable)
With myTbl
.Fields.Append .CreateField("学号", dbText, 10)
.Fields.Append .CreateField("姓名", dbText, 6)
.Fields.Append .CreateField("性别", dbText, 1)
.Fields.Append .CreateField("班级", dbText, 10)
.Fields.Append .CreateField("数学", dbSingle)
.Fields.Append .CreateField("语文", dbSingle)
.Fields.Append .CreateField("物理", dbSingle)
.Fields.Append .CreateField("化学", dbSingle)
.Fields.Append .CreateField("英语", dbSingle)
.Fields.Append .CreateField("总分", dbSingle)
End With
myDb.TableDefs.Append myTbl
myDb.Close
Set myDb = Nothing
Set myTbl = Nothing
MsgBox "数据表<" & myTable & ">创建成功!", _
vbOKOnly + vbInformation, "创建数据表"
End Sub
Public Sub 在已有的数据库中创建数据表方法2()
Dim myCat As New ADOX.Catalog
Dim myTbl As New Table
Dim myData As String
Dim myTable As String
myData = ThisWorkbook.Path & "\学生成绩管理.mdb"
myTable = "期末成绩"
myCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & myData
myCat.Tables.Delete myTable
With myTbl
.Name = myTable
.Columns.Append "学号", adVarWChar, 10
.Columns.Append "姓名", adVarWChar, 6
.Columns.Append "性别", adVarWChar, 1
.Columns.Append "班级", adVarWChar, 10
.Columns.Append "数学", adSingle
.Columns.Append "语文", adSingle
.Columns.Append "物理", adSingle
.Columns.Append "化学", adSingle
.Columns.Append "英语", adSingle
.Columns.Append "总分", adSingle
End With
myCat.Tables.Append myTbl
Set myCat = Nothing
Set myTbl = Nothing
MsgBox "数据表<" & myTable & ">创建成功!", _
vbOKOnly + vbInformation, "创建数据表"
End Sub
Public Sub 在已有的数据库中创建数据表方法3()
Dim myCat As New ADOX.Catalog
Dim myCmd As New ADODB.Command
Dim myData As String
Dim myTable As String
Dim SQL As String
myData = ThisWorkbook.Path & "\学生成绩管理.mdb"
myTable = "期末成绩"
myCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & myData
myCat.Tables.Delete myTable
Set myCmd.ActiveConnection = myCat.ActiveConnection
SQL = "CREATE TABLE " & myTable _
& "(学号 text(10),姓名 text(6),性别 text(1),班级 text(10)," _
& "数学 Single,语文 Single,物理 Single,化学 Single," _
& "英语 Single,总分 Single)"
With myCmd
.CommandText = SQL
.Execute , , adCmdText
End With
Set myCat = Nothing
Set myCmd = Nothing
MsgBox "数据表<" & myTable & ">创建成功!", _
vbOKOnly + vbInformation, "创建数据表"
End Sub
Public Sub 在已有的数据库中创建数据表方法4()
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim myData As String
Dim myTable As String
Dim SQL As String
myData = ThisWorkbook.Path & "\学生成绩管理.mdb"
myTable = "期末成绩"
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open myData
End With
SQL = "drop table " & myTable
Set rs = cnn.Execute(SQL)
SQL = "CREATE TABLE " & myTable _
& "(学号 text(10),姓名 text(6),性别 text(1),班级 text(10)," _
& "数学 Single,语文 Single,物理 Single,化学 Single," _
& "英语 Single,总分 Single)"
Set rs = cnn.Execute(SQL)
cnn.Close
Set rs = Nothing
Set cnn = Nothing
MsgBox "数据表<" & myTable & ">创建成功!", vbInformation, "创建数据表"
End Sub |
|