VBA Search Using Text Box in Access - vba

I'm very new to both Access and VBA. I've created a search button that looks for different items depending on what is selected on the different combo boxes. However, I would like to add another search criteria where if I type text into a texbox called "txtNotes" I can look for records that look like a match on a table field called "Notes" inside the "tbl_ContructionOrders" table. The matches have to be somewhat loose as everyone types notes differently and would like to use this box to find work orders where we had similar issues and maybe have an easier way finding a solution for said problems.
This is what I have so far and it is not working
Private Sub btnLookup_Click()
Dim strWhere As String
Me.Filter = True
strWhere = ""
If IsNull(Me.txtNotes) Then
If Not IsNull(Me.txtNotes) Then
If strWhere <> "" Then
strWhere = strWhere & " like [Notes] = """ & Me.txtNotes & """"
Else
strWhere = strWhere & "[Notes] = """ & Me.txtNotes & """"
End If
End If
If Len(strWhere) <= 0 Then
MsgBox "No Criteria", vbInformation, "No Input."
Else
Me.Filter = strWhere
Me.FilterOn = True
Me.Requery
End If
If Me.FilterOn Then
If Me.Recordset.RecordCount = 0 Then
MsgBox "Nothing Found."
End If
End If
End Sub

Try this:
Private Sub Command0_Click()
'empty notes
If IsNull(txtNotes.Value) Then
MsgBox "No Criteria", vbInformation, "No Input."
Exit Sub
End If
'search
Filter = "[Notes] Like '*" & txtNotes.Value & "*'"
FilterOn = True
'count records
If RecordsetClone.RecordCount = 0 Then
MsgBox "Nothing Found."
FilterOn = False
End If
End Sub
If you want to search (filter) as you type, use this:
Private Sub txtNotes_Change()
'search after x number of chars
If Len(txtNotes.Text) <= 3 Then
FilterOn = False
Filter = vbNullString
Else
Filter = "[Notes] Like '*" & txtNotes.Text & "*'"
FilterOn = True
End If
End Sub

It sounds like you want an auto-complete object. Jut the words that are to be auto-filled in a table. Use a combo box control on the form to select the word. Here is an instructional video that shows you how to set this up.
https://www.youtube.com/watch?v=ptRb8ffv4f0
If you need something else, post back.

Related

Search box not allowing me to type a space between words

I have a textbox on a form that is filtering my data by the company name. The reason for the close and open code in the error handling is because I couldn't find a way to easily fix it throwing an error when a combination of characters not present would be entered. This way it just closes and reopens it and basically resets it. I am still fairly new to this development and all I know is taught to myself through google and forums like this so forgive my lack of understanding when things should make sense to someone else able to do these types of functions.
Upon typing part of a company name and pressing space to type in a second word it essentially removes the space and puts the cursor back to the last letter typed.
This is the code for the textbox.
Reminder that I find solutions to the functions I need and adapt the code as best I can to suit my needs. I don't pretend to fully comprehend what I use yet and I'm still learning.
Private Sub txtSearch_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo errHandler
Dim filterText As String
'Apply or update filter based on user input.
If Len(txtSearch.Text) > 0 Then
filterText = txtSearch.Text
Me.Form.Filter = "[tblSuppliers]![SupplierName] like '*" & filterText & "*'"
Me.FilterOn = True
'Retain filter text in search box after refresh
txtSearch.Text = filterText
txtSearch.SelStart = Len(txtSearch.Text)
Else
'Remove filter
Me.Filter = ""
Me.FilterOn = False
txtSearch.SetFocus
End If
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbInformation + vbOKOnly, "Information"
DoCmd.Close acForm, "frmCosteeDetails", acSaveNo
DoCmd.OpenForm "frmCosteeDetails"
End Sub
In my search to try and find a way to fix the removal of spaces I found this function that someone listed but wasn't sure how to integrate it into my code.
Public Function FindWord(varFindIn As Variant, varWord As Variant) As Boolean
Const PUNCLIST = """' .,?!:;(){}[]/"
Dim intPos As Integer
FindWord = False
If Not IsNull(varFindIn) And Not IsNull(varWord) Then
intPos = InStr(varFindIn, varWord)
' loop until no instances of sought substring found
Do While intPos > 0
' is it at start of string
If intPos = 1 Then
' is it whole string?
If Len(varFindIn) = Len(varWord) Then
FindWord = True
Exit Function
' is it followed by a space or punctuation mark?
ElseIf InStr(PUNCLIST, Mid(varFindIn, intPos + Len(varWord), 1)) > 0 Then
FindWord = True
Exit Function
End If
Else
' is it precedeed by a space or punctuation mark?
If InStr(PUNCLIST, Mid(varFindIn, intPos - 1, 1)) > 0 Then
' is it at end of string or followed by a space or punctuation mark?
If InStr(PUNCLIST, Mid(varFindIn, intPos + Len(varWord), 1)) > 0 Then
FindWord = True
Exit Function
End If
End If
End If
' remove characters up to end of first instance
' of sought substring before looping
varFindIn = Mid(varFindIn, intPos + 1)
intPos = InStr(varFindIn, varWord)
Loop
End If
End Function
Edit - Code for Current Solution
Private Sub txtSearch_Change()
On Error GoTo errHandler
'clear filter
If Len(txtSearch.Text) = 0 Then
FilterOn = False
Filter = vbNullString
Exit Sub
End If
'apply filter
Filter = "[SupplierName] like '*" & txtSearch.Text & "*'"
FilterOn = True
Leave:
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbInformation + vbOKOnly, "Information"
Resume Leave
'DoCmd.Close acForm, "frmCosteeDetails", acSaveNo
'DoCmd.OpenForm "frmCosteeDetails"
End Sub
A simple filter method is as shown below. You will need to handle the Change() event and use the Text property which is populated on every keystroke.
Also, filtering does not requery data, so no need to try to manually retain the search value.
The below assumes the txtSearch and the data due to be filtered are on the same form. If that's not the case, the reference to the data-form will need to be changed.
Private Sub txtSearch_Change()
On Error GoTo errHandler
'clear filter
If Len(txtSearch.Text) = 0 Then
FilterOn = False
Filter = vbNullString
Exit Sub
End If
'apply filter
Filter = "[SupplierName] like '*" & txtSearch.Text & "*'"
FilterOn = True
Leave:
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbInformation + vbOKOnly, "Information"
Resume Leave
End Sub
Rather than using a text box to constantly update as typed just change it to a search button that when clicked searches based on the value in the search box. All you have to do is then update the search criteria each time and click search. a bit slower but functions mostly the same.
Private Sub cmdSearch_Click()
Dim strWhere As String
strWhere = "[tblSuppliers]![SupplierName] Like '*" & Me.txtSearch & "*'"
'Apply Filter
Me.Filter = strWhere
Me.FilterOn = True
End Sub

Filter form records by date range using two unbound text boxes

Good afternoon.
I have a continuous form which I want to allow the user to filter by using unbound boxes in the form header. See screenshot of form below:
Form Design View
I can filter error type (Unbound: cboErrorType) and responsibility (Unbound: cboResponsibility) without any issues, but I cannot get the date range to work (Records outside of the selected date range are being returned). Please see screenshot:
Form - filtered
Please see my code below:
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Const conJetDate = "\#dd\/mm\/yyyy\#"
If Not IsNull(Me.cboErrorType) Then
strWhere = strWhere & "([ErrorType] = """ & Me.cboErrorType & """) AND "
End If
If Not IsNull(Me.cboResponsibility) Then
strWhere = strWhere & "([Decision] = """ & Me.cboResponsibility & """) AND "
End If
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([Date of Error] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
End If
If Not IsNull(Me.txtEndDate) Then
strWhere = strWhere & "([Date of Error] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
'Debug.Print strWhere
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
I am removing the filters with the following code:
Private Sub cmdResetFilter_Click()
Dim ctl As Control
For Each ctl In Me.Section(acHeader).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
ctl.value = Null
End Select
Next
Me.FilterOn = False
End Sub
Please can someone suggest a solution?
If further information is required, let me know and I will oblige.
Many thanks.
The date format must be either the "reverse" US format (mm/dd/yyyy) or the ISO format (which also works for ADO). So try:
Const conJetDate = "\#yyyy\/mm\/dd\#"

Quick Filters in a Split Form Datasheet

Creating an access database for work. Users will use a split form with only the datasheet visible to review and manipulate numeric data. On the form I have built in quick filters that consist of of textboxes in which the values are either raised or lowered with arrow buttons that have on-click events. I currently have the text boxes linked to the recordsource query criteria.
With all of this stated, the problem that I am having is that I need the filter to act in the following manner:
If the value in the text box equals 0 I want to see all records. If the value is greater than 0, I want all records greater than or equal to the text box value to show. Finally, if the value in the text box is less than 0, I want to see all values less than or equal to 0.
I have considered trying to use multiple sql statements but I typically have about 3 of these quick filters on each form, and my project will eventually have about 20 forms. That is a lot of sql statements to potentially mess up.
What ideas do you guys have to solve this problem? I really need help.
If you only have 1 textbox on each form, then you may want to consider using the form's Filter property:
Private Sub txtFilter_AfterUpdate()
On Error GoTo E_Handle
If Not IsNull(Me!txtFilter) Then
If IsNumeric(Me!txtFilter) Then
Select Case Me!txtFilter
Case Is < 0
Me.Filter = "Price<=0"
Me.FilterOn = True
Case 0
Me.FilterOn = False
Case Is > 0
Me.Filter = "Price>=" & Me!txtFilter
Me.FilterOn = True
End Select
End If
End If
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "Form2!txtFilter_AfterUpdate", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
If need to have multiple filters, then consider moving the filter creation to a procedure by itself that handles all cases, and just call this procedure from each text box. You may have to think about the logic here of what happens if one text box is 0 (No filter), but another text box is 5 (display all values >= 5) and another text box is -3 (display all values <= 0):
Private Sub txtFilter2_AfterUpdate()
On Error GoTo E_Handle
Call sFilterForm2
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "Form2!txtFilter2_AfterUpdate", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Private Sub sFilterForm2()
On Error GoTo E_Handle
Dim strSQL As String
If Not IsNull(Me!txtFilter) Then
If IsNumeric(Me!txtFilter) Then
Select Case Me!txtFilter
Case Is < 0
strSQL = " AND Price<=0 "
Case 0
Case Is > 0
strSQL = strSQL & " AND Price>=" & Me!txtFilter
End Select
End If
End If
If Not IsNull(Me!txtFilter2) Then
If IsNumeric(Me!txtFilter2) Then
Select Case Me!txtFilter2
Case Is < 0
strSQL = " AND Price<=0 "
Case 0
Case Is > 0
strSQL = strSQL & " AND Price>=" & Me!txtFilter2
End Select
End If
End If
If Len(strSQL) > 0 Then
strSQL = Mid(strSQL, 5)
Me.Filter = strSQL
Me.FilterOn = True
Else
Me.FilterOn = False
End If
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "Form2!sFilterForm2", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards
Given a text box control CriteriaField1 and a corresponding field to filter Field1 in the record source I would use this:
Private Sub CriteriaField1_AfterUpdate()
Const FIELD_NAME As String = "Field1"
Dim value As Long
value = Nz(Me("Criteria" & FIELD_NAME).Value, 0)
Dim condition As String
condition = FIELD_NAME & IIf(value < 0, " <= 0", " >= " & value)
Me.FilterOn = value <> 0
End Sub
If you need to combine multiple fields to a filter condition, you would have to use and set form-global variables instead of local ones.
You could call a helper function which holds a set of arrays and builds and sets the filter dynamically:
Private Sub Filter0_AfterUpdate()
SetFilter
End Sub
Private Sub Filter1_AfterUpdate()
SetFilter
End Sub
Private Sub Filter2_AfterUpdate()
SetFilter
End Sub
Private Sub SetFilter()
Dim FieldNames() As Variant
Dim TextboxNames() As Variant
Dim Criteria() As String
Dim Index As Integer
Dim Value As Long
' Specify the field names to filter on.
FieldNames = Array("Quantity", "Stock", "Size")
' Specify the names of the textboxes to enter filter values.
TextboxNames() = Array("Filter0", "Filter1", "Filter2")
ReDim Criteria(LBound(TextboxNames) To UBound(TextboxNames))
For Index = LBound(Criteria) To UBound(Criteria)
Value = Val(Nz(Me(TextboxNames(Index)).Value))
If Value < 0 Then
Criteria(Index) = FieldNames(Index) & " <= 0"
ElseIf Value > 0 Then
Criteria(Index) = FieldNames(Index) & " >= " & CStr(Value)
Else
Criteria(Index) = "True"
End If
Next
' Assemble and apply the filter.
Me.Filter = Join(Criteria, " And ")
Me.FilterOn = True
Debug.Print Me.Filter
End Sub

Search Datasheet Column

I have a datasheet on a form with a qdf as recordsource.
I'd like to provide ability to search a specific column in the datasheet for the first/next occurrence (row) of the wildcarded string from a textbox on the same form (e.g., Jump to NEXT RECORD with MfgID like 123).
I'm not really looking for a FILTER, as I need to see the matching record(s) in context of surrounding records.
Any suggestions how to begin?
Thanks HansUp for the pointer. Here is what I implemented:
1. Sets focus to the desired column
2. Supports multiple btn clicks, to keep looking for the NEXT occurence
3. When no_match or end of list, returns to first record, clears txtSearch
--------------
Private Sub btnSearch_Click()
Dim rs As Object
On error goto err_handler
Forms![myForm]![mySubform].Form![MFGID].SetFocus
Set rs = Forms![myForm]![mySubform].Form.RecordsetClone
rs.FindNext "[MfgID] like '*" & Me![txtSearch] & "*'"
If Not Trim(Me![txtSearch]) = "" Then
Forms![myForm]![mySubform].Form.Bookmark = rs.Bookmark
Else
MsgBox "Please enter search criteria.", vbOKOnly, "Error"
Me![txtSearch].SetFocus
Exit Sub
End If
If Not rs.NoMatch Then
Forms![myForm]![mySubform].Form.Bookmark = rs.Bookmark
Else
MsgBox "Match not found for: " & Me![txtSearch] & "", , "Error"
rs.MoveFirst
Forms![myForm]![mySubform].Form.Bookmark = rs.Bookmark
Me![txtSearch] = ""
End If
err_exit:
Exit Sub
err_handler:
If Err.Number = 2455 Then GoTo err_exit
MsgBox Err.Number
exit sub

Access Filter with subform

I have a search box on one form that finds records by OrderID (ex 305321) on a checkout and ordering form and a second that I just added to a form to look up flooring products that searches the FLID value (ex FLID00005) the second however will only return a new record even when the data that I am searching for exists and when I use the enter functionality on it it asks for a parameter value which if I enter the same value that is in the search box it runs properly.
The code for the Orders search form:
Private Sub cmdFindbyOID_Click()
If IsNull([FindByOIDSearchBox]) Then
MsgBox "You must Enter a Order ID", vbInformation, "Error"
Exit Sub
End If
Me.Filter = "(([Orders In Progress Query].OrderID = " & FindByOIDSearchBox & "))"
Me.FilterOn = True
Me.[Orders All Details Subform].Requery
End Sub
Private Sub FindByOIDSearchBox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
cmdFindByOID.SetFocus
If IsNull([FindByOIDSearchBox]) Then
MsgBox "You must Enter a Order ID", vbInformation, "Error"
Exit Sub
End If
Me.Filter = "(([Orders In Progress Query].OrderID = " & FindByOIDSearchBox & "))"
Me.FilterOn = True
Me.[Orders All Details Subform].Requery
End If
End Sub
The code for the flooring products form:
Private Sub cmdFindbyFLID_Click()
If IsNull([findByFLIDSearchBox]) Then
MsgBox "You must Enter a Flooring ID", vbInformation, "Error"
Exit Sub
End If
Me.Filter = "(([Flooring Products Query].FLID = " & findByFLIDSearchBox & "))"
Me.FilterOn = True
End Sub
Private Sub FindByFLIDSearchBox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
cmdFindbyFLID.SetFocus
If IsNull([findByFLIDSearchBox]) Then
MsgBox "You must Enter a Flooring ID", vbInformation, "Error"
Exit Sub
End If
Me.Filter = "(([Flooring Products Query].FLID = " & findByFLIDSearchBox & "))"
Me.FilterOn = True
End If
End Sub
FLID contains characters. Therefore, your reference needs to be inside a single quote.
Me.Filter = "(([Flooring Products Query].FLID = '" & findByFLIDSearchBox & "'))"