Option Explicit
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim SkipChange As Boolean
Private Sub Combo1_Change()
'The change event often gets fired when we don't want to use it, so very often you'll see a flag
'that prevents the code logic from being fired at the wrong time. We want the change event
'every time the user changes the text in the combo box, but we don't want it every time the
'we change the text programmatically.
If SkipChange Then
Exit Sub
End If
'Change the recordset's filter to the contents of the combo box, or if the combo box is empty,
'remove the filter.
rs.Filter = IIf(Combo1.Text = "", "", "name like '" & Combo1.Text & "%'")
PopulateListBox
End Sub
Private Sub Form_Load()
'Opens up a connection, uses it to open a recordset. Recordset gets all the authors from the
'pubs sample database that ships with SQL Server, and puts them in a field called Name.
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
'connection source is assumed to be an SQL Server instance on the local machine
cn.Open "Provider=sqloledb;Data Source=.;Initial Catalog=pubs;Integrated Security=SSPI;"
'select statement takes last name, adds a comma and a space, adds the first name, and stores
'the result in a field called Name.
rs.Open "select au_lname + ', ' + au_fname as Name from authors", cn
'do the initial population of the list box
PopulateListBox
End Sub
Private Sub PopulateListBox()
Dim cText As String
With Combo1
'Store the existing text to a variable, since the Clear method removes it.
cText = .Text
.Clear
'If you don't set the SkipChange flag here, you'll get an out of stack space error due to
'an unintended recursive procedure call. Setting the text property calls Combo1_Change,
'which calls this proc, and so on until you run out of memory. To demonstrate this,
'comment out the SkipChange variable and examine the call stack when you get the error.
SkipChange = True
'Now, put back the text that we removed with the Clear method
.Text = cText
SkipChange = False
'Then, put the cursor at the end of the string
.SelStart = Len(.Text)
'Here, we're going to exit if there are no matches to the string entered in Combo1, since
'we don't have any repopulating to do. Not to mention, we'd get an error at the MoveFirst
'method if the recordset's empty.
If rs.EOF And rs.BOF Then
Exit Sub
End If
'Finally, repopulate the list box with the filtered recordset.
rs.MoveFirst
Do Until rs.EOF
.AddItem rs(0)
rs.MoveNext
Loop
End With
End Sub