我在Access DB ('10)中设置了查询,我的用户在Excel工具('10)中输入参数,该工具使用VBA将查询结果拉到Excel工具中。
问题:有时(我猜是1/50次)会提取不完整的数据,我不明白为什么。
是矛盾让我困惑。我最好的猜测是,是什么原因导致了代码的最后一块激活并提前关闭了连接。但我不知道它为什么会偶尔这么做。
The Code:我稍微更新了代码,以包含一些错误捕获。这个问题一直存在。
Sub ParameterQuery_Awesome()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
On Error GoTo Whoa
'Step 1: Declare your variables
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase _
("\\folders\Awesome.accdb")
Set MyQueryDef = MyDatabase.QueryDefs("AwesomeQuery")
'Step 3: Define the Parameters
With MyQueryDef
.Parameters("[Enter Parameter:]") = [Parameter].Value
End With
'Step 4: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset
'Step 5: Clear previous contents
[DataInputRange].ClearContents
'Step 6: Copy the recordset to Excel
[DataInput].CopyFromRecordset MyRecordset
'MsgBox "Your Query has been Run"
LetsContinue:
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
On Error Resume Next
MyRecordset.Close
MyQueryDef.Close
MyDatabase.Close
Set MyRecordset = Nothing
Set MyQueryDef = Nothing
Set MyDatabase = Nothing
On Error GoTo 0
Exit Sub
Whoa:
MsgBox "Oh noes!!1" & vbCrLf & _
"Error Description :" & Err.Description & vbCrLf & _
"Error at line :" & Erl & vbCrLf & _
"Error Number :" & Err.Number
Resume LetsContinue
End Sub发布于 2015-06-24 17:51:08
我是个巨大的白痴。
当工具出错时,我让一个用户发送给我一份该工具的副本,试图深入研究这个问题,并意识到虽然不是所有的数据都在填充,但在稍后的过程中会发生这种情况。实际上,所有的原始数据都被放入了工具中。我很抱歉--
https://stackoverflow.com/questions/31004913
复制相似问题