我提供你三个函数,都好使。 随你意愿使用。 直接粘贴到 模块里即可, 不需变动任何东西。
函数1:
Function tblA(strTableName As String) As Integer ''' 表对象是否存在 tblA("表1") 返回值 -1 ,0
Dim db As Database
Dim I As Integer
Set db = DBEngine.Workspaces(0).Databases(0)
tblA = False
db.TableDefs.Refresh
For I = 0 To db.TableDefs.Count - 1
If strTableName = db.TableDefs(I).Name Then
'Table Exists
tblA = True
Exit For
End If
Next I
Set db = Nothing
End Function
函数2:
Function tblB(strTableName As String) As Boolean ''' 表对象是否存在 tblB("表1") 返回值 true , false
tblB = Nz(DLookup("[Id]", "[MSysObjects]", "[Type]=1 AND [Name]='" & strTableName & "'"))
End Function
函数3:
Function tblC(strTableName As String) As Boolean ''' 表对象是否存在 tblC("表1") 返回值 true , false
On Error Resume Next
tblC = IsObject(CurrentDb.TableDefs(strTableName))
End Function
Function TheTableIsExists(strDBName As String, strTableName As String) As Boolean
'本函数用于判断strDBName指定的数据库中,是否存在strTableName为表名的表
'返回结果:存在返回true 不存在返回 false
Dim CN As ADODB.Connection
Set CN = New ADODB.Connection
CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentProject.Path & strDBName
Set RS = CN.OpenSchema(adSchemaTables)
Do Until RS.EOF
If RS("TABLE_TYPE") = "TABLE" And RS("TABLE_NAME") = strTableName Then
TheTableIsExists = True
Exit Function
End If
RS.MoveNext
Loop
RS.Close
TheTableIsExists = False
End Function
zhangyjllf 您好!
TheTableIsExists函数对有密码的数据库不能测试,对Aeecss2007版.accdb数据库不能测试表是否存在,应该如何处理?
总记录:4篇 页次:1/1 9 1 :