Results 1 to 6 of 6

Thread: Check For THen Delete Table[RESOLVED: THANKS PEET!!]

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Check For THen Delete Table[RESOLVED: THANKS PEET!!]

    I need to check to see if a certain table exists in an Access database, and if it does, delete it. I'm lost.
    Last edited by SeanK; Aug 7th, 2002 at 06:03 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Dear Lost: Try this...

    VB Code:
    1. 'posted by peet
    2. '01/03/2002
    3. Private Sub Command1_Click()
    4. Dim db As Database
    5. Dim sDBN As String
    6. Dim SQL As String
    7.  
    8. sDBN = "C:\TEST.MDB"
    9. If TableExist(sDBN, "Table1") Then
    10.      Set db = OpenDatabase(sDBN)
    11.       SQL = "DROP TABLE Table1"
    12.       db.Execute SQL
    13. End If
    14. End Sub
    15.  
    16.  
    17. Private Function TableExist(sDBName As String, sTableName As String) As Boolean
    18. Dim i As Integer
    19. Dim db As Database
    20.  
    21. On Error GoTo xit
    22. 'open the database
    23. Set db = OpenDatabase(sDBName)
    24. 'go through all the tables and check their names
    25. For i = 0 To db.TableDefs.Count - 1
    26.            If UCase(sTableName) = UCase(db.TableDefs(i).Name) Then
    27.                    TableExist = True
    28.                    Exit Function
    29.            End If
    30. Next i
    31. 'If we get here ---> table doesn't exist
    32. TableExist = False
    33. Set db = Nothing
    34. Exit Function
    35. xit:
    36. TableExist = False
    37. Set db = Nothing
    38. MsgBox "The following unexpected error occured:" & vbCrLf & vbCrLf & Err.Number & " --> " & Err.Description, vbCritical
    39. End Function

  3. #3

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160
    Thanks Hack..it worked like a champ (with some minor modifications to suite my needs!)

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Don't thank me Sean, thank peet. He wrote/posted the code.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I hope peet sees this. He will love being thanked without ever, personnally, contributing anything.

    He might even break out his cheerleader outfit!!

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    sorry Hack, I don't think I have the strength of will to put away my only joy
    -= a peet post =-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width