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 / March 2005

Tip: Looking for answers? Try searching our database.

Detect Table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
box2003 - 28 Mar 2005 21:46 GMT
I have a form which is used for import and export functions.  As part of
this process, I want to check if a table already exists in the database, so
I can create the table or clear the table rows before my import.  I need a
method of detecting the presence of a table before an error message tells me
a table does not exist when I run a process.  When the table is known not to
exist, I create it using code behind the form.  I would like to determine if
the table is actually there before going ahead a creating it.  Would I use
similar code as was used to created the table.  I do not want to check the
database window to verify the table, I would rather have the process
seamless so the user can maintain interaction at the form level.

Thank you for your assistance.
Dirk Goldgar - 28 Mar 2005 21:58 GMT
> I have a form which is used for import and export functions.  As part
> of this process, I want to check if a table already exists in the
[quoted text clipped - 9 lines]
>
> Thank you for your assistance.

There are lots of ways to do this.  Here's one:

'----- start of code -----
Function fncTableExists( _
       TableName As String, _
       Optional DBPath As String) _
   As Boolean

   Dim DB As DAO.Database

   If Len(DBPath) > 0 Then
       Set DB = DBEngine.OpenDatabase(DBPath)
   Else
       Set DB = CurrentDb
   End If

   If Len(TableName) = 0 Then Err.Raise 5

   On Error Resume Next
   fncTableExists = IsObject(DB.TableDefs(TableName))

   If Not DB Is Nothing Then
       DB.Close
       Set DB = Nothing
   End If

End Function

'----- end of code -----

The above function requires that you have a reference set to the
Microsoft DAO 3.x Object Library -- 3.6 for Access 2000 or later, 3.51
for Access 97.  To check for the existence of a table in the current
database, you would call it like this:

   If fncTableExists("SomeTableName") Then
       ' the table exists
   Else
       ' it doesn't
   End If

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Jeff Conrad - 28 Mar 2005 22:02 GMT
Public Function funcTableExists(strTable As String) As Boolean
On Error GoTo ErrorPoint

' This function will check to see if a
' table exists within the current database
' Similar to IsLoaded function it will return True or False
' Jeff Conrad - Access Junkie
' Usage:
' If funcTableExists("SomeTable") = True Then
'    ' Table Exists
' Else
'    ' Table does not exist
' End If
' **Requires DAO Reference be set**

   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

> I have a form which is used for import and export functions.  As part of
> this process, I want to check if a table already exists in the database, so
[quoted text clipped - 8 lines]
>
> Thank you for your assistance.
Dirk Goldgar - 28 Mar 2005 22:08 GMT
> Public Function funcTableExists(strTable As String) As Boolean
> On Error GoTo ErrorPoint
[quoted text clipped - 37 lines]
>
> End Function

Jeff -

If you find the table, you may want to go ahead and leave the loop:

       For Each doc In .Documents
           If doc.Name = strTable Then
               funcTableExists = True
               Exit For
           End If
       Next doc

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Jeff Conrad - 28 Mar 2005 22:15 GMT
> Jeff -
>
[quoted text clipped - 6 lines]
>             End If
>         Next doc

Hi Yoda,

Yeah, I realize the loop need not continue.
With a large database this could save a little time, but I'm usually working
with databases that do not have a whole bunch of tables.
Also, I just did not want the other tables to feel left out.
;-)

Thanks, and I will change the code.
Signature

Jeff Conrad
Access Junkie
Bend, Oregon

Rob Oldfield - 28 Mar 2005 22:31 GMT
> > Jeff -
> >
[quoted text clipped - 16 lines]
>
> Thanks, and I will change the code.

What?  You mean I should be taking the emotions of my objects into account?
Should I be using...

DoCmd.DeleteObject acTable, "TableName", acMyApologiesForDoingThis

and changing

DoCmd.RunSQL "Delete from tbl Where Obsolete"

to

DoCmd.RunSQL "Delete from tbl Where
StillAReallyGoodRecordButNeedsToBeInADifferentTableToReallyFulfillYourPotent
ial"

Should we start a new newsgroup for ethical coding?
Dirk Goldgar - 28 Mar 2005 22:34 GMT
> What?  You mean I should be taking the emotions of my objects into
> account? Should I be using...
[quoted text clipped - 8 lines]
>
> DoCmd.RunSQL "Delete from tbl Where

StillAReallyGoodRecordButNeedsToBeInADifferentTableToReallyFulfillYourPo
tent
> ial"
>
> Should we start a new newsgroup for ethical coding?

ROFL

"Never anthropomorphize conputers.  They HATE that!"

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Jeff Conrad - 28 Mar 2005 22:45 GMT
> What?  You mean I should be taking the emotions of my objects into account?
> Should I be using...
[quoted text clipped - 12 lines]
>
> Should we start a new newsgroup for ethical coding?

ROFL!!
Yes, precisely.

Remember the Junkie motto:
"Be nice to thine objects, or fail they will when showing
the app to prospective clients."

Signature

Jeff Conrad
Access Junkie
Bend, Oregon

 
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.