下面是我的代码: '**************************************************************** '* 本函数功能是检测一个数据库是否存一个指定的表,返回值为逻辑型 * '* DatabaseName:被检测的数据库文件名 * '* TableName:被检测的表名称 * '**************************************************************** Public Function ExistsTable(DatabaseName As String, TableName As String) As Boolean If DatabaseName = "" Or TableName = "" Then MsgBox "请指定完整的数据名和表名.", vbCritical + vbOKOnly, "错误" ExistsTable = False Exit Function End If If Dir(DatabaseName) = "" Then MsgBox "指定的数据库不存在", vbCritical + vbOKOnly, "错误" ExistsTable = False Exit Function End If ExistsTable = False Dim Db As DAO.Database Dim Test As String Set Db = DBEngine.OpenDatabase(DatabaseName) On Error Resume Next Test = Db.TableDefs(TableName).Name If Err <> 3265 Then ExistsTable = True Err = 0 End If Db.Close: Set Db = Nothing End Function '*******************************************************
也可以用OpenSchema的方法打开系统表啊。
多谢Stiven_PFan(NCsoft) 我想最好不用DAO来做 ado或adox怎么做?
不用这么麻烦,执行下面的SQL语句就可以了: select * from dbo.sysobjects where id = object_id(N'[dbo].[yourtabname]') and OBJECTPROPERTY(id, N'IsUserTable')如果返回有记录,则该表存在,否则不存在
lishush(☆↑苯鸟先飞↑☆)是完全正确的。
'********************************************************* '* 名称:TableExists '* 功能:判断表是否存在(表名) '* 用法:TableExists(表名) adoCN是一个SQL的连接 '********************************************************* 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
另一种DAO方式Public Function ExistsTable(TName As String) As Boolean Dim dbAccess As Database Dim Test As String
On Error Resume Next
Set dbAccess = OpenDatabase("c:\Foreign.mdb", False, False) ' 该名字在表名中是否存在。 Test = dbAccess.TableDefs(TName).Name If Err <> 3265 Then ExistsTable = True Err = 0 End If dbAccess.Close End Function
'****************************************************************
'* 本函数功能是检测一个数据库是否存一个指定的表,返回值为逻辑型 *
'* DatabaseName:被检测的数据库文件名 *
'* TableName:被检测的表名称 *
'****************************************************************
Public Function ExistsTable(DatabaseName As String, TableName As String) As Boolean
If DatabaseName = "" Or TableName = "" Then
MsgBox "请指定完整的数据名和表名.", vbCritical + vbOKOnly, "错误"
ExistsTable = False
Exit Function
End If
If Dir(DatabaseName) = "" Then
MsgBox "指定的数据库不存在", vbCritical + vbOKOnly, "错误"
ExistsTable = False
Exit Function
End If
ExistsTable = False
Dim Db As DAO.Database
Dim Test As String
Set Db = DBEngine.OpenDatabase(DatabaseName)
On Error Resume Next
Test = Db.TableDefs(TableName).Name
If Err <> 3265 Then
ExistsTable = True
Err = 0
End If
Db.Close: Set Db = Nothing
End Function
'*******************************************************
我想最好不用DAO来做
ado或adox怎么做?
select * from dbo.sysobjects where id = object_id(N'[dbo].[yourtabname]') and OBJECTPROPERTY(id, N'IsUserTable')如果返回有记录,则该表存在,否则不存在
'* 名称:TableExists
'* 功能:判断表是否存在(表名)
'* 用法:TableExists(表名) adoCN是一个SQL的连接
'*********************************************************
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
Dim dbAccess As Database
Dim Test As String
On Error Resume Next
Set dbAccess = OpenDatabase("c:\Foreign.mdb", False, False)
' 该名字在表名中是否存在。
Test = dbAccess.TableDefs(TName).Name
If Err <> 3265 Then
ExistsTable = True
Err = 0
End If
dbAccess.Close
End Function