运行单个命令时如何解决“不响应”问题

时间:2019-05-26 23:42:27

标签: excel vba

我正在从Excel中的VBA表单运行对数据库的ODBC调用。 VBA表单表明该进程正在运行,但是如果ODBC运行时间太长(有时达到90秒超时),我将收到“无响应”

由于ODBC调用是一行代码,所以我不能放入DoEvents或任何常用的屏幕更新解决方法。 我已经读过Why does VBA say "(Not Responding)" even when code is running fine?,这表明我需要第二个线程。我该怎么做?

Call ShowWait("Loading Case Procedures via ODBC", 3)
ShtCPP.AutoFilterMode = False
ShtDocs.AutoFilterMode = False
If ShtCPP.Cells(1, 1) = vbNullString Then ShtCPP.Range("A1:J1") = Array("Date", "Sess", "Thr", "Surgeon", "Case#", "MRN", "Patient", "Procedure", "CMBS", "DocId"): ShtCPP.Range("A1:J1").Font.Bold = True
ShtTL.Cells.Clear
ShtTL.Rows.Delete

vData = Array(Int(Now()), Int(Now()) + 7, vbNullString, "41")
If GetData(Case_Procedures, vData, True) Then
    ShtCPP.Activate
    Application.StatusBar = vbNullString
    Call adjustCPPODBC(ShtTL)
    Call loadCPPODBC(ShtTL)
Else
    MsgBox "Connection failed to collect the procedures." & vbCrLf & _
        "See status on Excel for more information." & vbCrLf & vbCrLf & _
        "You may need to load Case Procedures from exported report."
    If MsgBox("Do you want to mark the process as completed so it doesn't attempt to re-load from ODBC?", vbYesNo + vbDefaultButton2) = vbYes Then
        ShtTables.Cells(2, 78) = Now()
    End If
End If
ShtCPP.Activate
ShtCPP.AutoFilterMode = False
ShtDocs.AutoFilterMode = False
Call ShowWait(, , False)

ShowWait将文本写到表单上的框架中,包括doevents和类似的内容 GetData可以完成所有ODBC事务,包括将数据加载到工作表和关闭ODBC连接。如果有效,则返回True,否则返回False(通常是由于超时)

我想做的是创建一个线程,该线程定期更新框架,以使Excel不会陷入“无响应”状态,即使ODBC调用最终在90秒的超时内成功,它也可以做到这一点。

0 个答案:

没有答案