获取工作代码以查看访问数据库是否打开

时间:2019-07-08 16:53:35

标签: excel vba ms-access

我通过单击和VBA从Excel打开Access数据库,我想检查Excel文件中的下一次单击是否打开了Access,以避免打开另一个访问文件。

我的代码:

Dim oApp As Object
    ' Path to Access database
    Dim LPath As String
    LPath = "C:\Users\kyle.overton1\Desktop\Access Database for Punch List.accdb"

    ' Exit if selection is more than one cell
    If Target.Cells.Count > 1 Then
        Exit Sub
    End If

    ' Validate if selected cell is in range
    If Not Application.Intersect(Range("A23:A2000"), Range(Target.Address)) Is Nothing Then
        ' Assign studentid value from current cell
        Init = Target.Value

        ' Call the open procedure with current cell value
        Call OpenAccess(Init)
    End If
End Sub
Sub OpenAccess(Init)
    Dim oApp As Object

    'Path to Access database
    Dim LPath As String
    LPath = "C:\Users\kyle.overton1\Desktop\Access Database for Punch List.accdb"

    'Open Access and make visible
    Set oApp = CreateObject("Access.Application")
    oApp.Visible = True

    'Open Access database as defined by LPath variable
    oApp.OpenCurrentDatabase LPath
    oApp.DoCmd.OpenForm "frm_AllRecords"
    oApp.DoCmd.ApplyFilter , "Initiative_Nbr=" & Init
End Sub

此代码可以打开并过滤到正确的文件

我的预期结果是检查数据库是否打开,是否关闭,否则继续

0 个答案:

没有答案