0

I have to run an access query via excel vba user form. This query basically find the record from the access table and updates the Out Time. The Table name is "Visitors" and the query name is "updateVisitors". Below is the Excel VBA code I have written :-

Sub VisitorsUpdateOT()

Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim con As Object

    Application.EnableCancelKey = xlDisabled
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Set dbs = CurrentDb
    Set qdf = dbs.QueryDefs("updateVisitors")

        qdf.Parameters("Recp_Visitors!OutTime").Value = Recp_Visitors.OutTime.Value
        qdf.Parameters("Recp_Visitors!SrNo").Value = Recp_Visitors.SrNo.Value
        qdf.Execute
        qdf.Close

    Set qdf = Nothing
    Set dbs = Nothing
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

This code runs perfectly when the access database is open. I figured out that it does not works when the DB is open because of "Set dbs = CurrentDb". Is there any way that I can run this query even when the DB is closed?

2
  • You have to have an open connection to get data, but it doesn't have to be currentDb. To run it from Excel you will need to get that connection a different way. Commented Nov 11, 2021 at 10:40
  • stackoverflow.com/a/60326389/8422953 Commented Nov 11, 2021 at 10:43

1 Answer 1

1

From here https://sourcedaddy.com/ms-access/opening-external-database.html

Change

Set dbs = CurrentDb

to

Set dbs = DBEngine(0).OpenDatabase("C:\Temp\db1.accdb", False, False)
Sign up to request clarification or add additional context in comments.

6 Comments

Excel does not know what DBEngine is.
It throws error saying MS Access DB cannot open or write to the file "DB Path". It is already opened exclusively by another user.
Have you got the MS Access file open? If you've been messing with VBA, it's possible it's still open in an orphan process.
Hi Nick, you are right. I changed the code as below Set dbs = DBEngine(0).OpenDatabase("DB Path", , False, True) Thanks a ton everyone... :)
feel free to accept as the correct anser
|

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.