I use a tecnique that handles just such a problem, as access will think that the table is there if access can see the table. Seing the table does not, however mean that the table can be opend AND show the relevant data..... Grrrr had me spooked for a while, so the trick.. is to open the table and if an error occours, delete the table and reattach.
Function IsTableChk(TableName) As Boolean ' Is table in Database
On Error Resume Next
Dim DB As DAO.Database
Set DB = DBEngine.Workspaces(0).Databases(0)
Application.Echo False
DoCmd.OpenTable TableName
If Err <> 0 Then DoCmd.DeleteObject acTable, TableName Else IsTableChk = True
Exit_IsTable:
DoCmd.Close acTable, TableName
Application.Echo True
Exit Function
Err_IsTable:
IsTableChk = False
Resume Exit_IsTable
End Function
I have a local table with names of all attached tables if I find a table is not correctly attached I reattach the whole thing via:
Function AttachDatabase(AttDatabase)
On Error GoTo Err_AttachDatabase
Dim Re As DAO.Recordset, Tal As Integer
DoCmd.Hourglass True
Set Re = CurrentDb.OpenRecordset("Select * From Z_Tabel Where TabelType='YrName'")
Re.MoveLast
SysCmd acSysCmdInitMeter, "Attaching database", Re.RecordCount
Re.MoveFirst
Do While Not Re.EOF
AttachTable AttDatabase, Re!TabelNavn
Tal = Tal + 1
SysCmd acSysCmdUpdateMeter, Re.AbsolutePosition
Re.MoveNext
Loop
Exit_AttachDatabase:
SysCmd acSysCmdRemoveMeter
DoCmd.Hourglass False
Exit Function
Err_AttachDatabase:
MsgBox Error$, , "YrApp"
Resume Exit_AttachDatabase
End Function
Sub AttachTable(AttDatabase, AttTable)
On Error GoTo Err_AttachTable
Dim MyTable As TableDef, MyField As Field, Svar
On Error Resume Next
If Left(AttDatabase, 4) = "ODBC" Then
Set MyTable = CurrentDb.CreateTableDef(AttTable, dbAttachSavePWD, AttTable, AttDatabase & AttTable)
CurrentDb.TableDefs.Append MyTable
Else
CurrentDb.TableDefs(AttTable).Connect = ";DATABASE=" & AttDatabase
'Debug.Print Err & " - " & Error(Err)
If Err = 3265 Then
Set MyTable = CurrentDb.CreateTableDef(AttTable)
MyTable.Connect = ";DATABASE=" & AttDatabase
MyTable.SourceTableName = AttTable
CurrentDb.TableDefs.Append MyTable
Else
Set MyTable = CurrentDb.CreateTableDef(AttTable)
MyTable.Connect = ";DATABASE=" & AttDatabase
MyTable.SourceTableName = AttTable
CurrentDb.TableDefs.Delete (AttTable)
CurrentDb.TableDefs.Append MyTable
End If
End If
Exit_AttachTable:
Exit Sub
Err_AttachTable:
If Err <> 3265 Then MsgBox "Attach Table: " & Err.Description, , "YrApp"
Resume Exit_AttachTable
End Sub
Not sure how much of the above code you need, but try what you like and see if this does not solve your problem.
Herman
Say no to macros