【发布时间】:2014-05-05 17:39:07
【问题描述】:
我在 excel 中有一个表,它通过与 Access 数据库的连接来填充。我正在尝试编写一个允许您从该表中删除单个行的宏。我使用 ADODB 连接将删除命令传递给 Access,效果很好。但是,在宏结束时,我想在 excel 中刷新表格,以便在宏完成后,删除的记录将不再出现在该表中。
我已经在填充表的连接上设置了 backgroundquery = False,但在这种情况下似乎没有什么不同。我已使用 application.wait 命令作为解决方法,但我正在寻找更好的解决方案。在我的删除查询运行之前,还有其他方法可以推迟表刷新吗?
我的代码如下:
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
感谢您的帮助
【问题讨论】: