比如有段sql语句
rs.open "select * form table1"
假如table1不存在怎么捕捉到这个错误而不是报错,比如得到不存在表的信息,msgbox "此数据表不存在"

解决方案 »

  1.   

    openschema()这个方法可以知道数据库中有哪些表
    好像只适合mdb
      

  2.   

    informix里
    select count(×) from systables where tabname = '...'
      

  3.   

    Option Explicit
    '愙懕暥帤楍
    Private Const strConnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    Public conn As ADODB.ConnectionPrivate DBPath As String
    Private Sub Command1_Click()
        If init() = False Then
            Exit Sub
        End If
        
        Dim a As Boolean
        
        a = IsExistTable("E0102") 'a=true表存在,a=false表不存在
        
    End Sub'CONN
    Private Function ConnToAccess(ByVal DBStr As String) As Boolean
        ConnToAccess = True
        
        'EXIST DB
        If Dir(DBStr) = "" Then
            MsgBox DBStr & " IS NOT EXIST", vbCritical, "DB Exists"
            Exit Function
        End If
        
        Set conn = New ADODB.Connection
        conn.ConnectionString = strConnstr & DBStr
        conn.Open
        conn.BeginTrans
        
        ConnToAccess = False
    End Function
    Private Function init() As Boolean
        init = False
        
        DBPath = "D:\Mytest\MEDB.mdb"
        'CONN DB
        If ConnToAccess(DBPath) Then
            Exit Function
        End If
        
        init = True
    End Function
    Private Function IsExistTable(ByVal table As String) As Boolean
        Dim Rs As ADODB.Recordset
        Set Rs = CreateObject("ADODB.RECORDSET")
        Set Rs = conn.OpenSchema(adSchemaTables)
        Do Until (Rs.EOF)
            If (UCase(Rs!TABLE_NAME) = UCase(table)) Then
                IsExistTable = True
                Exit Function
            End If
            Rs.MoveNext
        Loop
        IsExistTable = False
    End Function