Joseph Bupe
Joseph Bupe

Reputation: 77

Creating A Query From SQL Where Condition

I want to create a a query based on the Where condition from the previous statement in the same form, but the query is not being created when i run the button.

Something like this:

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

The Where condition part is:

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 & ";"

Upvotes: 2

Views: 526

Answers (1)

Andre
Andre

Reputation: 27634

A WHERE clause doesn't make a query. You need the SELECT part too.

e.g.

strNewSql = "SELECT * FROM otherEvents " & strWhere
Set qdf = CurrentDb.CreateQueryDef("qryEvents_Search", strNewSql)

Upvotes: 2

Related Questions