Create a query dynamically through code in MSAccess 2003 [VBA]

こ雲淡風輕ζ 提交于 2019-12-18 05:39:02

问题


Hi I need to create a query in MSAccess 2003 through code (a.k.a. VB) -- how can I accomplish this?


回答1:


A vague answer for a vague question :)

strSQL="SELECT * FROM tblT WHERE ID =" & Forms!Form1!txtID 

Set qdf=CurrentDB.CreateQueryDef("NewQuery",strSQL)
DoCmd.OpenQuery qdf.Name



回答2:


Thanks for this answer and the small piece of code. If somebody needs to define the datatypes for the variables used, use this:

    Dim strsql As Variant
    Dim qdf As QueryDef



回答3:


Dim strSql As String 'as already in example
Dim qdf As QueryDef 'as already in example

strSql = "SELECT * FROM tblT WHERE ID =" & Forms!Form1!txtID 'as already in example

On Error Resume Next
'Delete the query if it already exists
DoCmd.DeleteObject acQuery, "NewQuery"

Set qdf = CurrentDb.CreateQueryDef("NewQuery", strSql) 'as already in example
DoCmd.OpenQuery qdf.Name 'as already in example

'release memory
qdf.Close 'i changed qdef to qdf here and below
Set qdf = Nothing


来源:https://stackoverflow.com/questions/390420/create-a-query-dynamically-through-code-in-msaccess-2003-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!