Access运行时错误2501:OutputTo 操作被取消的解决方法-张亚帮
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


Access运行时错误2501:OutputTo 操作被取消的解决方法

发表时间:2012/11/17 15:38:49 评论(0) 浏览(17602)  评论 | 加入收藏 | 复制
   
摘 要:Access运行时错误2501:OutputTo 操作被取消的解决方法
正 文:

    在窗体的一个按钮里写了导出EXCEL报表的事件,是先通过几个组合框内容进行筛选后的结果,进行导出。代码如下:

 
Private Sub cmdImport_Click()
Dim strWhere
Dim strSql
strWhere = ""
strSql = Null
'判断状态是否为空
If Not IsNull(Me.state) Then
'有输入
strWhere = strWhere & "([state] like '" & Me.state & "')"
End If

'判断省份是否为空
If Not IsNull(Me.province) Then

'有输入
If (strWhere <> "") Then
strWhere = strWhere & " AND ([province] like '" & Me.province & "')"
Else
strWhere = strWhere & " ([province] like '" & Me.province & "')"
End If
End If

'判断城市是否为空
If Not IsNull(Me.city) Then
'有输入
If (strWhere <> "") Then
strWhere = strWhere & "AND ([city] like '" & Me.city & "')"
Else
strWhere = strWhere & "([city] like '" & Me.city & "')"
End If
End If

'判断姓名是否为空
If Not IsNull(Me.seaname) Then
'有输入
If (strWhere <> "") Then
strWhere = strWhere & "AND ([name] like '*" & Me.seaname & "*')"
Else
strWhere = strWhere & "([name] like '*" & Me.seaname & "*')"
End If
End If
'先在窗口显示一下strWhere的值,代码调试完后可以取消下一句
'Debug.Print strWhere
'让子窗体应用窗体查询
If (strWhere <> "") Then
strSql = "select * from q_trainlist where " & strWhere
Else
strSql = "select * from q_trainlist"
End If
Me.w_traindata.Form.Filter = strWhere

Set qdf = CurrentDb.QueryDefs("q_ea_candidate")
    qdf.SQL = strSql
    qdf.close
Set qdf = Nothing
DoCmd.OutputTo acOutputQuery, "q_ea_candidate", acFormatXLS, , True 
End Sub


问题描述:
在点击按钮后,弹出的保存框中,如果点确定,就顺利导出保存了。如果点取消,则提示  运行时错误2501:outputto操作被取消    这个调试错误,什么原因呢?

 

解决错误的方法:
On Error GoTo Err_按钮名称_Click
 
Exit_按钮名称_Click:
    Exit Sub

Err_按钮名称_Click:
    MsgBox err.Description
Resume Exit_按钮名称_Click

' 注:利用VB及vba的On Error要注意EXIT退出,否则VBA代码会继续向下执行。


Access软件网交流QQ群(群号:198465573)
 
 相关文章
Access运行时错误'2108':必须在执行GoToContro...  【麥田  2012/10/27】
Access运行时错误'2450':【试用版1.0】您开发的软件名...  【麥田  2012/11/18】
常见问答
技术分类
相关资源
文章搜索
关于作者

张亚帮

文章分类

文章存档

友情链接