首页 > 解决方案 > 如何设置引用库数据库的变量

问题描述

目前,我正在为我的所有 Access 应用程序开发一个库数据库,其中包含通用代码和表单。它进展顺利,但我唯一无法发现的是如何设置引用引用的变量。库名称由一个常量设置,并在启动时检查该文件是否存在于应用文件夹中,以及该文件是否等于存储在引用中的路径。

但是,我想用一个短变量“dbl”来引用库,因为我不想每次都使用库名。这个名字也可能会改变。如何设置变量来执行此操作?我试过 Set dbl = Application.References(APP_LIB)了,这被认为是有效的,但我不能用这个变量调用任何例程或表单。

谢谢!

艺术。

Public Function CkLib()
'Needs to stay in the App Module!
    Dim ref As Reference
    Dim strLib As String

    On Error GoTo Err_Proc

    For Each ref In Application.References
        If ref.Name = APP_LIB Then
            strLib = CurrentProject.Path & "\" & ref.Name & "." & APP_LIB_TYPE
            If Dir(strLib, vbNormal) = "" Then  'File does not exist
                MsgBox "The app library " & APP_LIB & " is missing. Please reinstall the app or ask for support!", vbCritical + vbOKOnly, APP_NAME & " App Error"
                DoCmd.Quit acQuitSaveNone
            Else
                If ref.FullPath <> strLib Then 'Path needs to be updated
                    References.Remove ref
                    References.AddFromFile strLib
                End If
                Exit For
            End If
        End If
    Next ref

    If Nz(strLib, "") = "" Then
        'Try to add ref
        If Dir(CurrentProject.Path & "\" & APP_LIB & "." & APP_LIB_TYPE, vbNormal) <> "" Then
            References.AddFromFile strLib
        Else
            MsgBox "Missing reference to app library " & APP_LIB & "! Please reinstall the app or ask for support!", vbCritical + vbOKOnly, APP_NAME & " App Error"
            DoCmd.Quit acQuitSaveNone
        End If
    End If

    Set dbl = Application.References(APP_LIB)

Exit_Proc:
    Exit Function

Err_Proc:
    Select Case Err.number
        Case Else
            MsgBox "Error: " & Trim(Str(Err.number)) & vbCrLf & _
            "Desc: " & Err.description & vbCrLf & vbCrLf & _
            "Module: Mod_Generic" & vbCrLf & _
            "Function: CkLib", _
            vbCritical, "Error!"
    End Select
    Resume Exit_Proc
End Function

标签: vbams-access

解决方案


推荐阅读