I am trying to get a filtered list in a listbox in msAccess by setting the listbox.rowsource using VBA. The following code sample has comments that explain everything I've tried. To run the code create a small table "tblsop" with two fields matching the SQL columns, both fields are text. then uncomment the various attempts at setting searchSQL
Private Sub Form_Open(Cancel As Integer)
Dim searchSQL As String
' the following commented out versions of setting searchSQL show what I
' have tried and what works vs what doesn't work. I can't find a version
' of setting searchSQL = that works. forms!frmSearch.txt1 evaluates to
' the string chem in my testing
' this displays the whole table of rows in the listbox of this form
' searchSQL = "select sopid, sopname, soplink from tblSOP"
' this works also just to show it is not only adding a where that kills it
' searchSQL = "select sopid, sopname, soplink from tblSOP where 1=1"
' the next two display empty listbox with no columns
' searchSQL = "select sopid, sopname, soplink from tblSOP where sopName like ""*" & Forms!frmsearch.txt1.Value & "*"""
' debug.print searchSQL = select sopid, sopname, soplink from tblSOP where sopName like "*chem*"
' searchSQL = """select sopid, sopname, soplink from tblSOP where sopName like ""*" & Forms!frmsearch.txt1.Value & "*"""""
' debug.print searchSQL = "select sopid, sopname, soplink from tblSOP where sopName like "*chem*""
' this one I got from a web answer to another question so I tried the # as delimiters
' this one displayed 2 columns but they were empty
' searchSQL = "select sopid, sopname, soplink from tblSOP where sopName like #*" & Forms!frmsearch.txt1.Value & "*#"
' debug.print = select sopid, sopname, soplink from tblSOP where sopName like #*chem*#
resultsList.RowSource = searchSQL
' I have tried resultsList.requery here and also several variations of
' resultslist.recordsourcetype to no avail. A test of the last two searchSQL
' variations using a testSQL(searchSQL) routine works fine showing two records
' in the immediate window. somehow programatically setting rowsource evaluates quotes
' differently than sending it with openrecordset() (used in testsql)
End Sub