Question Loop vs Comboxbox vs performance

paulo.oliversa

New member
Joined
Jun 1, 2014
Messages
2
Programming Experience
Beginner
Hello guys,

My name is Paulo, im from Brazil
Actually i use the combobox.items.add with the looping method to fill a combobox from a recordset. But its get too slow..there are a lot of records in the data base.

Is there another way (a better way) to do that?

Sorry...my english is sucks

Thxxx
 
The first lesson to learn is that, whenever you have a question, the first thing you should do is read the relevant documentation. This is from the documentation for that very Add method:
To add a set of items to the combo box it is best to use the AddRange method. If you choose to use the Add method to add a number of items to the combo box, use the BeginUpdate method to suspend repainting during your add and the EndUpdate method to resume repainting.
I would suggest a different approach though. For a start, if you're using a Recordset then you're using ADO. We're not using VB6 any more so why use VB6 data access technology? You're using VB.NET so use ADO.NET for data access. Use a data adapter to populate a DataTable and then bind that to the ComboBox, e.g.
Dim table As New DataTable

Using adapter As New SqlDataAdapter("SELECT ID, Name FROM SomeTable", "connection string here")
    adapter.Fill(table)
End Using

With Me.ComboBox1
    .DisplayMember = "Name"
    .ValueMember = "ID"
    .DataSource = table
End With
 
Thank you so much!

Can you help me to adapt the code?
I'm trying, but without success!


Actual code:


Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim codigu as string
codigu = "SELECT posicao from table"


cn = New ADODB.Connection
cn.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Informacoes.accdb;Persist Security Info=False")
rs = cn.Execute(codigu)

Do While Not rs.EOF
ComboBox.Items.Add(rs.Fields("Posicao").Value)
rs.MoveNext()
Loop
rs = Nothing
 
Thank you so much!

Can you help me to adapt the code?
I'm trying, but without success!


Actual code:


Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim codigu as string
codigu = "SELECT posicao from table"


cn = New ADODB.Connection
cn.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Informacoes.accdb;Persist Security Info=False")
rs = cn.Execute(codigu)

Do While Not rs.EOF
ComboBox.Items.Add(rs.Fields("Posicao").Value)
rs.MoveNext()
Loop
rs = Nothing

That code is using ADO for data access. Like I said, you should use ADO.NET. I don't see any attempt to use ADO.NET so you should do some reading on that first. The web is littered with examples.
 
Back
Top