access里面怎麼判斷一個表是否存在?

解决方案 »

  1.   

    rs.open "select [name] from msysobjects where [type]=1 and [flags]=0 and [name]='" & 你要找的表名 & "'"if rs.eof then
        msgbox "表不存在"
      else
        msgbox "表已存在"
    end if
      

  2.   

    Private Sub Form_Load()
    Dim adoCN   As New ADODB.Connection                '定义数据库的连接
    Dim strCnn   As New ADODB.Recordset
    Dim I As Integer
       str1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Northwind.MDB;Persist Security Info=False"
       adoCN.Open str1
             
       Set rstSchema = adoCN.OpenSchema(adSchemaTables)
         
       Do Until rstSchema.EOF
            If rstSchema!TABLE_TYPE = "TABLE" Then
               out = out & "Table  name:  " & _
                   rstSchema!TABLE_NAME & vbCr & _
                   "Table  type:  " & rstSchema!TABLE_TYPE & vbCr
                I = I + 1
            End If
            rstSchema.MoveNext
       Loop
       MsgBox I
       rstSchema.Close
         
       adoCN.Close
    Debug.Print out
    End Sub
      

  3.   

    这个最直接!adoCN是一个access的连接
    你用的时候改一下即可
    '*********************************************************
    '* 名称:TableExists
    '* 功能:判断表是否存在(表名)
    '* 用法:TableExists(表名) adoCN是一个access的连接
    '*********************************************************
    Public Function TableExists(findTable As String) As Boolean
        Dim rstSchema As New ADODB.Recordset
        Set rstSchema = adoCN.OpenSchema(adSchemaTables)
        rstSchema.Find "TABLE_NAME='" & findTable & "'"
        If rstSchema.EOF Then
          TableExists = False
        Else
          TableExists = True
        End If
        rstSchema.Close
    End Function