如何判定一个表是否存在? 方法一: 很多人问如何判定一个表是否存在于某个数据库,有人会回答,用msysobjects这个表来判定啊,这是个Access高级技巧。但是默认情况下 admin对系统表没有读取权限,你需要手动设定,这该怎么办哪?要么你去设定一下权限(相关方法在本站另有动画以及文章介绍,这里不再阐述) 现在说两种方法来解决判定问题 以下这种方法就是使用陷阱,造成一个错误,通过系统错误来判定某个表是否存在 这是个少有人介绍,但是很实用的技巧。 Function test() MsgBox TableIsIn("表2") End Function Function TableIsIn(TableName As String) TableIsIn = True On Error Resume Next Dim strSQL As String strSQL = "select * from " & TableName CurrentDb.Execute strSQL If Err.Number = 3078 Then TableIsIn = False End If End Function 方法二: 通过写循环读取所有表的名字来判定表是否存在 Function searchTable(TableName As String) As Boolean searchTable = False Dim tbl As DAO.TableDef For Each tbl In CurrentDb.TableDefs If tbl.Name = TableName Then searchTable = True Exit For End If Next End Function '调用,比如要找名字是 aaa 的表是否存在: msgbox searchTable("aaa") '如果存在返回 True,不存在返回 False 判断表中是否存在某个字段的函数
|
|