Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting a table that does not exist 1

Status
Not open for further replies.

Chesterex1

Vendor
Mar 31, 2003
34
CA
In one of my procedures, I delete a table.
Occassionally the table does not exist.. How can I reference the table in VB Code to bypass the docmd.DeleteObject code if the table does not exist.
IE the Syntax for the pseudo code
IF tblData1 does not exist then....

Thanks a Bunch
Chester
 
I'm currently working on something that has just that type of functionality :)
Here's the Sub I created. It asks for a (text) file to insert, parses the name (for that's what the sheet name will be), then checks to see if it already exists. If it does, it trys to remove it, then continues adding the new sheet. The sheet name is then put in cell A1 of Sheet1 for reference.

-Swifty
---
Sub OpenThings()
'
' OpenThings Macro
'
Dim fileName As String
Dim pathName As String
Dim sheetName As String
Dim parsed() As String

fileName = Application.GetOpenFilename
parsed = Split(fileName, ".")
pathName = parsed(0)

Do Until Left(sheetName, 1) = "\"
iCount = iCount + 1
sheetName = Right(pathName, iCount)
If iCount = Len(pathName) Then Exit Do
Loop
sheetName = Right(sheetName, Len(sheetName) - 1)

For Each ws In Worksheets
If ws.Name = sheetName Then
Sheets(sheetName).Select
Application.ActiveSheet.Delete
End If
Next

Workbooks.OpenText fileName:=fileName, Origin:= _
xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1)
Sheets(sheetName).Move after:=ThisWorkbook.Sheets(1)
Sheets("Sheet1").Select
Range("A1").Select
Application.ActiveCell = sheetName

End Sub
 
Hi Chester,

I presume you are asking about Access from your mention of tables and the easiest thinhg to do is simply to ignore errors:

Code:
On Error Resume Next
tjDatabase.TableDefs.Delete Table1
Code:
 ' or however you delete your table
Code:
On Error GoTo 0

But if you want to check for existence then you must code your own function:

Code:
Function TableExists(strTableName As String) As Boolean

TableExists = False

Dim tjDatabase As DAO.Database
Dim tjTable As DAO.TableDef

Set tjDatabase = CurrentDb

For Each tjTable In tjDatabase.TableDefs
    If tjTable.Name = strTableName Then
        TableExists = True
        Exit For
    End If
Next

Set tjTable = Nothing
Set tjDatabase = Nothing

End Function

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top