忽略OBDC查询上的连接错误

时间:2017-10-25 17:09:38

标签: ms-access

我有一个查询连接到多个远程计算机以将数据联合到一个表中。当所有机器都连接时它工作正常但偶尔可能关闭任何给定的机器(这会导致错误),我希望我的查询忽略任何不可用的连接并继续查询的其余部分。有没有办法做到这一点?

我正在使用带有OBDC conncetion的链接表(驱动程序:MySql ODBC 5.3 Ansi Driver)

这是我的问题:

SELECT "HX32" AS workcenter, "HX32." & [HX32].[dataid] AS tbldataid, HX32.dataid AS dataid, HX32.TS, DMin("[TS]","[HX32]","[TS] > #" & [TS] & "#") AS EndTS, DateDiff("s",[TS],EndTS) AS durationsec, Format(Int([durationsec]/86400)) & " " & Format([durationsec]/86400,"hh:nn:ss") AS duration, Format(TS,"mm/dd/yyyy") AS [Day], Switch(incycle=0,'Down',incycle=1,'Running') AS Status
FROM HX32
WHERE (((HX32.TS)>Date()-3) AND ((HX32.incycle)=0))

UNION ALL 

SELECT "VL65A" AS workcenter, "VL65A." & [VL65A].[dataid] AS tbldataid, VL65A.dataid AS dataid, VL65A.TS, DMin("[TS]","[VL65A]","[TS] > #" & [TS] & "#") AS EndTS, DateDiff("s",[TS],EndTS) AS durationsec, Format(Int([durationsec]/86400)) & " " & Format([durationsec]/86400,"hh:nn:ss") AS duration, Format(TS,"mm/dd/yyyy") AS [Day], Switch(incycle=0,'Down',incycle=1,'Running') AS Status
FROM VL65A
WHERE (((VL65A.TS)>Date()-3) AND ((VL65A.incycle)=0))

UNION ALL 

SELECT "VL68B" AS workcenter, "VL68B." & [VL68B].[dataid] AS tbldataid, VL68B.dataid AS dataid, VL68B.TS, DMin("[TS]","[VL68B]","[TS] > #" & [TS] & "#") AS EndTS, DateDiff("s",[TS],EndTS) AS durationsec, Format(Int([durationsec]/86400)) & " " & Format([durationsec]/86400,"hh:nn:ss") AS duration, Format(TS,"mm/dd/yyyy") AS [Day], Switch(incycle=0,'Down',incycle=1,'Running') AS Status
FROM VL68B
WHERE (((VL68B.TS)>Date()-3) AND ((VL68B.incycle)=0))
;

1 个答案:

答案 0 :(得分:1)

我最终使用VBA来解决@ Erik的评论:

循环遍历每个连接检查它,如果连接良好,它会修改查询并运行。

Dim cnn As ADODB.Connection
Dim canConnect As Boolean
Set cnn = New ADODB.Connection

Dim conctns As Variant
Dim conctn As Variant

conctns = Array("HX32", "VL65A", "VL68B")

For Each conctn In conctns

    On Error GoTo sub_error

    cnn.Open conctn
    If cnn.State = adStateOpen Then
        canConnect = True

        strSQL = "SELECT '" & conctn & "' AS workcenter, '" & conctn & ".' & [" & conctn & "].[dataid] AS tbldataid, " & conctn & ".dataid AS dataid, " & conctn & ".TS, DMin('[TS]','[" & conctn & "]','[TS] > #' & [TS] & '#') AS EndTS, DateDiff('s',[TS],EndTS) AS durationsec, Format(Int([durationsec]/86400)) & ' ' & Format([durationsec]/86400,'hh:nn:ss') AS duration, Format(TS,'mm/dd/yyyy') AS [Day], Switch(incycle=0,'Down',incycle=1,'Running') AS Status FROM " & conctn & " WHERE (((" & conctn & ".TS)>Date()-3) AND ((" & conctn & ".incycle)=0));"
        CurrentDb.QueryDefs("unionall").SQL = strSQL
        DoCmd.OpenQuery "appendall", acViewNormal, acEdit
        DoCmd.OpenQuery "splithours", acViewNormal, acEdit

        MsgBox conctn & " updated: " & canConnect
        cnn.Close

    End If

sub_error:
  MsgBox conctn & ": " & Error$
  Resume sub_error_exit
sub_error_exit:
Next conctn