我在Access DB('10)中进行了查询设置。我的用户将参数输入到Excel工具('10)中,该工具使用VBA将查询结果提取到Excel工具中。
问题:有时候(我猜的是1/50)它会提取不完整的数据,我不明白为什么。
这让我感到困惑。我最好的猜测是某些东西导致最后一段代码激活并提前关闭连接。但我不确定为什么它偶尔会这样做。
守则: 我稍微更新了代码以包含一些错误捕获。这个问题一直存在。
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
答案 0 :(得分:0)
我是一个巨大的白痴。
我有一个用户在错误时向我发送了该工具的副本,试图深入研究该问题,并意识到虽然并非所有数据都在填充,但这一过程发生在后期。事实上,所有原始数据都已成为工具。道歉-_-