【译文】代码来帮助你调试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源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access两种方式实现即时更...(03.01)
- Access隐藏与显示lacc...(01.12)
- 【Access高效办公】将每个...(12.23)
- Access21点游戏源代码(12.13)
- 【Access窗体导出Exce...(11.15)
- 【Access开发】Acces...(11.14)
- 通过Access宏录入数据到选...(11.10)
- 用DLOOKUP函数将需求表中...(10.31)
- Access日期区间段查询数据...(10.25)
学习心得
最新文章
- Access快速开发平台--在WI...(03.08)
- 使用SQL语句删除xscj表中学号...(03.08)
- Access快速开发平台进销存教程...(03.07)
- Access快速开发平台--frm...(03.06)
- 【Access删除查询】删除数字最...(03.06)
- Access快速开发平台进销存教程...(03.05)
- Access快速开发平台进销存教程...(03.04)
- Access快速开发平台--IsL...(03.02)
- Access两种方式实现即时更新月...(03.01)
- Access开发的资金管理系统;基...(02.29)