原作者: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!