检查访问表是否存在

Has*_*anG 17 sql ms-access ms-access-2007

我想记录网站访问的IP,日期时间,客户端和refferer数据来访问数据库,但我打算每天记录06.06.2010的示例日志中的单独表中的日志数据将记录在2010_06_06命名表中.当日期改变时,我将创建一个名为2010_06_07的表.但问题是如果已经创建了这个表.

有关如何检查Access中是否存在表的任何建议?

Fio*_*ala 38

您可以使用隐藏的系统表MSysObjects来检查表是否存在:

If Not IsNull(DlookUp("Name","MSysObjects","Name='TableName'")) Then
    'Table Exists
Run Code Online (Sandbox Code Playgroud)

但是,我同意每天创建一个新表是一个非常糟糕的主意.

编辑:我应该添加表格有类型1,4或6,并且其他类型的其他对象可能与表格具有相同的名称,因此最好说:

If Not IsNull(DlookUp("Name","MSysObjects","Name='TableName' And Type In (1,4,6)")) Then
    'Table Exists
Run Code Online (Sandbox Code Playgroud)

但是,无法创建与查询同名的表,因此如果需要查找以测试名称,最好将5(即查询)添加到"类型"列表中.

  • 有:SELECT [Name] FROM MSysObjects WHERE [Name] ='TableName'并输入(1,4,6) (12认同)

Dav*_*ton 7

我测试了各种方法,以确定几年前是否存在表格.以下是我实现的所有代码,包括我的简单测试例程.

Public Function TableExists(strTableName As String, Optional ysnRefresh As Boolean, Optional db As DAO.Database) As Boolean
' Originally Based on Tony Toews function in TempTables.MDB, http://www.granite.ab.ca/access/temptables.htm
' Based on testing, when passed an existing database variable, this is the fastest
On Error GoTo errHandler
  Dim tdf As DAO.TableDef

  If db Is Nothing Then Set db = CurrentDb()
  If ysnRefresh Then db.TableDefs.Refresh
  Set tdf = db(strTableName)
  TableExists = True

exitRoutine:
  Set tdf = Nothing
  Exit Function

errHandler:
  Select Case Err.Number
    Case 3265
      TableExists = False
    Case Else
      MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error in mdlBackup.TableExists()"
  End Select
  Resume exitRoutine
End Function

Public Function TableExists2(strTableName As String, Optional ysnRefresh As Boolean, Optional db As DAO.Database) As Boolean
On Error GoTo errHandler
  Dim bolCleanupDB As Boolean
  Dim tdf As DAO.TableDef

  If db Is Nothing Then
     Set db = CurrentDb()
     bolCleanupDB = True
  End If
  If ysnRefresh Then db.TableDefs.Refresh
  For Each tdf In db.TableDefs
    If tdf.name = strTableName Then
       TableExists2 = True
       Exit For
    End If
  Next tdf

exitRoutine:
  Set tdf = Nothing
  If bolCleanupDB Then
     Set db = Nothing
  End If
  Exit Function

errHandler:
  MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error in mdlBackup.TableExists1()"
  Resume exitRoutine
End Function

Public Function TableExists3(strTableName As String, _
     Optional db As DAO.Database) As Boolean
' Based on testing, when NOT passed an existing database variable, this is the fastest
On Error GoTo errHandler
  Dim strSQL As String
  Dim rs As DAO.Recordset

  If db Is Nothing Then Set db = CurrentDb()
  strSQL = "SELECT MSysObjects.Name FROM MSysObjects "
  strSQL = strSQL & "WHERE MSysObjects.Name=" & Chr(34) & strTableName & Chr(34)
  strSQL = strSQL & " AND MSysObjects.Type=6;"
  Set rs = db.OpenRecordset(strSQL)
  TableExists3 = (rs.RecordCount <> 0)

exitRoutine:
  If Not (rs Is Nothing) Then
     rs.Close
     Set rs = Nothing
  End If
  Exit Function

errHandler:
  MsgBox Err.Number & ": " & Err.Description, vbCritical, _
     "Error in TableExists1()"
  Resume exitRoutine
End Function

Public Sub TestTableExists(strTableName As String, intLoopCount As Integer)
  Dim dteStart As Date
  Dim i As Integer
  Dim bolResults As Boolean

  dteStart = Now()
  For i = 0 To intLoopCount
    bolResults = TableExists(strTableName, , CurrentDB())
  Next i
  Debug.Print "TableExists (" & intLoopCount & "): " & Format(Now() - dteStart, "nn:ss")

  dteStart = Now()
  For i = 0 To intLoopCount
    bolResults = TableExists2(strTableName, , CurrentDB())
  Next i
  Debug.Print "TableExists2 (" & intLoopCount & "): " & Format(Now() - dteStart, "nn:ss")

  dteStart = Now()
  For i = 0 To intLoopCount
    bolResults = TableExists3(strTableName, CurrentDB())
  Next i
  Debug.Print "TableExists3 (" & intLoopCount & "): " & Format(Now() - dteStart, "nn:ss")
End Sub
Run Code Online (Sandbox Code Playgroud)


Kev*_*nen 6

这是另一种解决方案,比循环遍历所有表要快一些.

Public Function doesTableExist(strTableName As String) As Boolean
    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Set db = CurrentDb
    On Error Resume Next
    Set td = db.TableDefs(strTableName)
    doesTableExist = (Err.Number = 0)
    Err.Clear
End Function
Run Code Online (Sandbox Code Playgroud)

  • Err.Number = 0 是一个比较。它产生一个布尔值,然后将其分配给 doesTableExist。 (2认同)