首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎么得到数据表不存在的异常信息

2012-02-29 
如何得到数据表不存在的错误信息比如有段sql语句rs.open select * form table1假如table1不存在怎么捕捉

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

[解决办法]
Option Explicit
'愙懕暥帤楍
Private Const strConnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Public conn As ADODB.Connection

Private 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

热点排行