Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / January 2005

Tip: Looking for answers? Try searching our database.

how to find if a table is in a database?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frederick Wilson - 18 Jan 2005 23:56 GMT
Currently I am cycling through all the tables in the database to find
out if a particular table is present. Is there a way to directly query
to find out if a table is present?

Thank you,
Fred
Jeff Conrad - 19 Jan 2005 00:35 GMT
Query to find out if a table exists in the database???
Not sure about that.

I just use a little function I wrote to check for this type of thing.
Just copy/paste into a new standard module.
Is this what you are looking for?

Public Function funcTableExists(strTable As String) As Boolean
On Error GoTo ErrorPoint

' Jeff Conrad - Access Junkie
' ? 2004 Conrad Systems Development
' This function will check to see if a table exists within
' the current database.
' Must have reference set to DAO object library
' Similar to IsLoaded function it will return True or False
' Call like so:
' If funcTableExists("TableNameHere") = True Then
'    ' Table Exists
' Else
'    ' Table does not exist
' End If

   Dim db As DAO.Database
   Dim doc As DAO.Document

   Set db = CurrentDb()

   With db.Containers!Tables
       For Each doc In .Documents
           If doc.Name = strTable Then
               funcTableExists = True
           End If
       Next doc
   End With

ExitPoint:
   On Error Resume Next
   Set db = Nothing
   Exit Function

ErrorPoint:
   MsgBox "The following error has occurred:" _
   & vbNewLine & "Error Number: " & Err.Number _
   & vbNewLine & "Error Description: " & Err.Description _
   , vbExclamation, "Unexpected Error"
   Resume ExitPoint

End Function

Signature

Jeff Conrad
Access Junkie
Bend, Oregon

> Currently I am cycling through all the tables in the database to find
> out if a particular table is present. Is there a way to directly query
> to find out if a table is present?
>
> Thank you,
> Fred
Marshall Barton - 19 Jan 2005 00:43 GMT
>Currently I am cycling through all the tables in the database to find
>out if a particular table is present. Is there a way to directly query
>to find out if a table is present?

Even though it hasn't change in twelve years, this is not
guaranteed to true in the future.

You can query the MSysObjects table to retrieve all table
names:

SELECT [Name]
FROM MSysObjects
WHERE [Type] = 1

or determine if a string is the name of an existing table:

SELECT Count(*)
FROM MSysObjects
WHERE [Type] = 1
    AND [Name] = "mytablename"

which will return a single record with a single field
containing a 0 if mytablename is not a table name or a 1 if
it is.

Signature

Marsh
MVP [MS Access]

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.