通过 MSAccess 2003 [VBA] 中的代码动态创建查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/390420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:32:22  来源:igfitidea点击:

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

vbams-accessaccess-vba

提问by Andrew G. Johnson

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

嗨,我需要通过代码(又名 VB)在 MSAccess 2003 中创建一个查询——我怎样才能做到这一点?

回答by Fionnuala

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

回答by Oliver R.

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

回答by Romana Ahmad

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