动态创建查询

Create a query dynamically

提问人:Andrew G. Johnson 提问时间:12/24/2008 最后编辑:braXAndrew G. Johnson 更新时间:7/11/2020 访问量:53090

问:

嗨,我需要通过代码(又名 .VB)在 MSAccess 2003 中创建一个查询 - 我怎样才能完成这个?

VBA MS 访问

评论


答:

38赞 Fionnuala 12/24/2008 #1

一个模糊问题的模糊答案:)

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

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

评论

1赞 Andrew G. Johnson 12/24/2008
呃,在过去的 3 个小时里,我一直在尝试类似的事情:(谢谢一堆
7赞 Oliver R. 1/23/2014 #2

感谢您的回答和一小段代码。如果有人需要为所用变量定义数据类型,请使用以下命令:

    Dim strsql As Variant
    Dim qdf As QueryDef
7赞 Romana Ahmad 7/11/2017 #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