动态创建查询

问题描述 投票:0回答:4

您好,我需要通过代码(又名 VB)在 MSAccess 2003 中创建一个查询 - 我该如何完成此操作?

vba ms-access
4个回答
39
投票

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

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

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

8
投票

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

    Dim strsql As Variant
    Dim qdf As QueryDef

8
投票
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

0
投票

不需要打开查询,只需写前2行即可,

strSQL="SELECT * FROM tblT WHERE ID =" & Forms!Form1!txtID 设置 qdf=CurrentDB.CreateQueryDef("NewQuery",strSQL)

© www.soinside.com 2019 - 2024. All rights reserved.