Loading data into a combo box is slow

I have a VB6 application with a search screen. In the search I have 9 combo boxes. Some of the lists have only a couple of items, but some have a couple of hundreds of items. It takes a long time to fill in the data (a couple of seconds).

Each combo box is configured the same way: Sorted = False, Style = 2 - Dropdown List

3 of the lists with a list of less than 20 items. 1 has 130 items. 4 have approximately 250 items; 1 has nearly 700 items.

I fill out all nine combined fields with similar code.

While Not RS.EOF

    cmbX.List(i) = RS("Description")
    cmbX.ItemData(i) = RS("Id")

    i = i + 1

    RS.MoveNext
Wend

I tried to set Visible = False, but this did not affect the performance.

Is there any other way to populate a combo box that will work better than my existing method?

+3
7

. 60% , Windows API, AddItem

Private Const CB_ERR As Long = -1
Private Const CB_ADDSTRING As Long = &H143
Private Const CB_RESETCONTENT As Long = &H14B
Private Const CB_SETITEMDATA As Long = &H151

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

Public Sub AddItem(cmb As ComboBox, Text As Variant, Optional ItemData As Long)

   Dim l As Long
   Dim s As String

   If VarType(Text) = vbString Then
      s = Text
   Else
      s = Trim$(Str$(Text))
   End If

   l = SendMessage(cmb.hwnd, CB_ADDSTRING, 0&, ByVal s)
   If l <> CB_ERR Then
      SendMessage cmb.hwnd, CB_SETITEMDATA, l, ByVal ItemData
   End If

End Sub

Public Sub Clear(cmb As ComboBox)
   SendMessage cmb.hwnd, CB_RESETCONTENT, 0, 0&
End Sub

, , API .

+8

. 700 . , .

, ​​:

yourproduct.jpg

+3

:

  • With RS.

  • RecordCount Recordset object EOF ( RecordCount = -1, , .., RecordCount).

  • For..Next, .

  • bang (!).

:

With RS

  Debug.Assert .RecordCount >= 0

  Dim counter As Long
  For counter = 0 To .RecordCount - 1

    cmbX.List(counter) = !Description
    cmbX.ItemData(counter) = !Id

    .MoveNext
  Next

End With

, - , combo Sorted False, , Recordset Sort, (, ORDER BY SQL ).

+2

, . WM_SETREDRAW. - , , , , , , , .

  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 WM_SETREDRAW = &HB

  Call SendMessage(cmbX.hWnd, WM_SETREDRAW, 0, 0&)
  'DO STUFF'
  Call SendMessage(cmbX.hwnd, WM_SETREDRAW, 1, 0&)

: - VB6 , LockWindowUpdate. , . , !

+2

, List (i) ItemData (i)?

While Not RS.EOF

    cmbX.AddItem RS("Description")
    cmbX.ItemData(cmbX.NewIndex) = RS("Id")

    RS.MoveNext
Wend

. MarkJ.

, , . , . , .

+1

. ? ( , )? , . , SQL , (.. SELECT *).

SQL WHERE JOINS, , .

ACCESS , SQL Server Express.

0

'' COMBOBOX

20 , 80 000 ,

    Me.txt_City.DataSource = tblCities
    Me.txt_City.ValueMember = "city"
    Me.txt_City.DisplayMember = "city"

, , 5

    Me.txt_City.ValueMember = "city"
    Me.txt_City.DisplayMember = "city"

    Me.txt_City.DataSource = tblCities

0

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


All Articles