If the two
Text Boxes (not Fields) are named "
txtYear" and "
txtQ" and if the command button is named "
cmdGetFilter", then the code for calling the function "
BuildQueryFilter" is as follows:
Code:
Private Sub cmdGetFilter_Click()
Dim strFilter As String
strFilter = BuildQueryFilter(Me.txtYear.value, Me.txtQ.value)
End Sub
Once you have the criteria (in "
strFilter") you can use "strFilter" as a "
WhereCondition" parameter to open another Form:
Code:
DoCmd.OpenForm "MyForm", , , strFilter
Or a Report:
Code:
DoCmd.OpenReport "MyReport", , , strFilter
You can also use it to create a new Query or modify an existing one:
Code:
Dim strSQL as String
strSQL = "SELECT * FROM MyTable WHERE " & strFilter
CreateQuery "MyQuery", strSQL
With a procedure "
CreateQuery" such as:
Code:
Sub CreateQuery(ByVal QueryName As String, ByVal SQL As String)
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Set dbs = CurrentDb
For Each qdf In dbs.QueryDefs
If qdf.Name = QueryName Then Exit For
Next qdf
If Not qdf Is Nothing Then
qdf.SQL = SQL
Else
Set qdf = dbs.CreateQueryDef(QueryName, SQL)
End If
dbs.QueryDefs.Refresh
Set qdf = Nothing
Set dbs = Nothing
End Sub