N. Cotton said:
I current use this to test if a table exists in the front end:
If DCount("*", "MsysObjects", "[Name]='tblTempXLS'") > 0 Then
'Table Exists
How Would I amend this to test for a table in the back end?
You'd need to open a connection to the back-end database, and check
there. First you'd need to determine the path to the back-end, and then
you could open a DAO Database object on that path and check the
database's TableDefs collection. See the following two functions:
'----- start of code -----
Function fncDataDBName() As String
' Return the name (including path) of the "back-end" database
' containing the data this application is working with. If the
' data tables are local, not linked, return the name of the current
' database.
'*** NOTE: This code presumes that the front-end database contains
' a linked table named "tblProfile". Change that as needed for
' your application.
Static strDBFileName As String
On Error Resume Next
If Len(strDBFileName) = 0 Then
strDBFileName = CurrentDb.TableDefs("tblProfile").Connect
If Left$(strDBFileName, 10) = ";DATABASE=" Then
strDBFileName = Mid$(strDBFileName, 11)
Else
strDBFileName = CurrentDb.Name
End If
End If
fncDataDBName = strDBFileName
End Function
Function fncTableExists( _
TableName As String, _
Optional DBPath As String) _
As Boolean
Dim db As DAO.Database
If Len(DBPath) > 0 Then
Set db = DBEngine.OpenDatabase(DBPath)
Else
Set db = CurrentDb
End If
If Len(TableName) = 0 Then Err.Raise 5
On Error Resume Next
fncTableExists = IsObject(db.TableDefs(TableName))
If Not db Is Nothing Then
db.Close
Set db = Nothing
End If
End Function
'----- end of code -----
Given such functions (after substituting one of your linked table names
in fncDataDBName as indicated), you can check for table "TableX" in the
back-end using code like this:
If fncTableExists("TableX", fncDataDBName()) Then
' ... the table exists in the back-end
Else
' ... it doesn't.
End If