简体   繁体   English

如何在excel VBA中而不是CurrentDB中定义文件夹中访问数据库的路径?

[英]How to define a path of access database in a folder instead of CurrentDB in excel VBA?

I have to run an access query via excel vba user form.我必须通过 excel vba 用户表单运行访问查询。 This query basically find the record from the access table and updates the Out Time.此查询基本上从访问表中查找记录并更新 Out Time。 The Table name is "Visitors" and the query name is "updateVisitors".表名是“Visitors”,查询名是“updateVisitors”。 Below is the Excel VBA code I have written :-以下是我编写的 Excel VBA 代码:-

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 ".我发现当数据库打开时它不起作用,因为“ Set dbs = CurrentDb ”。 Is there any way that I can run this query even when the DB is closed?即使数据库关闭,有什么方法可以运行此查询吗?

From here https://sourcedaddy.com/ms-access/opening-external-database.html从这里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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM