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

Delete all Tables

I am trying to import tables using an odbc connection on a periodic basis. I don't want to create a second copy of the tables, so I have to delete them all before doing the import. I found another question similar to this and tried the following code, but it doesn't work.

Private Sub btnPBGUpdate_Click()
Dim TblName As String
Dim obj As AccessObject, dbs As Object

Set dbs = Application.CurrentData

' Search for closed Tables.
For Each obj In dbs.AllTables
TblName = obj.Name
If obj.IsLoaded = False Then
' Delete the tables.
If Not (Left(TblName, 4)) = "MSys" Then
Access.DoCmd.DeleteObject acTable, obj.Name
Else

End If
Next obj
End Sub

I get a compile error Next without For. I am very new to this, so I really don't know what I am doing and would appreciate any help. Thanks.
Jan 29 '08 #1
3 16436
jyoung2
32
You mised the closeing "end if" for the "If obj.IsLoaded = False Then" see the bolded if

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnPBGUpdate_Click()
  2.     Dim TblName As String
  3.     Dim obj As AccessObject, dbs As Object
  4.  
  5.     Set dbs = Application.CurrentData
  6.  
  7.      '  Search for closed Tables.
  8.     For Each obj In dbs.AllTables
  9.         TblName = obj.Name
  10.         If obj.IsLoaded = False Then
  11.              ' Delete the tables.
  12.             If Not (Left(TblName, 4)) = "MSys" Then
  13.                 Access.DoCmd.DeleteObject acTable, obj.Name
  14.             Else
  15.  
  16.             End If
  17.         End if
  18.     Next obj
  19.     End Sub
I am trying to import tables using an odbc connection on a periodic basis. I don't want to create a second copy of the tables, so I have to delete them all before doing the import. I found another question similar to this and tried the following code, but it doesn't work.

Private Sub btnPBGUpdate_Click()
Dim TblName As String
Dim obj As AccessObject, dbs As Object

Set dbs = Application.CurrentData

' Search for closed Tables.
For Each obj In dbs.AllTables
TblName = obj.Name
If obj.IsLoaded = False Then
' Delete the tables.
If Not (Left(TblName, 4)) = "MSys" Then
Access.DoCmd.DeleteObject acTable, obj.Name
Else

End If
Next obj
End Sub

I get a compile error Next without For. I am very new to this, so I really don't know what I am doing and would appreciate any help. Thanks.
Jan 29 '08 #2
Thanks. Now I get a message that says some of the tables can't be deleted because they are part of a relationship. Is there a way to programatically remove all relationships before running this script?
Jan 30 '08 #3
JustJim
407 Expert 256MB
Thanks. Now I get a message that says some of the tables can't be deleted because they are part of a relationship. Is there a way to programatically remove all relationships before running this script?
You would only have to re-enter the relationships. A better method might be to "Empty" the current tables, then append the incoming data to the same (but now empty) table. This keeps all references to the original table, eg relationships, queries, etc intact.

"Emptying" a table is as simple as opening an editable recordset, deleting a record, moving to the next record and continuing until the end of the recordset.

Air Code Warning: just typing off the top of my head here, following is not tested.
Expand|Select|Wrap|Line Numbers
  1. Private Sub SomeButton_Click()
  2.  
  3. Dim dbMyDatabase as Database
  4. Dim rsMyRecordset as Recordset
  5.  
  6. Set dbMyDatabase = CurrentDB()
  7. Set rsMyRecordset = dbMyDatabase.OpenRecordset("TableToEmpty", dbOpenDynaset)
  8.  
  9. Do Until rsMyRecordset.EOF
  10.      With rsMyRecordset
  11.           .Delete
  12.           .MoveNext
  13.      End With
  14. Loop
  15.  
  16. rsMyRecordset.Close
  17. set rsMyRecordset = Nothing
  18. Set dbMyDatabase = Nothing
  19. Exit Sub
Jim
Jan 30 '08 #4

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

Similar topics

2
by: michael | last post by:
Gotta post because this is driving me nuts. Trying to DELETE orphans. I can successfully: SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE UsersID IS NULL; but when I...
5
by: Mike | last post by:
I am not a sql person and could use some help with a delete...here is what I want: I have the following tables/fields (only including necessary fields) answers result_id results result_id
2
by: Bob Ganger | last post by:
Hello, I am working on a project using SQL Server 2000 with a database containing about 10 related tables with a lot of columns containing text. The total current size of the database is about...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
6
by: Paul T. Rong | last post by:
Dear all, Here is my problem: There is a table "products" in my access database, since some of the products are out of date and stopped manufacture, I would like to delete those PRODUCTS from...
2
by: NoSpam | last post by:
Hi, I am working with C# and ASP.NET with code behind and a SQL Server. I'm making an e-shop. When clients see what they have in their basket, I added a function DELETE to delete a line. It took...
2
by: R.Welz | last post by:
Hello. I want to discuss a problem I have with my database design becourse I feel I cannot decide wheather I am on the right way of doing things. First of all, I am writing a literature and...
2
by: nekiv90 | last post by:
Greetings, I have to delete older policies and its related records in other tables. The deletion from the parent table will trigger the deletion of relevant records from about 30 something...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
1
by: jpr | last post by:
Hello, My database has 5 tables. WHen I add data to one table, it runs an append query that copies three records to other 4 tables. The main table is MASTER. The data I copy are: ID, SSN and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.