引用:microsoft ado ext.2.x for ddl and securityxU9检测VBA xU9检测VBA xU9检测VBA 代码:xU9检测VBA Public Sub1_10()xU9检测VBA Dim myCat As New ADOX.Catalog ‘定义catalog变量xU9检测VBA Dim myTable As New ADOX.Table ‘定义table变量xU9检测VBA Dim myColumn As ADOX.Column ‘定义column变量xU9检测VBA Dim myIdx As New ADOX.Index ‘定义index变量xU9检测VBA Dim ws As Worksheet ‘定义worksheet变量xU9检测VBA Dim i As Long xU9检测VBA Dim myData As String ‘xU9检测VBA myData = ThisWorkbook.Path & "\学生成绩管理.mdb" ‘指定数据文件xU9检测VBA ‘判断是否有保存数据表资料的工作表存在xU9检测VBA On Error Resume NextxU9检测VBA Set ws = Worksheets("数据表设计")xU9检测VBA On Error GoTo 0xU9检测VBA If ws Is Nothing ThenxU9检测VBA MsgBox "没有数据表资料存在!", vbCritical, "警告"xU9检测VBA Exit SubxU9检测VBA End IfxU9检测VBA ws.ActivatexU9检测VBA ‘建立与数据库的连接xU9检测VBA myCat.ActiveConnection = "provider=microsoft.jet.oledb.4.0;" _xU9检测VBA & " data source=" & myDataxU9检测VBA ‘删除已经存在的数据表xU9检测VBA On Error Resume NextxU9检测VBA myCat.Tables.Delete ws.Range("B1").ValuexU9检测VBA On Error GoTo 0xU9检测VBA ‘建立索引xU9检测VBA myIdx.Name = "PrimaryKey"xU9检测VBA myIdx.PrimaryKey = TruexU9检测VBA ‘开始根据工作表的数据创建数据表xU9检测VBA With myTablexU9检测VBA .Name = ws.Range("B1").ValuexU9检测VBA For i = 4 To ws.Range("A65536").End(xlUp).RowxU9检测VBA Set myColumn = New ColumnxU9检测VBA With myColumnxU9检测VBA .Name = ws.Cells(i, 1).ValuexU9检测VBA .Type = GetConstNo(ws.Cells(i, 2).Value)xU9检测VBA If ws.Cells(i, 3).Value > 0 ThenxU9检测VBA .DefinedSize = ws.Cells(i, 3).ValuexU9检测VBA .Attributes = adColNullablexU9检测VBA End IfxU9检测VBA End WithxU9检测VBA .Columns.Append myColumnxU9检测VBA If ws.Cells(i, 4).Value = "是" ThenxU9检测VBA myIdx.Columns.Append ws.Cells(i, 1).ValuexU9检测VBA End IfxU9检测VBA NextxU9检测VBA End WithxU9检测VBA ‘将表定义进行保存xU9检测VBA myCat.Tables.Append myTablexU9检测VBA myTable.Indexes.Append myIdxxU9检测VBA ‘弹出信息xU9检测VBA MsgBox "数据表<" & ws.Range("B1").Value & ">创建成功!", _xU9检测VBA vbOKOnly + vbInformation, "创建数据表"xU9检测VBA ‘关闭连接,并释放变量xU9检测VBA Set ws = NothingxU9检测VBA Set myIdx = NothingxU9检测VBA Set myTable = NothingxU9检测VBA Set myCat = NothingxU9检测VBA End SubxU9检测VBA xU9检测VBA ‘将工作表中定义的数据类型(字符串型)转换为字段类型VBA常量,即编制一个自定义函数GetConstNoxU9检测VBA Function GetConstNo(myStr As String) As IntegerxU9检测VBA Select Case myStrxU9检测VBA Case "adBigInt": GetConstNo = 20xU9检测VBA Case "adBinary": GetConstNo = 128xU9检测VBA Case "adBoolean": GetConstNo = 11xU9检测VBA Case "adBSTR": GetConstNo = 8xU9检测VBA Case "adChapter": GetConstNo = 136xU9检测VBA Case "adChar": GetConstNo = 129xU9检测VBA Case "adCurrency": GetConstNo = 6xU9检测VBA Case "adDate": GetConstNo = 7xU9检测VBA Case "adDBDate": GetConstNo = 133xU9检测VBA Case "adDBTime": GetConstNo = 134xU9检测VBA Case "adDBTimeStamp": GetConstNo = 135xU9检测VBA Case "adDecimal": GetConstNo = 14xU9检测VBA Case "adDouble": GetConstNo = 5xU9检测VBA Case "adEmpty": GetConstNo = 0xU9检测VBA Case "adError": GetConstNo = 10xU9检测VBA Case "adFileTime": GetConstNo = 64xU9检测VBA Case "adGUID": GetConstNo = 72xU9检测VBA Case "adIDispatch": GetConstNo = 9xU9检测VBA Case "adInteger": GetConstNo = 3xU9检测VBA Case "adIUnknown": GetConstNo = 13xU9检测VBA Case "adLongVarBinary": GetConstNo = 205xU9检测VBA Case "adLongVarChar": GetConstNo = 201xU9检测VBA Case "adLongVarWChar": GetConstNo = 203xU9检测VBA Case "adNumeric": GetConstNo = 131xU9检测VBA Case "adPropVariant": GetConstNo = 138xU9检测VBA Case "adSingle": GetConstNo = 4xU9检测VBA Case "adSmallInt": GetConstNo = 2xU9检测VBA Case "adTinyInt": GetConstNo = 16xU9检测VBA Case "adUnsignedBigInt": GetConstNo = 21xU9检测VBA Case "adUnsignedInt": GetConstNo = 19xU9检测VBA Case "adUnsignedSmallInt": GetConstNo = 18xU9检测VBA Case "adUnsignedTinyInt": GetConstNo = 17xU9检测VBA Case "adUserDefined": GetConstNo = 132xU9检测VBA Case "adVarBinary": GetConstNo = 204xU9检测VBA Case "adVarChar": GetConstNo = 200xU9检测VBA Case "adVariant": GetConstNo = 12xU9检测VBA Case "adVarNumeric": GetConstNo = 139xU9检测VBA Case "adVarWChar": GetConstNo = 202xU9检测VBA Case "adWChar": GetConstNo = 130xU9检测VBA Case Else: GetConstNo = -1xU9检测VBA End SelectxU9检测VBA End FunctionxU9检测VBA xU9检测VBA