有什么方法可以快速判断ACCESS数据库中是否存在表?比如判断MyBd.mdb数据库中是否存在表MyTable.

解决方案 »

  1.   

    '用ado怎样实现
    '工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)
    '----------------------------------------------------------------------------
    Private Sub Form_Load()
        If ExistsTable("MyTable") Then
            MsgBox "存在表MyTable"
        End If
    End SubPrivate Function ExistsTable(ByVal strTableName As String) As Boolean
        ExistsTable = False
        Dim adoCN   As New ADODB.Connection                '定义数据库的连接
        Dim strCnn   As String
        Dim rstSchema As New ADODB.Recordset    strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb.MDB;Persist Security Info=False"
        adoCN.Open strCnn
              
        Set rstSchema = adoCN.OpenSchema(adSchemaTables)
          
        Do Until rstSchema.EOF
             If rstSchema!TABLE_TYPE = "TABLE" Then
                 If UCase(rstSchema!TABLE_NAME) = UCase(strTableName) Then
                     ExistsTable = True
                     Exit Do
                 End If
             End If
             rstSchema.MoveNext
        Loop
        rstSchema.Close
        adoCN.Close
        Set rstSchema = Nothing
        Set adoCN = Nothing
        
    End Function
      

  2.   

    strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb.MDB;Persist Security Info=False"
    -------------------------------------------------------------------------
    上面的连接多写了一个.mdb,改一下:strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb;Persist Security Info=False"
      

  3.   

    rst.open "select * from sysobjects where name='TblName'" ,cnn  
        if rst.recordcount=0 then 
         msgbox "不存在此表" 
        else 
         msgbox "存在" 
        end if 
      

  4.   

    rst.open "select * from sysobjects where name='TblName'" ,cnn 
        if rst.recordcount=0 then 
         msgbox "不存在此表" 
        else 
         msgbox "存在" 
        end if 
         
      

  5.   

    引用Microsoft ADO Extensions 2.1 for DDL and Security (ADOX)): 
        (需要升级至VB 6.0SP3) 
        Dim adoConnectionX As New ADODB.Connection 
        'Ms SQL 7: 
        adoConnectionX.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Data Source=yuer;DataBase=NorthwindCS" 
        'Access 2000: 
        'adoConnectionX.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\DRptPlus\DRptPlus\Data\NWind2K.mdb;Persist Security Info=False" 
         
        Dim adoxCatalogX As New ADOX.Catalog 
        Set adoxCatalogX.ActiveConnection = adoConnectionX 
         
        Dim TestTableName As String 
        TestTableName = "产品" 
         
        Dim adoxTableX As ADOX.Table 
        For Each adoxTableX In adoxCatalogX.Tables 
         If adoxTableX.Name = TestTableName Then 
         MsgBox "[" & TestTableName & "]表已存在!" 
         Exit For 
         End If 
       Next 
      

  6.   

    http://www.china-askpro.com/msg26/qa72.shtml
      

  7.   

    第一个问题和第二个读可以参考这个
    http://soft.mrol.net/article/56/57/2006/2006013111939.html  你读懂这些代码就很容易了,其实代码也很容易的,呵呵
    抢分的还真快呀呵呵
      

  8.   

    '用ado怎样实现
    '工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)
    '----------------------------------------------------------------------------
    Private Sub Form_Load()
        MsgBox "表MyTable中有 " & fieldCount & " 个字段"
    End Sub
    Private Function fieldCount(ByVal strTableName As String) As Integer
        fieldCount = 0
        Dim adoCN   As New ADODB.Connection                '定义数据库的连接
        Dim strCnn   As String
        Dim rstSchema As New ADODB.Recordset    strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MyBd.mdb.MDB;Persist Security Info=False"
        adoCN.CursorLocation = adUseClient
        adoCN.Open strCnn
        '查询MyTable,得到字段数
        rstSchema.Open "select top 1 * from MyTable", adoCN, adOpenDynamic, adLockOptimistic
        fieldCount = rstSchema.Fields.Count
        rstSchema.Close
        adoCN.Close
        Set rstSchema = Nothing
        Set adoCN = Nothing
    End Function