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
5 9676
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. -
On Error Resume Next
-
DoCmd.RunSQL "DROP TABLE <TableName>;"
-
On Error GoTo 0
-
Regards,
Fish
Thank you! Out of curiosity, how would one check to see if an object exists, thus eliminating the need for the error handling?
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
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.
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: -
Dim tdf As TableDef
-
Dim intResponse As Integer
-
Dim blnTableExists As Boolean
-
Const conTABLE_TO_DELETE As String = "<Your Table Name>"
-
-
blnTableExists = False 'Initialize
-
-
For Each tdf In CurrentDb.TableDefs
-
If tdf.Name = conTABLE_TO_DELETE Then
-
blnTableExists = True
-
Exit For
-
End If
-
Next
-
-
If blnTableExists Then
-
intResponse = MsgBox("DELETE the Table [" & tdf.Name & "]?", vbQuestion + vbYesNo, "Table Deletion")
-
If intResponse = vbYes Then
-
CurrentDb.TableDefs.Delete tdf.Name
-
RefreshDatabaseWindow
-
End If
-
Else
-
MsgBox "Table [" & conTABLE_TO_DELETE & "] does not exist!", vbOKOnly + vbExclamation, "No Tableamundo!"
-
End If
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sam |
last post by:
Is there a simple way to test if a mysql table exists?
JUST One line !
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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,...
|
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?
...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |