【问题标题】:Check for valid connection between frontend and backend Access database检查前端和后端 Access 数据库之间的有效连接
【发布时间】:2021-05-11 06:22:49
【问题描述】:

我有一个 Access 数据库应用程序,它已分为前端和后端。后端位于所有用户都可以访问的共享网络驱动器上。我的问题是,当用户启动此应用程序的前端时,由于共享驱动器可能尚未在本地安装,因此他们没有与后端的连接,启动应用程序时显示的初始表单不会t 打开并让用户质疑发生了什么。我已经有代码来检查后端是否已连接,尽管由于某种原因,当它未连接时,介绍屏幕表单永远不会显示并且访问应用程序就在那里

Private Sub Form_Open(Cancel As Integer)
    Dim strBackEndPath, LResult As String
    Dim i, j, lenPath As Integer

    'initialize variable status to 0
    Me.BEDB_Status = 0
    'define what to check in backend database
    strBackEndPath = CurrentDb.TableDefs("VersionInfo-Available").Connect

    ' Now remove the datebase  & password prefix
    j = InStrRev(strBackEndPath, "=") + 1
    strBackEndPath = Mid(strBackEndPath, j)

    'Checking access to Backend database files...
    Me.MessageText = "Checking access to Backend database files..."
    On Error Resume Next
    LResult = Dir(strBackEndPath)
    'Set status to Length of LResult
    Me.BEDB_Status = Len(LResult)

    'Check length of BEDB_Status, if greater than 0, backend is connected. If 0, backend is not connected.
    If Me.BEDB_Status > 0 Then
        'length is greater than 0 so continue opening the app
        DoCmd.OpenForm "IntroScreen"
    Else
        'length is 0 so backend is not connected. Alert user and quit the app
        Me.MessageText = "The database isn't currently accessible. Program will now exit. Please ask the support team for assistance"
        DoCmd.Quit acQuitSaveNone
    End If

End Sub

【问题讨论】:

    标签: vba ms-access backend


    【解决方案1】:

    尝试打开其中一个链接表并忽略错误可能更简单、更快:

    Public Function IsLinkedTable(ByVal TableName As String) As Boolean
    
        Dim LinkOk  As Boolean
    
        On Error Resume Next
        LinkOk = (DCount("*", TableName) >= 0)
    
        IsLinkedTable = LinkOk
    
    End Function
    

    请务必使用表单的 OnLoad 事件,因为这样可以打开表单。

    【讨论】:

    • 完美!除了您提供的功能和使用 OnLoad 而不是 OnOpen 事件之外,它运行良好。谢谢。
    【解决方案2】:

    实施了 Gustav 的建议,即使用 On Load 事件而不是 On Open 事件以及他的 IsLinkedTable 函数,并且效果很好。

    感谢古斯塔夫。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多