【译文】代码来帮助你调试SQL代码—POPQuery-周芳
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> 综合其它


【译文】代码来帮助你调试SQL代码—POPQuery

发表时间:2012/4/22 10:50:58 评论(0) 浏览(6629)  评论 | 加入收藏 | 复制
   
摘 要:用代码创建查询。
正 文:

原作者:Juan Soto 翻译:周芳

【译文】代码来帮助你调试SQL代码—POPQuery
      我在我的代码中经常使用SQL,我不喜欢创建查询,因为它可能会被用户在使用中删除或者改变,所以我就在代码中使用查询。
      我经常建立SQL字符串,然后我需要在Access的查询里去调试他们,过去我是在立即窗口上得到我的SQL字符串的值然后粘贴在一个新的查询窗口。我已经厌倦了这样做,所以我创建了一个小功能来为我做这件事:

Public Function PopQuerySQL(strSQL As String, strQueryName As String)
    Dim qdf As DAO.QueryDef
    On Error GoTo PopQuerySQL_Error
    '关闭查询
    DoCmd.Close acQuery, strQueryName
    ‘删除查询
    DoCmd.DeleteObject acQuery, strQueryName
    ’创建查询
    Set qdf = CurrentDb.CreateQueryDef(strQueryName, strSQL)
    Set qdf = Nothing
    ‘打开查询
    DoCmd.OpenQuery strQueryName, acViewNormal
    On Error GoTo 0
Exit Function
    PopQuerySQL_Error:
    If Err.Number = 7874 Then
       Resume Next 'Query does not exist
    End If
    MsgBox "Error" & Err.Number & "(" & Err.Description & ") in procedure PopQuerySQL of Module mdlAPI"
End Function


我现在可以在立即窗口使用我的SQL代码:
PopQuery (strSQL,"qryTemp")
它节省了我很多时间,我希望它也同样对你有好处!

【原文】
Code to help you debug SQL code – POPQuery


I use SQL in my code a lot, I’m not a fan of creating queries and then referencing them in my code since users may delete or change them.
Often I build SQL strings and then I need to debug them in the query Access grid, in the past I would get the value of my SQL string in the immediate window

and paste the SQL in a new query window. I got tired of doing it all the time so I created a small function that will do it for me:

Public Function PopQuerySQL(strSQL As String, strQueryName As String)
Dim qdf As DAO.QueryDef
On Error GoTo PopQuerySQL_Error
DoCmd.Close acQuery, strQueryName
DoCmd.DeleteObject acQuery, strQueryName
Set qdf = CurrentDb.CreateQueryDef(strQueryName, strSQL)
Set qdf = Nothing
DoCmd.OpenQuery strQueryName, acViewNormal
On Error GoTo 0
Exit Function
PopQuerySQL_Error:
If Err.Number = 7874 Then
Resume Next 'Query does not exist
End If
MsgBox "Error" & Err.Number & "(" & Err.Description & ") in procedure PopQuerySQL of Module mdlAPI"
End Function

I now use the code with my SQL variable in the immediate window:
PopQuery (strSQL,"qryTemp")
It saves me a lot of time and I hope it does the same for you!

            

Access软件网交流QQ群(群号:198465573)
 
 相关文章
调试简介  【leolong  2009/4/1】
关于调试VBA代码时三种类型错误的说明  【微软  2011/8/23】
VBA代码调试方法  【风行  2012/1/21】
视频:access课程第九章6_程序调试与错误处理  【whtancky  2012/3/1】
视频:access课程第八章3_宏的运行与调试  【whtancky  2012/3/2】
监视窗口工具使用方法-VBA代码调试  【风行  2012/5/26】
常见问答
技术分类
相关资源
文章搜索
关于作者

周芳

文章分类

文章存档

友情链接