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