添加where子句时,ms access vba listbox.rowsource格式化没有结果

时间:2018-08-19 14:45:05

标签: vba ms-access listbox

我试图通过使用VBA设置listbox.rowsource来在msAccess的列表框中获取过滤列表。以下代码示例的注释说明了我尝试过的所有内容。要运行代码,请创建一个小表“ tblsop”,其中两个字段与SQL列匹配,两个字段均为文本。然后取消注释各种设置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

1 个答案:

答案 0 :(得分:0)

我找到了解决我特定问题的答案。请参阅选项/对象设计器/ sqlserver兼容,将ANSI92设置为yes。这本身并不是我感到如此艰难的原因,而是在调试模式下该选项没有任何意义。它仅在实际运行表格时有意义。我认为是NASTY NASTY设计错误。我正在上传一个示例数据库,该数据库清楚地显示了问题,并说明了解决问题的步骤。这使我花了一周多的时间才能交付给客户。 optionSQL92ConmpatProblems.zip