Listing issue in Excel

I have a form in Excel with a combo control. I want the values ​​to be populated from the database table when the combo box is opened using what has already been entered as LIKE criteria. This is the code that I still did for the DropButtonClick event to achieve this.

Private Sub cboVariety_DropButtonClick()
    Static search_text As String
    Static is_open As Boolean
    Dim rs As New Recordset

    If is_open Then
        is_open = False
        Exit Sub
    End If
    is_open = True

    If search_text = cboVariety Then Exit Sub
    search_text = cboVariety

    cboVariety.Clear
    cboVariety.AddItem search_text
    If Len(search_text) > 2 Then
        rs.Open _
            "SELECT Name FROM tbl_Varieties " & _
            "WHERE Name LIKE '%" & search_text & "%' " & _
            "ORDER BY Name", connect_string, adOpenStatic
        Do Until rs.EOF
            If rs!Name <> search_text Then cboVariety.AddItem rs!Name
            rs.MoveNext
        Loop
        rs.Close
    End If
End Sub

The problem is that the DropButtonClick event is fired both when the combo box is opened and when it is closed. If this auxiliary element is executed when the combo window closes, the code that clears the combo box causes the user to be deleted.

, is_open, , . . ?

+3
4

. , , , .

Private Sub cboVariety_DropButtonClick()
    Static search_text As String
    Dim rs As New Recordset

    If search_text = cboVariety Then Exit Sub
    search_text = cboVariety

    cboVariety.Clear
    If Len(search_text) > 2 Then
        rs.Open _
            "SELECT Name FROM tbl_Varieties " & _
            "WHERE Name LIKE '%" & search_text & "%' " & _
            "ORDER BY Name", connect_string, adOpenStatic
        Do Until rs.EOF
            cboVariety.AddItem rs!Name
            rs.MoveNext
        Loop
        rs.Close
    End If

    '' Reassign cboVariety in case this event was triggered by combo close
    cboVariety = search_text
End Sub
+1

, is_open boolean , , , - " ?"

, ? , , ( is_open). , ?

, , , - , - . , , . , , . , , .

, , , . Excel, , txtSearch Text. :

Private mPreviousSearchText As String

:

Private Sub cboVariety_DropButtonClick()                                      
    Dim rs As New Recordset                    
    Dim search_text As String

              search_text = txtSearch.Text

    If mPreviousSearchText = search_text Then                      
       'The current search matches the previous search,'
       'so we do not need to perform the update.'
        Exit Sub                    
    End If                                                    

    cboVariety.Clear                    
    cboVariety.AddItem search_text                    
    If Len(search_text) > 2 Then                    
        rs.Open _                    
            "SELECT Name FROM tbl_Varieties " & _
            "WHERE Name LIKE '%" & search_text & "%' " & _
            "ORDER BY Name", connect_string, adOpenStatic
        Do Until rs.EOF                    
            If rs!Name <> search_text Then cboVariety.AddItem rs!Name
            rs.MoveNext                    
        Loop                    
        rs.Close                                
    End If             
    'Set the previousSearchText var to be the search_text so that it does'
    'not run unless the value of my text box changes.'
    mPreviousSearchText = search_text 
End Sub

, , , , , , , .

+1

This worked for me, instead of assigning a value, I assign a property ListIndex.

index = cb.ListIndex

cb.Clear

while condition
    cb.AddItem item
Wend

If index < cbLinia.ListCount Then
    cb.ListIndex = index
Else
    cb.ListIndex = -1
End If
+1
source

Use GotFocus () instead.

 Private Sub ComboBox1_GotFocus()
    MsgBox "caca"
 End Sub

Triggers only when combos get focus.

NTN

0
source

Source: https://habr.com/ru/post/1763855/


All Articles