2

I'm trying to run Access DB queries in Excel, but facing problem.

Dim dbs As DAO.Database 

Set dbs = CurrentDb
dbs.Execute "DELETE FROM tblMyTable WHERE Bad", dbFailOnError

here it's getting

run time error 424 object required

exactly on the 2ndline

set dbs = CurrentDb

I already added reference DAO 3.6 object library. what to keep in place of CurrentDB. My MsAccess DB is in local disk.

3
  • 1
    You need to do something like Set dbs = OpenDatabase("C:\db1.mdb") Commented May 2, 2017 at 14:55
  • Hi Xidgel,Thanks for the quick response. but what if my DB is already Opened? Commented May 2, 2017 at 14:58
  • I used this code before declaring dbs variable Set objAccess = CreateObject("Access.Application") objAccess.OpenCurrentDatabase "C:\Users\" & sUser & "\Downloads\ICO\" & DB & ".mdb" Commented May 2, 2017 at 15:04

1 Answer 1

3

CurrentDb isn't recognized by Excel, so it's being treated as an empty variant, since you haven't assigned it anything. Using Option Explicit will prevent this sort of problem in the future.

If your DB is already open, try something like: Set dbs = GetObject(,"Access.Application").CurrentDb

Option Explicit '### This goes at the VERY TOP of ALL MODULES
Sub foo()

    Dim dbs As DAO.Database 
    Dim appAccess as Object '# Access.Application
    Set appAccess = GetObject(,"Access.Application") '# Get a handle on Access 
    Set dbs = appAccess.CurrentDb
    dbs.Execute "DELETE FROM tblMyTable WHERE Bad", dbFailOnError

End Sub
Sign up to request clarification or add additional context in comments.

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.