0

I know how to open a secondary database from a master database, but how can I switch "focus" to the secondary database and scan through the table names to see if a table exists in that database, if it does delete it?

This is how I would open the database but this seems to only check the database I am running the code in, not the secondary database that is opened.

Set qd = CurrentDb.QueryDefs("exec_Dead")
Set ws = DBEngine(0)
Set db = ws.OpenDatabase("C:\Test\Testing.mdb")
On Error Resumse Next
DoCmd.DeleteObject acTable = acDefault, "Late Payments For Today"

2 Answers 2

3

You need to create an Access.Application variable so that you can interact with that instance of Access.

Dim db as DAO.Database
Dim tbl as DAO.TableDef
Dim accDB As Access.Application

Set accDB = New Access.Application
accDB.OpenCurrentDatabase "C:\Test\Testing.mdb", false

For each tbl in accDB.CurrentDb.TableDefs
    If tbl.Name = "The name of the table you want to delete" then
        accDB.DoCmd.DeleteObject acTable, tbl.Name
    End If
Next

accDB.CloseCurrentDatabase
set accDB = nothing
Sign up to request clarification or add additional context in comments.

Comments

3

DoCmd.DeleteObject is a method of Application, so you would need an Application instance for Testing.mdb in order to use DeleteObject.

You can do that, as @Zaider demonstrated. However you could just execute a DROP TABLE statement from db in your existing code:

Set ws = DBEngine(0)
Set db = ws.OpenDatabase("C:\Test\Testing.mdb")
On Error Resumse Next
'DoCmd.DeleteObject acTable = acDefault, "Late Payments For Today"
db.Execute "DROP TABLE [Late Payments For Today];"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.