Checking to see if a table is empty

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dowlingm815
    New Member
    • Feb 2010
    • 133

    Checking to see if a table is empty

    A procedure is calling a function to see if a table is empty through a Boolean value, however, i am receiving a compiler error. any assistance, would be appreciated.

    the code is:

    Code:
        If TableExists = False Then
            DoCmd.DeleteObject acTable, "ContractImport"
            DoCmd.DeleteObject acTable, "ContractImport$_ImportErrors"
        End If
    Code:
    Public Function TableExists(strTable As String) As Boolean
        Dim strName As String
      
        On Error Resume Next
        'If table exists already then strName will be > ""
        strName = CurrentDb.TableDefs(strTable).Name
        TableExists = Not (strName = "")
    
    End Function
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    dowlingm815,
    Your function declaration
    Code:
    Function TableExists(strTable As String)
    indicates the function requires an input parameter, yet your call to the function
    Code:
    If TableExists = False Then
    is not supplying the needed parameter.

    You need to add the name of the table to the call to the function so it knows what table name to check.

    Jim

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by jimatqsi
      dowlingm815,
      Your function declaration
      Code:
      Function TableExists(strTable As String)
      indicates the function requires an input parameter, yet your call to the function
      Code:
      If TableExists = False Then
      is not supplying the needed parameter.

      You need to add the name of the table to the call to the function so it knows what table name to check.

      Jim
      As previously pointed ou by jamitqsi:
      Code:
      If TableExists("Employees") Then
        MsgBox "Exists"
      Else
        MsgBox "No exists"
      End If

      Comment

      • dowlingm815
        New Member
        • Feb 2010
        • 133

        #4
        thanks appreciate the correction. here is the working code:
        Code:
            If TableExists("ContractImport") Then
            ' delete the table if it exists
                DoCmd.DeleteObject acTable, "ContractImport"
            End If
            If TableExists("ContractImport$_ImportErrors") Then
            ' delete the table if it exists
                DoCmd.DeleteObject acTable, "ContractImport$_ImportErrors"
            End If
        Code:
        Public Function TableExists(strTable As String) As Boolean
           On Error GoTo Err_Hndlr
           
           Dim strName As String
          
        '    On Error Resume Next
            TableExists = True
            'If table exists already then strName will be > ""
            strName = CurrentDb.TableDefs(strTable).Name
            TableExists = Not (strName = "")
            Exit Function
            
        Err_Hndlr:
            TableExists = False
            
        End Function

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Given your circumstances, another option to consider is (In-Line Code):
          Code:
          Dim tdf As DAO.TableDef
          Const conTABLE_NAME As String = "ContractImport"
            
          For Each tdf In CurrentDb.TableDefs
            If Left$(tdf.Name, Len(conTABLE_NAME)) = conTABLE_NAME Then
              CurrentDb.TableDefs.Delete tdf.Name
            End If
          Next

          Comment

          • dowlingm815
            New Member
            • Feb 2010
            • 133

            #6
            Originally posted by ADezii
            Given your circumstances, another option to consider is (In-Line Code):
            Code:
            Dim tdf As DAO.TableDef
            Const conTABLE_NAME As String = "ContractImport"
              
            For Each tdf In CurrentDb.TableDefs
              If Left$(tdf.Name, Len(conTABLE_NAME)) = conTABLE_NAME Then
                CurrentDb.TableDefs.Delete tdf.Name
              End If
            Next
            I'm not sure if i follow the instructions, please keep me honest. it looks like it calculating the length of the table correct?

            mary

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by dowlingm815
              I'm not sure if i follow the instructions, please keep me honest. it looks like it calculating the length of the table correct?

              mary
              In a Nutshell, any Table beginning with ContractImport will be DELETED!

              Comment

              • dowlingm815
                New Member
                • Feb 2010
                • 133

                #8
                Originally posted by ADezii
                In a Nutshell, any Table beginning with ContractImport will be DELETED!
                that's awesome, but i still don't understand the code.

                the following code, reads each table NAME in the db, correct?

                Code:
                For Each tdf In CurrentDb.TableDefs
                the next statement, calculates the length of the string, but what does the rest do?

                Code:
                  If Left$(tdf.Name, Len(conTABLE_NAME)) = conTABLE_NAME Then
                i appreciate all your guidance.

                mary

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  Originally posted by dowlingm815
                  that's awesome, but i still don't understand the code.

                  the following code, reads each table NAME in the db, correct?

                  Code:
                  For Each tdf In CurrentDb.TableDefs
                  the next statement, calculates the length of the string, but what does the rest do?

                  Code:
                    If Left$(tdf.Name, Len(conTABLE_NAME)) = conTABLE_NAME Then
                  i appreciate all your guidance.

                  mary
                  The code loops through every Table in the Database and if the Table Name begins with the String defined in the Constant (conTABLE_NAME ), it is then DELETED! The code is compact and efficient, but the only drawback would be if you had Tables named:
                  Code:
                  ContractImport_2
                  ContractImport_New
                  ContractImport_06182010
                  ContractImportFK
                  ContractImportYaDaYaDa
                  ContractImport Error
                  They would all be DELETED!

                  Comment

                  Working...