General discussion


Verify if a table exists or not

By amed30 ·
Can I ask you a question? I have an
Access db and working with ASP I'd like
to be able to verify if a table exists
on my db and if not to create it on the
fly. For the creation I have no
problem, but I can't verify the
existence of the table with SQL; how
can I do? I read about the object
sysobjects, but I can't use it. Can you
please suggest me how can I do?
Thanks to all

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

Check it exists

by FarmerB In reply to Verify if a table exists ...

I am not an Access guru, but the following line may be used with SQL Server:

If Exists (SELECT * FROM <table&gt
-- do something

Collapse -

Verify if a table exists or not

by ganesh.babu In reply to Verify if a table exists ...

Try TableDefs Collection if the Definition of the Table is present..

Collapse -


by juleeus In reply to Verify if a table exists ...

if db_name() = 'DATABASENAME' and
(select count(*) from sysobjects
where name = 'TABLENAME') = 1
print 'Table Exist'
print 'Table does not Exist'

Collapse -

Here's a VB function

by Glen_McLeod In reply to Verify if a table exists ...

Set a reference to ADO 2.5 for DDL and security.

Private Function TableExists(conMyDB as ADODB.Connection, Byval sTableName as string) as Boolean

Dim cat as ADOX.Catalog
Dim tb as ADOX.Table

On Error Resume Next
Set cat = New ADOX.Catalog
'conMyDB is an already opened ADO connection to your database
Set cat.ActiveConnection = conMyDB
Set tb = cat.Tables(sTableName)

If Err.Number = 0 then
'No error
TableExists = True
Set tb = Nothing
'table doesn't exist
TableExists =False
End If

Set cat = Nothing

End Function

You can adapt this pretty easily to VBS, or simply compile it into a COM object and call it there.


Related Discussions

Related Forums