登录说明 | 升级会员 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 行业资讯

.mdb.access数据库VBA-利用工作表数据创建数据表(ADOX)

时间:2025-01-20 08:52:55  来源:  作者:

利用工作表数据创建数据表(ADOX)

引用: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
 
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
合肥住建口检测单位招聘,高级工程师+协会九大专项证书,人员全职兼职均可
合肥住建口检测单位招
关于印发安徽省建设工程专业技术资格评审标准条件的通知(图文)
关于印发安徽省建设工
一般溶液有效期一览表
一般溶液有效期一览表
2022年一建《机电》真题解析
2022年一建《机电》真
相关文章
    无相关信息
栏目更新
栏目热门