-----Original Message-----
FYI
A Multi-Select ListBox can not be used directly in the Query Grid Criteria.
e.g. Forms![FormName]![ListBoxName] will NOT work.
Why?
Simply examine the output of the Selection and you will see that the string
produced is different from the one the Query Grid requires:
"MyText1 Or MyText2 Or MyText3" from Listbox
"MyText1" Or "MyText2" Or "MyText3" from query grid.
The solution to the multi-select listbox problem is to write the query in
code so that it looks the same way the query grid does.
Build the criteria based on selected values that you can determine by
looping through the selected property of the list box.
I usually write the query in the Grid and use multiple criteria and then
copy the SQL to code and work it into this format:
Private Sub btnCreateInvoice_Click()
On Error GoTo Err_btnCreateInvoice_Click
Dim frm As Form, ctl As Control, varItm As Variant, strCriteria As
String
Dim dbs As Database, qdf As QueryDef
Set dbs = CurrentDb
Set frm = Forms![CreateInvoiceData]
Set ctl = frm!ListPOs
strCriteria = ""
For Each varItm In ctl.ItemsSelected
strCriteria = strCriteria & "(pohdr.ponumber)='" &
Trim(ctl.ItemData(varItm)) & "' Or "
Next varItm
If strCriteria = "" Then
MsgBox "Select one or more PO's."
Exit Sub
End If
strCriteria = Left(strCriteria, Len(strCriteria) - 4)
strSQL = "SELECT [invnumber], Date() AS invdate, pohdr.vendno "
strSQL = strSQL & "FROM pohdr INNER JOIN poln ON pohdr.pokey =poln.pokey
"
strSQL = strSQL & "WHERE (" & strCriteria & ");"
If QueryExists("Inv1") = True Then
dbs.QueryDefs.Delete "Inv1"
End If
Set qdf = dbs.CreateQueryDef("Inv1")
qdf.SQL = strSQL
Set ctl = Nothing
Set frm = Nothing
Set dbs = Nothing
Exit_btnCreateInvoice_Click:
Exit Sub
Err_btnCreateInvoice_Click:
MsgBox ("Error # " & str(Err.Number) & " was generated by " & Err.Source
& Chr(13) & Err.Description)
Resume Exit_btnCreateInvoice_Click
End Sub
Function QueryExists(strQueryName As String) As Boolean
On Error Resume Next
QueryExists = IsObject(CurrentDb.QueryDefs (strQueryName))
End Function
To de-select all items in a list box try:
Dim lngX As Long
With Me![lstMyListBox]
For lngX = (.ItemsSelected.Count - 1) To 0 Step - 1
.Selected(.ItemsSelected(lngX)) = False
Next lngX
End With
--
Joe Fallon
Access MVP
Bob Barnes said:
I posted this question below, but want to redefine it.
When using the QBE-Grid, entering "1 Or 2" as a Criteria
works properly.
Trying to get the same result from an unbound field on a
Form (Criteria of [Forms]![frmMain]![ABC]) returns NO
records if the unbound field has something like "1 Or 2".
I build the "1 Or 2" from a multi-select List Box that
populates properly.
I even ran this thru code (QueryDef, Parameter) but
it still fails when the unbound control has the "1 Or 2".
Again, assistance welcomed.
TIA - Bob
.