如何判定一個(gè)表是否存在?
方法一:
很多人問(wèn)如何判定一個(gè)表是否存在于某個(gè)數(shù)據(jù)庫(kù),有人會(huì)回答,用msysobjects這個(gè)表來(lái)判定啊,這是個(gè)Access高級(jí)技巧。但是默認(rèn)情況下 admin對(duì)系統(tǒng)表沒(méi)有讀取權(quán)限,你需要手動(dòng)設(shè)定,這該怎么辦哪?要么你去設(shè)定一下權(quán)限(相關(guān)方法在本站另有動(dòng)畫(huà)以及文章介紹,這里不再闡述)
現(xiàn)在說(shuō)兩種方法來(lái)解決判定問(wèn)題 以下這種方法就是使用陷阱,造成一個(gè)錯(cuò)誤,通過(guò)系統(tǒng)錯(cuò)誤來(lái)判定某個(gè)表是否存在
這是個(gè)少有人介紹,但是很實(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
方法二:
通過(guò)寫(xiě)循環(huán)讀取所有表的名字來(lái)判定表是否存在 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
判斷表中是否存在某個(gè)字段的函數(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("訂單表","訂單日期") '檢測(cè)訂單表中是否有訂單日期字段
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。