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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Table function 2

Status
Not open for further replies.

DHSBW

Technical User
Jan 23, 2007
11
US
Hi:

The function below deletes a specific table with the user supplying the table name, and deletes it if it does. But the function returns an error "table not found" if the table name has a space in it. How can I get it to accept the space? Thanks for the help in advance.


Function DeleteTables(sTableName As String)
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Set dbs = CurrentDb()
DoCmd.SetWarnings False
For Each tdf In dbs.TableDefs

If tdf.Name = sTableName Then
CurrentDb.Execute "DROP TABLE " & sTableName
 
Try this:
Code:
CurrentDb.Execute "DROP TABLE '" & sTableName & "'"

Sure, the early bird gets the worm, but the second mouse gets the cheese in the trap.
 
Thanks for the reply. Now I'm getting a "syntax error in drop table or drop index" message.
 
Oops. So much for my stroke of genius.
Pretty sure the syntax error is mine.
Sorry can't be of more help....
[banghead]

Sure, the early bird gets the worm, but the second mouse gets the cheese in the trap.
 
You need to enclose the table name in square brackets:

Code:
CurrentDb.Execute "DROP TABLE [" & sTableName & "]"

The looping through each tabledef is also pointless.

The following should work for you:

Code:
Public Sub DropTable(ByVal strTableName As String)
    CurrentDb.Execute "DROP TABLE [" & strTableName & "];", dbFailOnError
End Sub

Please do not feed the trolls.....
 
Another approach if you need to test if the table exists before dropping it.
Code:
Function DoesTableExist(TableName As String) As Boolean
Dim rs      As ADODB.Recordset

[blue]'uses the schema to see if the temptable currently exists.  Only looks for the specific
'table name.  Doesn't loop thru all tables.
'Values in the array are  Array(TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME)
'Since we are looking for a TABLE NAME we populate the 3 constraint and leave the rest empty. 
'conn = ADODB connection [/blue]

Set rs = conn.OpenSchema(adSchemaTables, Array(Empty, Empty, TableName, Empty))
    DoesTableExist = Not (rs.BOF And rs.EOF)
    rs.Close
Set rs = Nothing

End Function

Then you could pass the function the table name and drop based on the value of DoesTableExists.

Code:
If DoesTableExist(YourTable) then
[blue]'Drop table[/blue]
end if
 
WOW!
jadams inadvertently solved a problem I never even posted.
Thanks, and a star!
[cheers]

Sure, the early bird gets the worm, but the second mouse gets the cheese in the trap.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top