473,396 Members | 1,804 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,396 software developers and data experts.

Need to Delete a Table if Found to Exist

Hello,
First, I must admit, that I am not trained in coding with VBA. I use MS Access macros and queries to build my application. I use some temporary import files, and need to either run a macro, or some VBA, to test if they exist, and then if they do, to delete them.

My table name is "TempImport1"

I've researched this via google searches and have come across some VBA that might work, but I am lost trying to figure out how to put the code into a module or a click sub button. I have cut/pasted VBA code under a button function in the past, and it worked, but I can't figure out why it's not working this time.

Honestly, I'm sure it's my lack of understanding of private vs public functions, as well as of course, the fact that I don't know VBA. Thank you for any advice you can provide.

Here's the code I'm trying to make work:
Expand|Select|Wrap|Line Numbers
  1. Function IsTable(sTblName As String) As Boolean
  2.     'does table exists and work ?
  3.     'note: finding the name in the TableDefs collection is not enough,
  4.     '      since the backend might be invalid or missing
  5.  
  6.     On Error GoTo TrapError
  7.     Dim x
  8.     x = DCount("*", sTblName)
  9.     IsTable = True
  10.     Exit Function
  11. TrapError:
  12.     Debug.Print Now, sTblName, Err.Number, Err.Description
  13.     IsTable = False
  14.  
  15. End Function
May 7 '17 #1

✓ answered by NeoPa

Let's take it one step at a time. We'll start with a simple procedure that is self-contained and will ensure that if a named table exists then it is deleted. That's my reading of what you're after. Please let us know if I have that wrong.

We can move on later to other questions, posted in separate threads, which cover some of the other basics.
Expand|Select|Wrap|Line Numbers
  1. Private Sub LoseTable(strTable As String)
  2.     On Error Resume Next
  3.     Call DoCmd.DeleteObject(ObjectType:=acTable, ObjectName:=strTable)
  4. End Sub
NB. It's not at all rare to see piles of code where only a couple of lines are required. This comes from either the developer having a limited understanding of the requirements or the developer getting code from somewhere where the requirements are different from their own in some way(s).

1 2466
NeoPa
32,556 Expert Mod 16PB
Let's take it one step at a time. We'll start with a simple procedure that is self-contained and will ensure that if a named table exists then it is deleted. That's my reading of what you're after. Please let us know if I have that wrong.

We can move on later to other questions, posted in separate threads, which cover some of the other basics.
Expand|Select|Wrap|Line Numbers
  1. Private Sub LoseTable(strTable As String)
  2.     On Error Resume Next
  3.     Call DoCmd.DeleteObject(ObjectType:=acTable, ObjectName:=strTable)
  4. End Sub
NB. It's not at all rare to see piles of code where only a couple of lines are required. This comes from either the developer having a limited understanding of the requirements or the developer getting code from somewhere where the requirements are different from their own in some way(s).
May 7 '17 #2

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

Similar topics

3
by: Michael Magg | last post by:
Hello! I wanna import a table with a makro, but a table with this name already exists. So the imported table (f. i. "table") is saved as "table1". How can I check, if such a table already exists...
3
by: vijaykokate | last post by:
Our company http://www.softnmation.com/ offers its customers a great variety of products. Everything you need can be found in this site. Web Template, CSS Template, Logo Template, Corporate...
2
by: Scott | last post by:
I have a macro set up to delete a table and then it imports an up-to-date copy of the table. Every once in a while the table gets deleted but the new one isn't imported, I assume the user is...
2
by: Kenxo | last post by:
Hello, I am new at scripting and having some problems with syntax on EXECUTE cmd I was traying to reset the primary key on an access db table but I thought it was easier to delete the table and...
3
by: bbatson | last post by:
Hello, Does anyone know how to avoid the 'delete table' warning that comes with a make-table query? As an example, I am referring to this message box: ...
3
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be...
3
by: maheswaran | last post by:
May this question exist....But i confused...I want to check whether the table is exist or not .... Like i run a project , on that project i create a temp tables while run time.... on that i want...
2
by: anoop s | last post by:
I want to delete records of one table that exist in another table. The tables have no primary key. It will take a lot of time compare field by field. Is there any method record by record comparison...
3
by: Omar Awan | last post by:
I have an unbound control "CC_Security_1_TextBox" that gets its data from a crosstab query tied to the form. What i need is code that would update the table "CC Security" which has two columns: User...
3
by: fm mamun | last post by:
Hi, I have deleted all record somehow. I am not delete table. But all records has been deleted. I want to recover all record and data. Pls, Help me how to recover these record.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.