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

问题描述:

您好我需要通过代码(a.k.a. VB)在MSAccess 2003中创建一个查询 - 我该如何实现?通过MSAccess 2003中的代码动态创建查询[VBA]

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

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

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

唉我一直在努力的事情与此类似为过去三小时:(多谢 – 2008-12-24 00:01:55

感谢这个答案和一小段代码。如果有人需要定义中使用的变量数据类型,使用此:

Dim strsql As Variant 
    Dim qdf As QueryDef 
+4

昏暗STRSQL作为字符串 – Fionnuala 2014-07-11 12:05:34

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