Fastest Functions to check whether a Table or Field exists in a Database
Public Function isTableExists(con As ADODB.Connection, Table As String) As Boolean
On Error GoTo handle
Dim rs As New ADODB.Recordset
If rs.State = 1 Then rs.Close
rs.Open "Select * from " & Table, conn, adOpenStatic, adLockReadOnly
isTableExists= True
Exit Function
handle:
If Err.Number = -2147217865 Then
isTableExists = False
Else
MsgBox Err.Description
End If
End Function
Public Function isFieldExists(conn As ADODB.Connection, Table As String, field As String) As Boolean
On Error GoTo handle
Dim rs As New ADODB.Recordset
If rs.State = 1 Then rs.Close
rs.Open "Select " & field & " from " & Table, conn, adOpenStatic, adLockReadOnly
isFieldExists = True
Exit Function
handle:
If Err.Number = -2147217904 Then
isFieldExists= False
Else
MsgBox Err.Description & Err.Number
End If
End Function
Public Function isTableExists(con As ADODB.Connection, Table As String) As Boolean
On Error GoTo handle
Dim rs As New ADODB.Recordset
If rs.State = 1 Then rs.Close
rs.Open "Select * from " & Table, conn, adOpenStatic, adLockReadOnly
isTableExists= True
Exit Function
handle:
If Err.Number = -2147217865 Then
isTableExists = False
Else
MsgBox Err.Description
End If
End Function
Public Function isFieldExists(conn As ADODB.Connection, Table As String, field As String) As Boolean
On Error GoTo handle
Dim rs As New ADODB.Recordset
If rs.State = 1 Then rs.Close
rs.Open "Select " & field & " from " & Table, conn, adOpenStatic, adLockReadOnly
isFieldExists = True
Exit Function
handle:
If Err.Number = -2147217904 Then
isFieldExists= False
Else
MsgBox Err.Description & Err.Number
End If
End Function
No comments:
Post a Comment