Bob,
I recently developed an applicaiton for a client where I had much the same
situation. I really needed a list box to display the list of records but
I
also needed the capability of the user to be able to type in values and
have
the list box immediately filtered to the typed criteria.
In my situation I had a list of individuals names, both first and last
names. So in my case I have two text boxes above the list box. Actully
my
text boxes are in the header of my form and the list box is in the details
section.
In a nutshell what I am doing is that I am creating the sql statement on
the
fly that will return the filtered values for the list box. In my case the
user can use either or both of the text boxes to type a value into for
filtering the list box. I also have a command button that lets the user
clear all entries in the two text boxes. I also have a group control that
has two options for sorting the list box in asscending and descending
order.
I am going to provide you with the code for my controls and hope that the
code will be at least helpful.
As an explanation, here are the names of my controls:
txtFirstName = name of text box where user can type in the first name
txtLastName = name of text box where user cna type in the last name
cmdClearAll = name of command button to clear what user types into text
boxes
grpSortList = name of group control with two option buttons for sorting
order
lstContacts = name of list box control
I also have the following variables defined in the declarations area of my
VBA code for the form.
Dim strActiveControl As String
This variable is being set in the "Got Focus" event of each of the text
boxes.
Here is the function that is called to set the variables value
Function SetCurControlName()
strActiveControl = Me.ActiveControl.Name
End Function
Here is the code for the Got Focus event of the "txtFirstName" text box
Private Sub txtFirstName_GotFocus()
SetCurControlName
End Sub
Here is the code for the Got Focus event of the "txtLastName" text box
Private Sub txtLastName_GotFocus()
SetCurControlName
End Sub
Here is the code for the On Change event of the "txtFirstName" text box
Private Sub txtFirstName_Change()
If Me.txtFirstName.Text > "" Then
Me.cmdClearAll.Enabled = True
Else
If Me.txtLastName > "" Then
Me.cmdClearAll.Enabled = True
Else
Me.cmdClearAll.Enabled = False
End If
End If
'call the "GetContactsList" function
GetContactsList
End Sub
Here is the code for the On Change event of the "txtLastName" text box
Private Sub txtLastName_Change()
If Me.txtLastName.Text > "" Then
Me.cmdClearAll.Enabled = True
Else
If Me.txtFirstName > "" Then
Me.cmdClearAll.Enabled = True
Else
Me.cmdClearAll.Enabled = False
End If
End If
'call the "GetContactsList" function
GetContactsList
End Sub
Here is the "GetContactsList" function
Function GetContactsList()
Select Case strActiveControl
Case "txtFirstName"
strStartSql = "SELECT Contacts.ContactID, [sFirstName] & "" "" &
[slastname] & "", " _
& """ & [Contacts]![sCity] & "", "" &
[Contacts]![sStateOrProvince] AS Name FROM Contacts"
varFName = Me.txtFirstName.Text
If Me.txtLastName = "" Then
varLName = ""
Else
varLName = Me.txtLastName
End If
If varFName > "" Then
If varLName > "" Then
strNameSql = " WHERE (((Contacts.sLastName) Like """ &
varLName
& "*"") " _
& "AND ((Contacts.sFirstName) Like """ & varFName &
"*""))"
Else
strNameSql = " WHERE (((Contacts.sFirstName) Like """ &
varFName & "*"")) "
End If
ElseIf varLName > "" Then
strNameSql = " WHERE (((Contacts.sLastName) Like """ & varLName &
"*""))"
Else
strNameSql = ""
End If
Select Case Me.grpSortList
Case 1
strOrderSql = " ORDER BY Contacts.sFirstName, Contacts.sLastName;"
Case 2
strOrderSql = " ORDER BY Contacts.sFirstName DESC,
Contacts.sLastName;"
End Select
With Me.txtFirstName
.SetFocus
.SelStart = Len(varFName) + 1
End With
Case "txtLastName"
strStartSql = "SELECT Contacts.ContactID, [sLastName] & "", "" & " _
& "[sFirstname] & "", "" & [Contacts]![sCity] & "", ""
&
" _
& "[Contacts]![sStateOrProvince] AS Name FROM Contacts"
varLName = Me.txtLastName.Text
If Me.txtFirstName = "" Then
varFName = ""
Else
varFName = Me.txtFirstName
End If
If varLName > "" Then
If varFName > "" Then
strNameSql = " WHERE (((Contacts.sLastName) Like """ & " _
& "varLName & "*"") AND ((Contacts.sFirstName)
" _
& "Like """ & varFName & "*""))"
Else
strNameSql = " WHERE (((Contacts.sLastName) Like """ & " _
& "varLName & "*"")) "
End If
ElseIf varFName > "" Then
strNameSql = " WHERE (((Contacts.sLastName) Like """ & varFName &
"*""))"
Else
strNameSql = ""
End If
Select Case Me.grpSortList
Case 1
strOrderSql = " ORDER BY Contacts.sLastName, Contacts.sFirstName;"
Case 2
strOrderSql = " ORDER BY Contacts.sLastName DESC ,
Contacts.sFirstName;"
End Select
With Me.txtLastName
.SetFocus
.SelStart = Len(varLName) + 1
End With
End Select
strSql = strStartSql & strNameSql & strOrderSql
With Me.lstContacts
.RowSource = strSql
.Requery
.Value = Null
End With
End Function
'end of code
Please be aware that this code is not intended to actully apply to your
situation but should only be use as a template for how to accomplish what
you
want to do.
I hope this is not too confusing.
--
HTH
Mr B
askdoctoraccess dot com
Bob Howard said:
I'm aware of your "combo box alternative" and the AutoExpand property.
But
the nature of the application is such that a ListBox is what's needed on
the
screen.
Bob
Is there a reason why you can't use a combobox instead of a listbox?
With
AutoExpand Property set to Yes, which is the default setting, it does
this
automatically.