Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Option Compare Database
Option Explicit
Public faytProducts As New FindAsYouTypeCombo
Private Sub Form_Load()
faytProducts.InitalizeFilterCombo Me.cmbProducts, "ProductName", False
End Sub
Private Sub TypeID_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 40 Then Me.TypeID.Dropdown End IfEnd Sub
Option Compare Database
Option Explicit
'Class Module Name: FindAsYouTypeCombo
'Purpose: Turn any combobox into a "Find As You Type" 'Combobox
'Created by: MajP
'
'Use:To use the class, you need code similar to the 'following in a form's module.
'Also requires a reference to DAO
'Parmaters:
' TheComboBox: Your Combobox object passed as an object
' FilterFieldName: The name of the field to Filter as string
' FilterFromStart: Determines if you filter a field that starts with the text or if the
' text appears anywhere in the record.
'
'*******Start Form Code*******************
'
'Option Compare Database
'Option Explicit
'Public faytProducts As New FindAsYouTypeCombo
'Private Sub Form_Load()
' faytProducts.InitalizeFilterCombo Me.cmbProducts, "ProductName", False
'End Sub
'
'******* END Form Code ******************
Private WithEvents mCombo As Access.ComboBox
Private WithEvents mForm As Access.Form
Private mFilterFieldName As String
Private mRsOriginalList As DAO.Recordset
Private mFilterFromStart As Boolean
Private mStopFilter As Boolean
Public Property Get FilterComboBox() As Access.ComboBox
Set FilterComboBox = mCombo
End Property
Public Property Set FilterComboBox(TheComboBox As Access.ComboBox)
Set mCombo = TheComboBox
End Property
Private Sub mCombo_Change()
If Not mStopFilter Then
Call FilterList
End If
End Sub
Private Sub mCombo_GotFocus()
mCombo.Dropdown
End Sub
Private Sub mCombo_AfterUpdate()
mStopFilter = False
Call unFilterList
End Sub
Private Sub mCombo_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 40 Then
mStopFilter = True
End If
End Sub
Private Sub mForm_Current()
Call unFilterList
End Sub
Private Sub FilterList()
On Error GoTo errLable
Dim rsTemp As DAO.Recordset
Dim strText As String
Dim strFilter As String
strText = mCombo.Text
If mFilterFieldName = "" Then
MsgBox "Must Supply A FieldName Property to filter list."
Exit Sub
End If
strText = Replace(strText, "'", "''")
If mFilterFromStart = True Then
strFilter = mFilterFieldName & " like '" & strText & "*'"
Else
strFilter = mFilterFieldName & " like '*" & strText & "*'"
End If
Set rsTemp = mRsOriginalList.OpenRecordset
rsTemp.Filter = strFilter
Set rsTemp = rsTemp.OpenRecordset
If rsTemp.RecordCount > 0 Then
Set mCombo.Recordset = rsTemp
End If
mCombo.Dropdown
Exit Sub
errLable:
If Err.Number = 3061 Then
MsgBox "Will not Filter. Verify Field Name is Correct."
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub
Private Sub unFilterList()
On Error GoTo errLable
Set mCombo.Recordset = mRsOriginalList
mStopFilter = False
Exit Sub
errLable:
If Err.Number = 3061 Then
MsgBox "Will not Filter. Verify Field Name is Correct."
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub
Public Property Get FilterFieldName() As String
FilterFieldName = mFilterFieldName
End Property
Public Property Let FilterFieldName(ByVal theFieldName As String)
mFilterFieldName = theFieldName
End Property
Private Sub Class_Terminate()
Set mForm = Nothing
Set mCombo = Nothing
Set mRsOriginalList = Nothing
End Sub
Public Sub InitalizeFilterCombo(TheComboBox As Access.ComboBox, FilterFieldName As String, Optional FilterFromStart = True)
On Error GoTo errLabel
If Not TheComboBox.RowSourceType = "Table/Query" Then
MsgBox "This class will only work with a combobox that uses a Table or Query as the Rowsource"
Exit Sub
End If
Set mCombo = TheComboBox
Set mForm = TheComboBox.Parent
mFilterFieldName = FilterFieldName
mFilterFromStart = FilterFromStart
mForm.OnCurrent = "[Event Procedure]"
mCombo.OnGotFocus = "[Event Procedure]"
mCombo.OnChange = "[Event Procedure]"
mCombo.AfterUpdate = "[Event Procedure]"
mCombo.OnKeyDown = "[Event Procedure]"
With mCombo
.SetFocus
.AutoExpand = False
End With
Set mRsOriginalList = mCombo.Recordset.Clone
Exit Sub
errLabel:
MsgBox Err.Number & " " & Err.Description
End Sub
Yes they were not designed to work together.
The search form is primitive because it only allows you to search one field at a time.If you can understand this then, I will show the next step to have a button to make a complex search and a key word search.
I still think the type of search form I showed is far better than the generic keyword search. Just my opinion with a couple of thousand databases under my belt.
If you fix this throughout I can send you a real nice search form with a lot of utility, based on what you have.
If Not Trim(Me.qType1 & " ") = "" Then
strType = "[TypeID] = '" & qType1 & strType ="[sType1] = '" & sType1 "
End If
Run-time error '2465':
Microsoft Office Access can't find the field '|' referred to in your expression.
Private Sub cmdFilterAdvanced_Click()
'put in your error checking
DoCmd.OpenForm "frmFilterSearchAdvanced", , , , , acDialog
'code stops here until search form closed/hidden
If CurrentProject.AllForms("frmFilterSearchAdvanced").IsLoaded Then
[COLOR=green]'this section of code is highlighted when the Go To Selected Tool button is clicked in the filter form[/color]
[COLOR=red]Me.Filter = Forms("frmFilterSearchAdvanced").getFilter[/color]
If Not Me.Filter = "" Then
[COLOR=green]'this section of code is highlighted when the Apply Filter button is clicked in the filter form[/color]
[COLOR=red]Me.FilterOn = True [/color]
End If
If Me.Recordset.RecordCount = 0 Then
MsgBox "No Records Found"
Me.Filter = ""
Me.FilterOn = False
Else
MsgBox Me.Recordset.RecordCount & " Tools found meeting the Filter."
End If
DoCmd.Close acForm, "frmFilterSearchAdvanced"
End If
End Sub