我想基于同一表单中前一个语句的Where条件创建一个查询,但是当我运行该按钮时没有创建查询。
这样的事情:
Dim qdf As QueryDef
strSql = "SELECT * FROM qryEvents WHERE strWhere"
'Delete the query if it already exists
DoCmd.DeleteObject acQuery, "qryEvents_Search"
Set qdf = CurrentDb.CreateQueryDef("qryEvents_Search", strWhere)
DoCmd.OpenQuery "qryEvents_Search"
'release memory
qdf.Close
Set qdf = Nothing
Where条件部分是:
If Len(strWhere) > 0 Then
If Nz(DCount("*", "qryEvents", Left(strWhere, Len(strWhere) - 0)), 0) > 0 Then
strWhere = "WHERE " & Left(strWhere, Len(strWhere) - 0)
strSQL = "SELECT * FROM qryEvents " & strWhere & ";"
答案 0 :(得分:2)
WHERE子句不进行查询。你也需要SELECT部分。
e.g。
strNewSql = "SELECT * FROM otherEvents " & strWhere
Set qdf = CurrentDb.CreateQueryDef("qryEvents_Search", strNewSql)