热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

查看项目是否在列表框控件中的最有效方法-Mostefficientwaytoseeifanitemisorisnotinalistboxcontrol

ThisrequestisbasedinMSAccessVBA.Iwouldliketoknowwhatthemostefficientwayis,tosee

This request is based in MS Access VBA. I would like to know what the most efficient way is, to see if an item exists in a listbox control.

此请求基于MS Access VBA。我想知道最有效的方法是查看列表框控件中是否存在某个项目。

3 个解决方案

#1


2  

Here is a sample function that might be adapted to suit.

这是一个可能适合的示例函数。

Function CheckForItem(strItem, ListB As ListBox) As Boolean
Dim rs As DAO.Recordset
Dim db As Database
Dim tdf As TableDef

    Set db = CurrentDb

    CheckForItem = False

    Select Case ListB.RowSourceType
        Case "Value List"
            CheckForItem = InStr(ListB.RowSource, strItem) > 0

        Case "Table/Query"
            Set rs = db.OpenRecordset(ListB.RowSource)

            For i = 0 To rs.Fields.Count - 1
                strList = strList & " & "","" & " & rs.Fields(i).Name
            Next

            rs.FindFirst "Instr(" & Mid(strList, 10) & ",'" & strItem & "')>0"

            If Not rs.EOF Then CheckForItem = True

        Case "Field List"

            Set tdf = db.TableDefs(ListB.RowSource)

            For Each itm In tdf.Fields
                If itm.Name = strItem Then CheckForItem = True
            Next

    End Select

End Function

#2


1  

Unfortunately there is no more efficient way than a linear search, unless you know that your listbox is sorted or indexed in some particular fashion.

不幸的是,没有比线性搜索更有效的方法,除非你知道你的列表框是以某种特定方式排序或索引的。

For i = 1 To TheComboBoxControl.ListCount
  if TheComboBoxControl.ItemData(i) = "Item to search for" Then do_something()
Next i

#3


1  

If you don't mind resorting to the Windows API you can search for a string like this:

如果您不介意使用Windows API,可以搜索如下字符串:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long   
Private Const LB_FINDSTRINGEXACT = &H1A2

Dim index as Integer
Dim searchString as String
searchString = "Target" & Chr(0)

index = SendMessage(ListBox1.hWnd, LB_FINDSTRINGEXACT , -1, searchString)

Which should return the index of the row that contains the target string.

哪个应该返回包含目标字符串的行的索引。


推荐阅读
author-avatar
哀乐交加6
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有