Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 971
  • Last Modified:

Delete Multiple Tables from VBA in Ms Access

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
Kev
Asked:
Kev
  • 4
  • 3
2 Solutions
 
tbsgadiCommented:
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
 
KevAuthor Commented:
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
 
tbsgadiCommented:
budorat,

I'm glad to help.
Is it working?

Gary
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
KevAuthor Commented:
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
 
tbsgadiCommented:
budorat,

Try  Set dbs = Application.CurrentData

Gary
0
 
KevAuthor Commented:
Gary,

Perfect. Thanks very much for your help.

Kev
0
 
tbsgadiCommented:
Great!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now