Check if access table exists

前端 未结 5 2070
萌比男神i
萌比男神i 2020-11-30 09:50

I want to log web site visits\' IP, datetime, client and refferer data to access database but I\'m planning to log every days log data in separate tables in example logs for

5条回答
  •  难免孤独
    2020-11-30 10:35

    This question is quite old but I found that no answer is satisfying, because:

    • they do not handle the case of "bad" linked tables, where the linked table points to a non existing db or table.
    • since linked tables are potentially huge, we must be able to check them with a fast query.

    So here is my simple but more complete solution:

    Function isTableOk(tblName As String) As Boolean
    'works with local or linked tables
        Dim db As DAO.Database, rs As DAO.Recordset
        Dim sSql As String
        sSql = "SELECT TOP 1 ""xxx"" AS Expr1 FROM [" & tblName & "]"
    
        On Error Resume Next
        Err.Clear
        Set db = CurrentDb
        Set rs = db.OpenRecordset(sSql)
        isTableOk = (Err.Number = 0)
        rs.Close
    End Function
    

    You can even check table in an external Access db with this version:

    Function isTableOk(tblName As String, Optional dbName As String) As Boolean
    'works with local or linked tables, or even tables in external db (if dbName is provided)
    
        Dim db As DAO.Database, rs As DAO.Recordset
        Dim sSql As String
    
        Set db = CurrentDb
        sSql = "SELECT TOP 1 'xxx' AS Expr1 FROM [" & tblName & "]"
        If Len(dbName) > 0 Then 'external db 
            sSql = sSql & " IN '" & dbName & "'"
        End If
        Err.Clear
        On Error Resume Next
        Set rs = db.OpenRecordset(sSql)
        isTableOk = (Err.Number = 0)
        rs.Close
    End Function
    

提交回复
热议问题