Question : Object not found in the collection using IsObject

I am trying to send a table name to a module and the module is intended to determine whether the table already exists (in which case delete it) and then create a new table. The bit of code that calls the module is this:

        strName = CStr(Format(startDate, "mmm" & "-" & "yy"))
        tblName = "CareDays-" & strName
        createTableDef (tblName)

The createTableDef module code is attached below. When I step through the code, I get an "Object not found in this collection" error when I get to the line:

        If IsObject(db.TableDefs(tblNm)) Then

It seems that if the object is not found, then the code after "End If" would execute, but it doesn't. It just hops right out of the module back into the calling module and continues from there. But from there I end up working with a "nothing" recordset because the table was not created. Can anyone show me what I am doing wrong?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
Public Sub createTableDef(tblNm As String)
 
    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Dim fld1 As DAO.Field
    Dim fld2 As DAO.Field
    Dim fld3 As DAO.Field
    Dim fld4 As DAO.Field
    Dim fld5 As DAO.Field
    Dim fld6 As DAO.Field
    Dim fld7 As DAO.Field
    Dim fld8 As DAO.Field
    Dim fld9 As DAO.Field
    Dim fld10 As DAO.Field
    Dim fld11 As DAO.Field
    
    Set db = CurrentDb
    
        If IsObject(db.TableDefs(tblNm)) Then
            DoCmd.DeleteObject acTable, tblNm
        End If
 
        Set td = db.createTableDef(tblNm)
        Set fld1 = td.CreateField("Species", dbText, 20)
        Set fld2 = td.CreateField("LH", dbLong)
        Set fld3 = td.CreateField("MCN", dbLong)
        Set fld4 = td.CreateField("MCNII", dbLong)
        Set fld5 = td.CreateField("MCNIII", dbLong)
        Set fld6 = td.CreateField("MRBIII", dbLong)
        Set fld7 = td.CreateField("PHV", dbLong)
        Set fld8 = td.CreateField("PRB", dbLong)
        Set fld9 = td.CreateField("VA", dbLong)
        Set fld10 = td.CreateField("VUIIS", dbLong)
        Set fld11 = td.CreateField("WH", dbLong)
        
        td.Fields.Append fld1
        td.Fields.Append fld2
        td.Fields.Append fld3
        td.Fields.Append fld4
        td.Fields.Append fld5
        td.Fields.Append fld6
        td.Fields.Append fld7
        td.Fields.Append fld8
        td.Fields.Append fld9
        td.Fields.Append fld10
        td.Fields.Append fld11
 
        
        db.TableDefs.Refresh
        Application.RefreshDatabaseWindow
        
End Sub

Answer : Object not found in the collection using IsObject

I believe you need to trap for that error number ... which indicates that the table does not exist ... which is what you are checking for.  

mx
Random Solutions  
 
programming4us programming4us