Solved

Delete Multiple Tables from VBA in Ms Access

Posted on 2008-06-15
7
957 Views
Last Modified: 2013-12-25
Hi,

My access DB has a number of functions that import data from external sources such as text files. The import operations work well; however, sometimes, ImportError tables are generated. I need a way to delete mutiple ImportError tables within the function.

Below is the code that performs the import operations. As you can see it cycles through top determine if there is more than one file called prof.txt. Generally, they are named, prof.txt, prof1.txt, prof2.txt and so on.

The following error tables have been created most recently.

prof1_ImportErrors
prof2_ImportErrors
prof3_ImportErrors
prof4_ImportErrors
prof5_ImportErrors

Thanks
Kev

Function Import_PMKEYS_Proficiencies()
On Error GoTo ImportError_Err
'Imports Proficiencies data from PMKeyS Output Text File
    Dim strFile As String
'Calculate PMKeyS data file path
    varPMKeySFilePath = DLookup("ImportPMKeySPath", "systblSettings", "")
    DoCmd.SetWarnings False
 
'Delete current data set
    DoCmd.RunSQL "DELETE tblpmkProficiencies.* FROM tblpmkProficiencies;"
'Import new data set
    strFile = Dir(varPMKeySFilePath & "prof*.txt", vbNormal)
    Do While strFile <> ""
        If strFile <> "." And strFile <> ".." Then
        DoCmd.TransferText acImportDelim, "ImportSpec_PMKeyS_Proficiencies", "tblpmkProficiencies", varPMKeySFilePath & strFile, False, ""
        End If
        strFile = Dir()
    Loop
 
'Delete ImportError tables if created
    DeleteTable ("Prof_ImportErrors")
 
'Clean up unwanted records
    DoCmd.RunSQL "DELETE tblpmkProficiencies From tblpmkProficiencies WHERE (((tblpmkProficiencies.[Unit Id]) Is Null));"
 
'Update PMKeyS Update Info
    With Form_frmMenuMain
        .ProfBy.Value = .varCurrentUser
        .ProfUpdate.Value = Now()
    End With
 
    MsgBox "Data Transfer Complete"
    
ImportError_Exit:
    DoCmd.SetWarnings True
    Exit Function
 
ImportError_Err:
    MsgBox Error$
    Resume ImportError_Exit
End Function

Open in new window

0
Comment
Question by:budorat
  • 4
  • 3
7 Comments
 
LVL 46

Accepted Solution

by:
tbsgadi earned 500 total points
ID: 21787978
Hi budorat,

Use something like:

Good Luck!

Gary
Dim obj As AccessObject, dbs As Object
    Set dbs = Application.CurrentProject 
    For Each obj In dbs.AllTables
  if obj.Name like "*ImportErrors" then  DoCmd.DeleteObject acTable, obj.Name
Next obj

Open in new window

0
 
LVL 5

Author Comment

by:budorat
ID: 21787985
Hi,

Firstly, wow, thanks for a super fast response.

I get an error, "RunTime Error 438" "Object doesnt support this property or method"

I have a number of functions similar to the one above. As a result, I have used your code as a separate funciton that I can call. I am running the function directly rather than calling it.

Kev
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 21788040
budorat,

I'm glad to help.
Is it working?

Gary
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 5

Author Comment

by:budorat
ID: 21788050
Gary, nope, not working.

I  get an error, "RunTime Error 438" "Object doesnt support this property or method". The Debug procedure highlights line "For Each obj In dbs.AllTables"

Kev
0
 
LVL 46

Assisted Solution

by:tbsgadi
tbsgadi earned 500 total points
ID: 21788060
budorat,

Try  Set dbs = Application.CurrentData

Gary
0
 
LVL 5

Author Closing Comment

by:budorat
ID: 31467314
Gary,

Perfect. Thanks very much for your help.

Kev
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 21788222
Great!
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question