473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Test to see if an object (table) exists

46
Hello,

I have a batch of code that imports data from an Excel file to an Access table. Sometimes the import creates a table with import errors and sometimes it does not. I would like the code to check to see if the table exists (call it "tbl_errors") and if it does, delete the table.

Seems like this would be an if statement but I am unsure how to approach. Any help would be appreciated.

-Brian
Sep 9 '08 #1
5 9676
FishVal
2,653 Expert 2GB
Hello, Brian.

Though there are several ways to check table existance, it is really not needed if you know exact table name.
Just try to delete it while ignoring possible errors.
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2. DoCmd.RunSQL "DROP TABLE <TableName>;"
  3. On Error GoTo 0
  4.  
Regards,
Fish
Sep 9 '08 #2
bbatson
46
Thank you! Out of curiosity, how would one check to see if an object exists, thus eliminating the need for the error handling?
Sep 9 '08 #3
FishVal
2,653 Expert 2GB
Thank you! Out of curiosity, how would one check to see if an object exists, thus eliminating the need for the error handling?
  • You may use Access hidden table [MSysObjects] where info about all Access objects (including tables is stored)
  • You may iterate CurrentDb.TableDefs collection

Regards,
Fish

P.S. "eliminating the need for the error handling" I will add to memories :D
Sep 9 '08 #4
bbatson
46
Thanks. For what it's worth, sometimes I prefer to get an actual error as opposed to a specified set of code that may or may not alert that something has gone awry. Thanks again.
Sep 9 '08 #5
ADezii
8,834 Expert 8TB
Thank you! Out of curiosity, how would one check to see if an object exists, thus eliminating the need for the error handling?
The following code will let you know one way or the other if a Table exists, and will also prompt you prior to Deletion. Substitute your own Table Name for the Constant:
Expand|Select|Wrap|Line Numbers
  1. Dim tdf As TableDef
  2. Dim intResponse As Integer
  3. Dim blnTableExists As Boolean
  4. Const conTABLE_TO_DELETE As String = "<Your Table Name>"
  5.  
  6. blnTableExists = False      'Initialize
  7.  
  8. For Each tdf In CurrentDb.TableDefs
  9.   If tdf.Name = conTABLE_TO_DELETE Then
  10.     blnTableExists = True
  11.       Exit For
  12.   End If
  13. Next
  14.  
  15. If blnTableExists Then
  16.   intResponse = MsgBox("DELETE the Table [" & tdf.Name & "]?", vbQuestion + vbYesNo, "Table Deletion")
  17.   If intResponse = vbYes Then
  18.     CurrentDb.TableDefs.Delete tdf.Name
  19.     RefreshDatabaseWindow
  20.   End If
  21. Else
  22.   MsgBox "Table [" & conTABLE_TO_DELETE & "] does not exist!", vbOKOnly + vbExclamation, "No Tableamundo!"
  23. End If
  24.  
Sep 10 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: sam | last post by:
Is there a simple way to test if a mysql table exists? JUST One line !
2
by: David | last post by:
My email application was working a couple of weeks ago, then all of a sudden I get Error Type: (0x8009000F) Object already exists. I checked the permissions on the machinekeys directory...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
7
by: Simon Wigzell | last post by:
You'd think this would be the most basic sql query in the world but noooooo! I've tried this: on error resume next strsql = "SELECT * FROM " & session("TablePrefix") & CurrentTable SET rs =...
3
by: DottingTheNet | last post by:
i promise no more silly questions after this but how do i check if a table exists?? it is my understanding that the exists can only be used in the where clause of the query. i just want s'thing...
7
by: G G | last post by:
I need to check if a table exists in Access using ADO externally. I tried "Select Name from MSysObjects Where Name = 'myTable'" with the ADO command object, but I got an error than I don't...
10
by: Geoff Jones | last post by:
Hi I'm trying to drop a table by using: Dim cmd As New OleDbCommand("DROP TABLE IF EXISTS books", myconnection) cmd.ExecuteNonQuery() but I get a syntax error: "Syntax error in DROP TABLE...
5
by: phillip.s.powell | last post by:
$sql = "SELECT IF((SHOW TABLES LIKE '$subselectTableName'), count(*), NULL) AS numRows FROM $subselectTableName"; I am trying to write a SQL statement that will tell me if a table exists or not,...
1
by: sampalmer21 | last post by:
Is there a way I can find out if a database I have created exists in the sql server using ado.net methods? How about determining if a table exists, or any other database object for that matter? ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.