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

And*_*son 17 ms-access vba access-vba

嗨,我需要通过代码(又名VB)在MSAccess 2003中创建一个查询 - 我该如何实现这一目标?

Fio*_*ala 34

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

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

Set qdf=CurrentDB.CreateQueryDef("NewQuery",strSQL)
DoCmd.OpenQuery qdf.Name
Run Code Online (Sandbox Code Playgroud)


Oli*_* R. 6

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

    Dim strsql As Variant
    Dim qdf As QueryDef
Run Code Online (Sandbox Code Playgroud)

  • Dim strSQL As String (5认同)

小智 5

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
Run Code Online (Sandbox Code Playgroud)