Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

Finding a db object by name for an if...then statement

Discussion in 'Information Technology' started by rls0905, Jul 28, 2006.

  1. rls0905

    rls0905
    Expand Collapse
    Guest

    I am trying to write code that will look to see if a table exists with a
    given name.
    If obj.Name = "xxx" ("exists") Then....
    How can this be done?
     
  2. Loading...

    Similar Threads Forum Date
    The Salmon Story, Finding Home Sikh Sikhi Sikhism Jul 29, 2016
    Sikhi Guru Nanak's Methodology Of Finding Truth Sikh Sikhi Sikhism Jan 1, 2016
    Movies Ashdoc's movie review---Finding Fanny ( English version ) Theatre, Movies & Cinema Sep 15, 2014
    S Asia Sikh Girls in Afghanistan Face Difficulty in Finding Mr. Right Breaking News Jul 31, 2013
    Finding True Guru Gurmat Vichaar Jan 7, 2012

  3. Ofer Cohen

    Ofer Cohen
    Expand Collapse
    Guest

    Create a function in a module that get a table name and return True if exist
    and False if it doesn't exist

    Function CheckTables( TableName as String) As Boolean

    Dim i As Integer

    On Error Resume Next
    CheckTables=False
    For i = 0 To CurrentDb.TableDefs.Count - 1
    If CurrentDb.TableDefs(i).Name = TableName Then
    CheckTables = True
    Exit Function
    End if
    Next

    End Function

    In your code
    If Not CheckTables("MyTableName") Then
    ' Doesn't exist
    End if

    --
    Good Luck
    BS"D


    "rls0905" wrote:

    > I am trying to write code that will look to see if a table exists with a
    > given name.
    > If obj.Name = "xxx" ("exists") Then....
    > How can this be done?
     
  4. Allen Browne

    Allen Browne
    Expand Collapse
    Guest

    Function TableExists(strTable As String) As Boolean
    Dim varDummy As Variant
    On Error Resume Next
    varDummy = CurrentDb().TableDefs(strTable)
    TableExists = (Err.Number = 0&)
    End Function

    --
    Allen Browne - Microsoft MVP. Perth, Western Australia.
    Tips for Access users - http://allenbrowne.com/tips.html
    Reply to group, rather than allenbrowne at mvps dot org.

    "rls0905" <rls0905@discussions.microsoft.com> wrote in message
    news:5365FC55-6C42-4D93-B5AA-DC2193EE3781@microsoft.com...
    >I am trying to write code that will look to see if a table exists with a
    > given name.
    > If obj.Name = "xxx" ("exists") Then....
    > How can this be done?
     
  5. fredg

    fredg
    Expand Collapse
    Guest

    On Mon, 22 May 2006 08:28:02 -0700, rls0905 wrote:

    > I am trying to write code that will look to see if a table exists with a
    > given name.
    > If obj.Name = "xxx" ("exists") Then....
    > How can this be done?


    Public Sub GetTableNames()
    Dim dbs As Database, tdf As TableDef
    Set dbs = CurrentDb
    For Each tdf In dbs.TableDefs
    If tdf.Name = "YourTableName" Then
    MsgBox "Yes the table exists."
    End If
    Next tdf

    Set dbs = Nothing
    End Sub
    =========
    If the table exists you will get a message, otherwise no message.

    Or.. why not simple open the table and trap the error?

    On Error GoTo This_Err
    DoCmd.OpenTable "YourTableName"

    Exit_This_Sub:
    Exit Sub
    This_Err:
    If Err = 7874 Then
    MsgBox "No Table by this name"
    Else
    MsgBox "Error " & Err.Number & " " & Err.Description
    End If
    Resume Exit_This_Sub
    --
    Fred
    Please respond only to this newsgroup.
    I do not reply to personal e-mail
     
  6. Pieter Wijnen

    Pieter Wijnen
    Expand Collapse
    Guest

    very expensive
    use allen browne's way
    or get used to MSysObjects (no errors that way <g>)

    Pieter

    "fredg" <fgutkind@example.invalid> wrote in message
    news:leif0vqlzfve$.1dmvhg86zp5tj.dlg@40tude.net...
    > On Mon, 22 May 2006 08:28:02 -0700, rls0905 wrote:
    >
    >> I am trying to write code that will look to see if a table exists with a
    >> given name.
    >> If obj.Name = "xxx" ("exists") Then....
    >> How can this be done?

    >
    > Public Sub GetTableNames()
    > Dim dbs As Database, tdf As TableDef
    > Set dbs = CurrentDb
    > For Each tdf In dbs.TableDefs
    > If tdf.Name = "YourTableName" Then
    > MsgBox "Yes the table exists."
    > End If
    > Next tdf
    >
    > Set dbs = Nothing
    > End Sub
    > =========
    > If the table exists you will get a message, otherwise no message.
    >
    > Or.. why not simple open the table and trap the error?
    >
    > On Error GoTo This_Err
    > DoCmd.OpenTable "YourTableName"
    >
    > Exit_This_Sub:
    > Exit Sub
    > This_Err:
    > If Err = 7874 Then
    > MsgBox "No Table by this name"
    > Else
    > MsgBox "Error " & Err.Number & " " & Err.Description
    > End If
    > Resume Exit_This_Sub
    > --
    > Fred
    > Please respond only to this newsgroup.
    > I do not reply to personal e-mail
     

Share This Page