如何判定一個表是否存在?
方法一:
很多人問如何判定一個表是否存在于某個數(shù)據(jù)庫,有人會回答,用msysobjects這個表來判定啊,這是個Access高級技巧。但是默認(rèn)情況下 admin對系統(tǒng)表沒有讀取權(quán)限,你需要手動設(shè)定,這該怎么辦哪?要么你去設(shè)定一下權(quán)限(相關(guān)方法在本站另有動畫以及文章介紹,這里不再闡述)
現(xiàn)在說兩種方法來解決判定問題 以下這種方法就是使用陷阱,造成一個錯誤,通過系統(tǒng)錯誤來判定某個表是否存在
這是個少有人介紹,但是很實(shí)用的技巧。
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
方法二:
通過寫循環(huán)讀取所有表的名字來判定表是否存在 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
'調(diào)用,比如要找名字是 aaa 的表是否存在: msgbox searchTable("aaa")
'如果存在返回 True,不存在返回 False
判斷表中是否存在某個字段的函數(shù)- Public Function IsExistField(ByVal sTableName As String, _
- ByVal sFieldName As String) As Booleanler
- Dim fld As Field
- Dim rs As DAO.Recordset
- IsExistField = False
- Set rs = CurrentDb.OpenRecordset(sTableName)
- For Each fld In rs.Fields
- If fld.Name = sFieldName Then
- IsExistField = True
- Exit For
- End If
- Next
- rs.Close
- Set rs = Nothing
- Set fld = Nothing
-
- ExitHere:
- Set rs = Nothing
- Set fld = Nothing
- Exit Function
-
- ErrorHandler:
- MsgBox Err.Description, vbInformation, "提示"
- Resume ExitHere
- End Function
復(fù)制代碼 使用示例: IsExistField("訂單表","訂單日期") '檢測訂單表中是否有訂單日期字段
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。