Good morning, i was hoping someone could help me with the code to accomplish this.
I have a form (frmMultiDealerSalesByYear) with two unbound fields (fiscalyear) <- combo and (liDealreship) <-- multi select list box.
I want both those forms to help generate a query, where the fiscal year will be one selection of the year of sale, and the dealership could be one or many selections.
The query name is (qryDealerSalesByFiscalYear) . I have tried examples i found online, but i do not know how to edit them properly to fit my format i am guessing.
One example i have tried with no luck is:
Code:
Dim Q As QueryDef, DB As Database
Dim Criteria As String
Dim ctl As Control
Dim Itm As Variant
' Build a list of the selections.
Set ctl = Me![liDealership]
For Each Itm In ctl.ItemsSelected
If Len(Criteria) = 0 Then
Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
Else
Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
& Chr(34)
End If
Next Itm
If Len(Criteria) = 0 Then
Itm = MsgBox("You must select one or more items in the" & _
" list box!", 0, "No Selection Made")
Exit Sub
End If
' Modify the Query.
Set DB = CurrentDb()
Set Q = DB.QueryDefs("qryDealerSalesByFiscalYear")
Q.SQL = "Select * From Orders Where [DealerName] In(" & Criteria & _
");"
Q.Close
' Run the query.
DoCmd.OpenQuery "qryDealerSalesByFiscalYear"
Any help would be fantastic, thanks!
edit: when i run that code, it comes back with "You cancelled the previous operation" and highlights my docmd.openquery