Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > 综合其它

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

时 间:2012-04-22 10:50:58
作 者:周芳   ID:24526  城市:上海
摘 要:用代码创建查询。
正 文:

原作者: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群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助