Results 1 to 9 of 9
  1. #1
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328

    Table exists function in A2007


    I use the function fncTableExists = (TableName = CurrentDb.TableDefs(TableName).Name) successfully in A2000 & 2003. In A2007 I get the message "not in this collection". OS is W7 on 64 bit computer. Can someone help?

  2. #2
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    I always got error message when table not exist:
    CurrentDb.TableDefs(TableName).Name

  3. #3
    DaveT is offline Access Developer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    May 2010
    Location
    Texas
    Posts
    69
    A little longer than your code, but I use this in a code module:
    <code>
    Public Function IsTableInDefs(Y) As Boolean
    Dim db As DAO.Database
    Dim zLng As Long
    Dim i as Long 'added in later edit
    On Error Resume Next
    IsTableInDefs = False
    If Len(Nz(Y)) = 0 Then
    Exit Function
    End If
    Set db = CurrentDb
    With db
    zLng = .TableDefs.Count - 1 'tabledefs is base zero
    For i = 0 To zLng
    If .TableDefs(i).Name = Y Then
    IsTableInDefs = True
    Set db = Nothing
    Exit Function
    End If

    Next
    End With
    Set db = Nothing
    End Function
    </code>
    Last edited by DaveT; 09-02-2010 at 12:01 PM.

  4. #4
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    I do code but am a long way from pro. I tried your code by putting in a button that acutuated the expression IsTableInDefs ("table1"), with table1 being existent. I got an error message that i was not defined, so added statemetn dim i as long. OK? It did work. Could you tell me what the "with" statement is about. It loops through it about 6 times even thought there is only one table. Thanks very much for your time..

  5. #5
    DaveT is offline Access Developer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    May 2010
    Location
    Texas
    Posts
    69
    Yes, good get, you are correct the procedure (to stand on its own) needs Dim i as L ong.

    As a matter of practice, I usually declare i, j, and k as public in a standard module such as: Public i As Long, j As Long, k As Long

    With/End With is a short cut to refer to objects. Using With is equivalent to:
    ...
    Set db = CurrentDb

    zLng = db.TableDefs.Count - 1 'tabledefs is base zero
    For i = 0 To zLng
    If db.TableDefs(i).Name = Y Then
    ...

    Even though you have one table, the routine is stepping through all tables to include the hidden system tables such as MSysObjects.

    If you want to see these in 2007, use Access Options, Current Database, Navigation Options, check Show System Objects, then OK.

    Since Access uses these MSys tables internally, best to leave them hidden (and alone).

  6. #6
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    with statement means in the following codes, all variables/objects which are not defined should be a member of the with object. In above case, .tabledefs is an object of db (with db). if you don't use with db, you need to specify db.tabledefs every time.

    the code loops through it about 6 times even thought there is only one table, because hidden system tables are also scaned.

    Late post

  7. #7
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    Thanks much-I have a lot to learn about code.

  8. #8
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    Thanks for the help and for explaining things, much appreciated.

  9. #9
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    I am not sure that I specifically thanked you. Your code works and I will use it. Not sure why my old shorter code works in A2000 2003 but not here. Thanks much.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Installation Conflict with A2002 & A2007
    By ldmadison in forum Access
    Replies: 4
    Last Post: 08-25-2010, 01:16 PM
  2. Replies: 0
    Last Post: 04-15-2010, 01:34 PM
  3. SQL - find out whether a table exists
    By yurako in forum Programming
    Replies: 2
    Last Post: 01-20-2010, 09:27 AM
  4. Replies: 1
    Last Post: 11-30-2009, 05:05 AM
  5. Create Table If Not Exists
    By jp2access in forum Queries
    Replies: 7
    Last Post: 07-14-2009, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums