I have the coding below in VB. I have a user form that allows the user to select a date range & an Open or Closed status or both. If you select a date range one of the status selections the query only looks at the status you selected & brings up everything for that status. If you open the query back up after you run the code in design mode it is blank except for the status you selected on the form. I can't figure out what's missing.
Private Sub OK_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("Range")
' Loop through the selected items in the list box and build a text string
If Me!Status1.ItemsSelected.Count > 0 Then
For Each varItem In Me!Status1.ItemsSelected
strCriteria = strCriteria & "[Vendor Hotline Log].Status = " & Chr(34) _
& Me!Status1.ItemData(varItem) & Chr(34) & " OR "
Next varItem
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
End If
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM [Vendor Hotline Log] " & _
"WHERE " & strCriteria & ";"
' Apply the new SQL statement to the query
Debug.Print strSQL
qdf.SQL = strSQL
' Open the query
DoCmd.OpenReport "Date Range", acViewPreview
DoCmd.Close acForm, "Search Criteria Form", acSaveNo
End Sub
Bookmarks