I have an Access form that allows selection of fields from a list box. After the fields are selected, a button is clicked and it displays the fields. What I need is for the date field to filter by date, as in the "Between [Begin Date] and [End Date]" even if they don't select the date field. I have included a screen shot and the procedure code.
Any help is appreciated.
Private Sub cmdRunQuery_Click()
If Me.lstFieldList.ItemsSelected.Count = 0 Then
MsgBox "Select some field names first."
Dim qDef As Object
Dim SQL As String
Dim vItem As Variant
' loop through selected field names
For Each vItem In Me.lstFieldList.ItemsSelected
SQL = SQL & ",[" & Me.lstFieldList.ItemData(vItem) & "]"
' build new SQL statement
SQL = "Select " & Mid(SQL, 2) & " from [Employees]"
' save query with new SQL statement
Set qDef = CurrentDb.QueryDefs("qryEmployees")
qDef.SQL = SQL
Set qDef = Nothing
' run query