Thanks again June7,
I've moved on slightly and have built a form with a multi select list which passes the selection and opens a query with the criteria selected. It seem to work but I would like to add a tick box to the form and when it is ticked, pass a Y to the criteria and if not then a N. The code so far is below. Could anyone help (or indeed improve the code below)? Do I need to send more info to you?
Option Compare Database
Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM tblOTR"
'Build the IN string by looping through the listbox
For i = 0 To lstSpecialties.ListCount - 1
If lstSpecialties.Selected(i) Then
If lstSpecialties.Column(0, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & lstSpecialties.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [Specialty] in (" & Left(strIN, Len(strIN) - 1) & ") "
'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qry_AdvSearch"
Set qdef = MyDB.CreateQueryDef("qry_AdvSearch", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qry_AdvSearch", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.lstSpecialties.ItemsSelected
Me.lstSpecialties.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", , "Selection Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
DoCmd.Close
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_cmdClose_Click
End Sub