Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 18th, 2006, 07:20 AM
Authorized User
 
Join Date: Jul 2004
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Default Table Exist?

I frequently use SysCmd to check whether a form is open or not and it works very well. However, is there a variant of SysCmd that can tell me whether a table exists or not within the current (or even a remote) database? It can be done by scrolling through the tableDefs Collection in a For Each tdf in CurrentDb.TableDefs... Next and checking whether tdf.Name equals what I am searching for, but this seems a large waste of resources. There must be a better way?



 
Old January 18th, 2006, 11:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

I think you're stuck with looping. While there's nothing quite as compact as SysCmd, I take the same approach and wrap my “table exists” functionality in a TableExists function, in the same way that I wrap SysCmd in an IsLoaded function. The following works with Jet or SQL Server (uses ADO)

Code:
Public Function TableExists(strTableName As String) As Boolean

   On Error GoTo Errorhandler:

   TableExists = False

   'g_cnn is the global, default ADO connnection my app is
   'using to any OLE DB Provider (Jet, SQL Server, etc.).
   'OpenSchema returns a read-only, static cursor of db
   'schema (catalog) information. adSchemaTables specifies a schema
   'query that returns tables and views (or Jet saved queries).
   With g_cnn.OpenSchema(adSchemaTables)
      If (False = .BOF) And (False = .EOF) Then
         .MoveFirst
         Do While Not .EOF
            'TABLE_NAME is a column-name defined in the returned
            'TABLES rowset.
            If 0 = StrComp(.Fields("TABLE_NAME"), strTableName, vbTextCompare) Then
               TableExists = True
               Exit Do
            End If
            .MoveNext
         Loop
      End If
      .Close
   End With
   Exit Function

Errorhandler:
   Debug.Print "Error: " & Err.Number, Err.Description, "clsCreateTempTable.TableExists"
   Exit Function
   Resume
End Function
Test with:

Code:
Global g_cnn As ADODB.Connection

Sub Test()

   Dim blnTableExists As Boolean          
   Set g_cnn = New ADODB.Connection

   'Open unsecured Jet connection
   With g_cnn
      .Provider = "Microsoft.Jet.OLEDB.4.0"
      .Properties("Data Source") = "C:\Unsecured.mdb"
   End With

   'Open secured Jet connection
   'With g_cnn
   '   .Provider = "Microsoft.Jet.OLEDB.4.0"
   '   .Properties("Data Source") = "C:\SecuredDatabase.mdb"
   '   .Properties("Jet OLEDB:System Database") = "C:\WorkgroupInformationFile.mdw"
   '   .Properties("Mode") = adModeShareDenyNone
   '   .Properties("User ID") = "username"
   '   .Properties("Password") = "password"
   'End With

   'Open SQL Server connection
   'With g_cnn
   '   .Provider = "SQLOLEDB"
   '   .Properties("Data Source") = "(local)"
   '   .Properties("Initial Catalog") = "database"
   '   .Properties("User ID") = "sa"
   '   .Properties("Password") = "password"
   'End With

   g_cnn.Open

   blnTableExists = TableExists("tblRecords")


End Sub
HTH,

Bob

 
Old January 18th, 2006, 12:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

For Jet tables only, if you're using CurrentProject.Connection, you can query the MSysObjects table. Not sure if you can read MSysObjects on a remote connection.

Code:
Public Function JetTableExists(strTableName As String) As Boolean

   Dim strSQL As String
   Dim rst As ADODB.Recordset
   Set rst = New ADODB.Recordset

   On Error GoTo Errorhandler:

   JetTableExists = False

   strSQL = "SELECT COUNT(*) FROM MSYSObjects WHERE Type = 1" & _
        " AND [Name]='" & strTableName & "'"
 
    Set rst = g_cnn.Execute(strSQL)
    JetTableExists = rst(0)
    rst.Close
    Set rst = Nothing
    Exit Function

Errorhandler:
   Debug.Print "Error: " & Err.Number, Err.Description, "clsCreateTempTable.TableExists"
   Exit Function
   Resume
End Function
Bob

 
Old January 18th, 2006, 01:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

I quess you could take the query approach with SQl Server too, and dispense with loopping for both Jet and SQL Server.

Public Function SQLServerTableExists(strTableName As String) As Boolean

   Dim strSQL As String
   Dim rst As ADODB.Recordset
   Set rst = New ADODB.Recordset

   On Error GoTo Errorhandler:

   SQLServerTableExists = False

   strSQL = "SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='" & strTableName & "'"

    Set rst = g_cnn.Execute(strSQL)
    If Not (rst.BOF And rst.EOF) Then SQLServerTableExists = True
    rst.Close
    Set rst = Nothing
    Exit Function

Errorhandler:
   Debug.Print "Error: " & Err.Number, Err.Description, "clsCreateTempTable.TableExists"
   Exit Function
   Resume
End Function

Bob

 
Old January 18th, 2006, 01:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Here’s the remote MSysObjects fix:

In the remote Jet DB:

1. Tools-->Options-->View-->Show-->check System objects.

2. Tools-->Security-->User and Group Permissions-->Object Type = Tables --> Select Msysobjects from Object Name List --> Assign Read Data Permissions.

Bob


 
Old January 23rd, 2006, 03:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Yet another economical, "loopless" way to see if a table exists. Just trap the error:

Function TableExists(strTableName As String) As Boolean
    Dim dbs As DAO.Database, tdf As DAO.TableDef

    On Error Resume Next
    Set dbs = CurrentDb
    Set tdf = dbs.TableDefs(strTableName)
    If Err = 3265 Then
        ' Table does not exist.
        TableExists = False
    Else
        ' Table exists.
        TableExists = True
    End If
    Err = 0
End Function

Bob






Similar Threads
Thread Thread Starter Forum Replies Last Post
Return a record that doesn't exist in a table? Unregistered SQL Server 2000 4 May 3rd, 2006 01:27 AM
Error :Provider (0x80040E37)Table does not exist. thahir Classic ASP Components 0 September 12th, 2005 12:02 AM
Determine if a table exist rdfernandez Access VBA 4 March 30th, 2005 04:40 AM
Table 'buzzly_comicsite.forum_admin' doesn't exist AMP_Engineer BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 17th, 2004 12:01 PM
Table 'buzzly_comicsite.forum_admin' doesn't exist AMP_Engineer BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 September 4th, 2004 06:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.