
Check if a table exist doesn't work
I'm not clearon how the function is behaving. It should err out and return
FALSE if the table does not exist.
I don't understand the purpose of setting variable x equal to count of
fields collection.
I would suggest that a more straightforward "table exists" test function
might loop through the tables collection, returning TRUE if a match is
found:
Public Function TableExists(strDbPath as String, strTablename as String) as
Boolean
on error goto TableExists_Err
dim db as DAO.database
dim tdf as DAO.tabledef
'initialize return value
TableExists = FALSE
Set db = OpenDatabase(strDbPath)
For Each tdf in db.Tabledefs
If tdf.name = strTableName
TableExists = TRUE
Exit For
End if
Next tdf
TableExists_Exit:
on error resume next
Exit Function
TableExists_Err:
msgbox "TableExists" & vbcrlf & _
err.number & vbcrlf & err.description
Resume TableExists_Exit
End Function
Quote:
> HELP!!
> this generates a error were i do not get rid of it
> what are i am doing wrong
> it gives the error item not find in this collection (what is true
ofcourse)
> but error handeling does not react on this :(
> i need to check if a table exist ( if not i want to make it if it is i
need
> to append fields to it)
> thanx
> Function DoesTableExist(sTblName As String) As Boolean
> Dim dbs As dao.Database
> Dim tbldef As dao.TableDef
> Dim rst As Recordset
> Dim x
> On Error GoTo DoesTableExist_err
> Set dbs = OpenDatabase("C:\WindowsUren\NoordWind.mdb")
> Set tbldef = dbs.TableDefs(sTblName)
> x = dbs.TableDefs(sTblName).Fields.Count
> DoesTableExist = True
> DoesTableExist_end:
> Exit Function
> DoesTableExist_err:
> MsgBox Err.Number
> DoesTableExist = False
> resume DoesTableExist_end
> End Function