在刷新其他连接之前,ADODB等待查询完成

我有一个Excel中的表,通过连接到Access数据库填充。 我正在尝试编写一个macros,允许您从此表中删除单个行。 我使用ADODB连接将删除命令传递给Access,这工作正常。 但是,在macros的末尾,我想要在Excel中刷新表,以便在macros完成后,删除的logging将不再出现在该表中。

我已经在填充表的连接上设置了backgroundquery = False,但是在这种情况下似乎没有什么区别。 我已经使用application.wait命令作为解决方法,但我正在寻找一个更好的解决scheme。 是否有另一种方法来阻止表刷新,直到我的删除查询已经运行?

我的代码如下:

Sub Delete_recipe() 'Set up query Dim Recipe_ID As Integer Worksheets("Recipe Adder").Calculate Recipe_ID = Range("New_recipe_ID").Value 'Open data connection Dim Cn As ADODB.Connection Set Cn = New ADODB.Connection Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tucker\Desktop\Recipe project\MURPH.xls;Extended Properties=Excel 8.0;" _ & "Persist Security Info=False" 'Execute delete query Cn.Execute "Delete from Recipe_master IN 'C:\Users\Tucker\Desktop\Recipe project\MURPH.accdb' where Recipe_ID =" & Recipe_ID Cn.Execute "Delete from Recipe_ingredients IN 'C:\Users\Tucker\Desktop\Recipe project\MURPH.accdb' where Recipe_ID =" & Recipe_ID Cn.Execute "Delete from Recipe_instructions IN 'C:\Users\Tucker\Desktop\Recipe project\MURPH.accdb' where Recipe_ID =" & Recipe_ID 'Close connection Cn.Close Set Cn = Nothing 'Application.Wait (Now + TimeValue("0:00:06")) ActiveWorkbook.Connections("Murph Ingredient").Refresh 'Clear data from cells Range("New_recipe_name_merged").ClearContents Range("Recipe_description").ClearContents Range("New_recipe_ingredients").ClearContents Range("New_recipe_instructions").ClearContents End Sub 

谢谢你的帮助

您需要asynchronous执行并等待它完成。 请注意,如果您等待每个命令执行,直到触发下一个命令为止,则会降低速度。

 cn.Execute sqlString, , adAsyncExecute Do While cn.state = adStateOpen + adStateExecuting ' wait for command to finish ' perhaps show a status here Loop ActiveWorkbook.Connections("Murph Ingredient").Refresh 

我们必须根据州财产文件检查州的组合

您可以随时使用State属性来确定给定对象的当前状态。 该对象的状态属性可以有一个值的组合。 例如,如果语句正在执行,则此属性将具有adStateOpen和adStateExecuting的组合值。

进一步阅读:

  • 执行方法
  • 选项状态枚举