如果Access与MySQL数据库服务器断开连接,如何刷新链接表?

时间:2015-06-05 06:30:07

标签: mysql vba ms-access odbc

所以,我需要一种方法刷新Access数据库中的链接表,这样如果因某种原因Internet断开连接,ODBC在发送查询时不会出错,只需刷新以查看查询可以再次发送。但是,当Internet重新启动时,Access数据库由于某种原因不会重新连接。如果发生这种情况,是否有办法在VBA中刷新链接表?

1 个答案:

答案 0 :(得分:1)

.RefreshLink方法会做你想做的吗?

这里有一个例子:https://msdn.microsoft.com/en-us/library/office/ff198349.aspx

另一个解决方案是重新连接到调用此函数的远程数据库。

Function ConnectODBC(ByVal strDsn As String, ByVal strDatabase As String, ByVal strUserName As String, ByVal strPassword As String)

    Dim qdf As DAO.QueryDef
    Dim rst As DAO.Recordset
    Dim strConnection As String

    strConnection = "ODBC;DSN=" & strDsn & ";" & _
                    "DATABASE=" & strDatabase & ";" & _
                    "UID=" & strUserName & ";" & _
                    "PWD=" & strPassword

    Set qdf = CurrentDb.CreateQueryDef("")

    With qdf
        .Connect = strConnection
        .SQL = "SELECT 1;"
    End With

    Set rst = qdf.OpenRecordset(dbOpenSnapshot, dbSQLPassThrough)
    ConnectODBC = True

    Set rst = Nothing
    Set qdf = Nothing

End Function