가장 효율적인 방법을 참조하는 항목이 있는 경우 또는지에 listbox 컨트롤

StackOverflow https://stackoverflow.com/questions/105935

문제

이 요청을 기반으로 MS 액세스 VBA.나는 무엇을 알고 가장 효율적인 방법을 참조하는 항목이 있는 경우에 존재하는 listbox 제어합니다.

도움이 되었습니까?

해결책

샘플 기능이 적용 할 수있다.

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

다른 팁

불행하게도 없습니다 더 효율적인 방법보다 선형 검색하지 않는 한,당신은 당신의 listbox 정렬 또는 인덱스에서 어떤 특정 패션이다.

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

지 않으면 마음에 의존하 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)

는 반환해야 합니다 인덱스가 포함된 행의 대상에는 문자열.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top