I have a form that I want to search for anything containing what is entered into a textbox. Right now the search only picks up data that matches exactly (ie MDD), but I want it to capture anything containing the searched item automatically (ie *MDD*)
Ideally I would like a user to enter what they are searching for and get anything that contains that search.
The code I wrote (that partially works) is:
`
If Me.tbIni = "" Or IsNull(Me.tbIni) Then
stCriteria = ""
Else
If InStr(1, Me.tbIni, "LIKE ") Then
stCriteria = "CURQCDB.DT_ini '" & Me.tbIni & "'"
Else
stCriteria = "CURQCDB.DT_ini = '" & Me.tbIni & "'"
Help would be much appreciated.
Just search for *MDD* instead of MDD
Try the following instead. I also took the liberty of sanitizing the input a bit so that it properly handles double and single quotes:
If Me.tbIni = "" Or IsNull(Me.tbIni) Then
stCriteria = ""
Else
stCriteria = "CURQCDB.DT_ini LIKE ""*" & Replace(Me.tbIni, """", """""") & "*"""
End If
Related
this filter statement work in other access software until I began constructing a new continuous form and now no longer works
Me.Filter = "[ItemNomen] like '*'" & "And [VendorNbr] = " & Vendor_Nbr
Me.FilterOn = True
I haven't had an issue with the statement until just recently. it seems the issue is with the "like " statement. I'm self-taught and therefore at a total loss on why it no longer works. any ideas?
You're missing a space before the And, corrected here:
Me.Filter = "[ItemNomen] like '*'" & " And [VendorNbr] = " & Vendor_Nbr
A more logical approach would be to just remove the string concatenation:
Me.Filter = "[ItemNomen] like '*' And [VendorNbr] = " & Vendor_Nbr
And, knowing that [ItemNomen] Like '*' matches anything except null values, you're better off using IS NOT NULL:
Me.Filter = "[ItemNomen] IS NOT NULL And [VendorNbr] = " & Vendor_Nbr
Trying to apply 2 filters at the same time to a subform.
Want to see records between DATES X and Y, and from BRANCH Z only.
Working fine alone, but can't use both at the same time. I know it's something
Current code:
Private Sub Command39_Click()
If IsNull(Me.txtFrom) Or IsNull(Me.txtTo) Then
MsgBox "Insert date!"
Else
With Me.frmDailyRevenue.Form
.Filter = "[DateDbl] BETWEEN " & Me.txtFromDbl & " AND " & Me.txtToDbl & "" And [F5] = " & Me.cboBranch & """
.FilterOn = True
End With
End If
End Sub
This is basically bits of code I got from the web as I'm really new to this.
So, all advice is welcome.
Try this:
.Filter = "[DateDbl] BETWEEN #" & Format(Me.txtFromDbl,"mm\/dd\/yyyy") & _
"# AND #" & Format(Me.txtToDbl,"mm\/dd\/yyyy") & "# And [F5] = '" & Me.cboBranch & "'"
I supposed that Me.cboBranch is text. If this field contains code, remove single quotes.
Also I noticed that controls you check and controls you take data from are different (Me.txtFrom and Me.txtFromDbl, Me.txtTo and Me.txtToDbl), check this.
Found the problem.
Using BETWEEN and AND for the date range was causing some conflict with the second AND to add the filter for field F5.
So I switched to use >= and <= as follows:
.Filter = "[DateDbl] >= " & Me.txtFromDbl & " AND [DateDbl] <= " & Me.txtToDbl & " AND [F5] = " & Me.cboBranch & ""
Just to clarify, for people that might come for this later, you should use # as Sergey pointed out if you have a date field, my date is in double format, so I don`t need to.
Thanks,
I have a subform, and it has a field with code source (custom VBA function doing a lookup). The filter button on that field doesn't work (Access by design). My users will want to use filters for this field and also for other fields.
As a workaround, I have added 3 checkboxes. If a user clicks any one of these checkboxes, filters get applied to the subform based on the function field.
The problem is, this removes all the other currently applied filters from the subform. This is not nice towards my users.
Is there a way to add and remove one field criteria to filtering without ruining the rest of the filter?
I have tried brute forcing it, but I gave up. When a filter gets added the normal way, many parentheses and AND words get added. My little filter text can be anywhere in a maze of filter criteria string. So using text functions to find it and manipulate it seems to be big, slow, stupid, unstable and dirty.
Am I missing something here? Any better way to do this?
Dim tx As String
If Not Me.flProcDONE And Not Me.flProcNOK And Not Me.flProcOK Then
tx = ""
Else
tx = "stProc IN (" & IIf(Me.flProcDONE, kStProcUPD, "99") _
& "," & IIf(Me.flProcNOK, kStProcNOK, "99") _
& "," & IIf(Me.flProcOK, kStProcOK, "99") & ")"
End If
With Me.sfrApply.Form
.Filter = tx
.FilterOn = True
End With
(Partly-working) brute force code:
With Me.sfrApply.Form
If .Filter = "" Then
.Filter = tx
Else
If tx = "" Then
lnStart = InStr(1, .Filter, "AND stProc IN (", vbTextCompare)
If lnStart > 0 Then
lnEnd = InStr(lnStart, .Filter, ")", vbTextCompare)
.Filter = Left(.Filter, lnStart - 1) & Mid(.Filter, lnEnd + 1)
End If
Else
lnStart = InStr(1, .Filter, "stProc", vbTextCompare)
If lnStart > 0 Then
lnEnd = InStr(lnStart, .Filter, ")", vbTextCompare)
.Filter = Left(.Filter, lnStart - 1) & tx & Mid(.Filter, lnEnd + 1)
Else
.Filter = "(" & .Filter & ") AND (" & tx & ")"
End If
End If
End If
.FilterOn = True
End With
It has a few errors, misses some parentheses. Making it work would require an additional 4-5 IFs and many more Instrs. Disgusting. Access filtering keeps adding [] and () to the filter text, that is what makes it near impossible to manipulate from the code.
A few examples of .Form.Filter texts:
"" - no filter
"stProc IN (99,99,1)" - the one I'm trying to manipulate
"([scrCarrierInvoiceGLSQuote].[ctParcelQUOTE] In (1,2))"
"((([stProc] In (99,99,1)))) AND ([scrCarrierInvoiceGLSQuote].[ctParcelSI]=1)"
"(([scrCarrierInvoiceGLSQuote].[ctParcelQUOTE] In (1,2))) AND (stProc IN (99,99,1))"
"((([scrCarrierInvoiceGLSQuote].[ctParcelQUOTE] In (1,2)) AND ([stProc] In (99,99,1)))) AND ([scrCarrierInvoiceGLSQuote].[lsError] Like "COD?")"
I would try something like
With Me.sfrApply.Form
.Filter = .Filter & " AND " & tx
.FilterOn = True
End With
This is just a quick sample but you can elaborate on that.
Well, I did manage to solve it. There was no nice way I have found. Basically:
If the filter string is empty, just add my filter string to it
If the filter part in question is not already in the filter string, just Concatenate it to the end of it (as #iDevlop suggested)
If the filter I'm about to apply is already part of the filter, just change the "IN(...)" part of it - never attempt to remove it.
Here is the code:
Dim txFullFilter As String
Dim txFilterPart As String
Dim lnStProcPos As Long 'Position of the column name in the existing filter text
Dim lnOpenParPos As Long 'Position of the opening parentheses "(" after column name
Dim lnCloseParPos As Long 'Position of the closing parentheses ")" after the opening one
'Create the actual filter text form the column I'm trying to filter from outside.
If Not Me.flProcDONE And Not Me.flProcNOK And Not Me.flProcOK Then
txFilterPart = "0,1,3,7"
Else
txFilterPart = IIf(Me.flProcDONE, kStProcUPD, "99") _
& "," & IIf(Me.flProcNOK, kStProcNOK, "99") _
& "," & IIf(Me.flProcOK, kStProcOK, "99")
End If
txFullFilter = "stProc IN (" & txFilterPart & ")"
'Apply said filter to the subform
With Me.sfrApply.Form
If .Filter = "" Then
.Filter = txFullFilter
ElseIf InStr(.Filter, "stProc") > 0 Then
lnStProcPos = InStr(.Filter, "stProc")
lnOpenParPos = InStr(lnStProcPos, .Filter, "(")
lnCloseParPos = InStr(lnOpenParPos, .Filter, ")")
.Filter = Left(.Filter, lnOpenParPos) & txFilterPart & Mid(.Filter, lnCloseParPos)
Else
.Filter = .Filter & "AND " & txFullFilter
End If
.FilterOn = True
End With
I'm using VBA for Access. I have some forms that I want filter.
With the follow code:
a = "ID =" & idwanted
Form_Form1.Form.Filter = a
Form_Form1.Form.FilterOn = True
It works nice, the form is correctly filtred. But I need to filter by 2 fields, something like that:
a = "ID =" & idwanted
b = "Name =" & namewanted
Form_Form1.Form.Filter = a And b
Form_Form1.Form.FilterOn = True
But it give me a error (non match types), but all the fields and variables (a & b) are integer.
(If I do only with b, it filter it correctly.)
Thanks for read me!
You can just make sure you use the right data type literal. Also you need to concatenate the two conditions. Finally Name is a reserved word, so you need to enclose them in []. Something like,
a = "ID =" & idwanted
b = "[Name] = '" & namewanted & "'"
Form_Form1.Form.Filter = a & " And " & b
Form_Form1.Form.FilterOn = True
I need to let users filter a continuous form using values the user enters into a textbox. And the continuous form is also nested within a couple levels of navigation subforms. This sounds easy enough, but all the examples I find on the web use macros instead of vba.
I set up the structure and wrote an AfterUpdate procedure for a textbox txtFilter as follows:
Private Sub txtFilter_AfterUpdate()
Dim filterval As String
filterval = txtFilter.Value
With Forms!Main!NavigationSubform.Form!NavigationSubform.Form
.Filter = "LastName Like " & filterval
.FilterOn = True
End With
End Sub
I have played with different syntax, but none of it seems to work properly. Here is a link to download the relevant parts of the database from a file sharing site: http://jmp.sh/v/HGctZ4Ru74vDAjzN43Wq
Can anyone show me how to alter this so that users can use the textbox to filter the continuous form?
I got it to work using this: .Filter = "LastName Like """ & filterval & """"
Need those annoying String Identifiers even for strings sometimes.
Okay, To get the form to open with no records and then pull up just the records you (or the user) specifies is easiest with a bit of re-work.
(I'd recommend you working with a copy and not your original)
1:On your Continuous Form, remove the Recordsource; we're going to use Late Binding (Kinda)
2:Then delete the code under the txtFilter box, then delete the box itself.
3:Add a comboBox with something like this as the recordsource:
SELECT DISTINCT myTable.LastName FROM myTable ORDER BY myTable.LastName; (This will get you a unique list of last names so knowing how to spell the name will not be necessary, plus it assures at least one match)
4:In the After Update event of that combobox, add code like this:
Dim strSource As String
strSource = "SELECT mt.IntakeNumber, mt.ClientNumber, " & _
"mt.LastName, mt.FirstName, mt.ConsultationDate " & _
" FROM myTable mt " & _
"WHERE (mt.LastName)= '" & Me.cboFilter.Value & "'"
Me.RecordSource = strSource
Me.Requery
Obviously you'll need to change the table and field names as necessary, but hopefully you get the idea.
Option Compare Database
Option Explicit '<- always include this!!!!!
Private Sub txtFilter_AfterUpdate()
Dim strFilter As String
' only set Filter when text box contains something
' to search for ==> don't filter Null, empty string,
' or spaces
If Len(Trim(Me.txtFilter.Value & vbNullString)) > 0 Then
strFilter = "LastName Like '*" & _
Replace(Me.txtFilter.Value, "'", "''") & _
"*'"
' that Replace() prevents the procedure from breaking
' when the user enters a name with an apostrophe
' into the text box (O'Malley)
Debug.Print strFilter ' see what we built, Ctrl+g
Me.Filter = strFilter
Me.FilterOn = True
Else
' what should happen here?
' maybe just switch off the filter ...
Me.FilterOn = False
End If
End Sub