I need to make a search box that could reach and take any field, since I know about nothing of VBA, I did it by using queries...
I have a query with this OR criteria in each field (properly done in there):
Like "*" & [txtbox1] & "*" Or Like "*" & [txtbox2] & "*"
Problem is:
it's not working as intended...
If I type nothing on either box1 or box2, it shows all... even when, say, I type something I know is on one field and in the other something from another field then it shows still a lot it shouldn't show...
I wanted it to filter by the first, and then that results by the other one... only show result that meet both criteria (when I type in both txtboxes)
if it needs to match both textbox1 and textbox2 you should use an AND instead of the OR
Like "*" & [txtbox1] & "*" And Like "*" & [txtbox2] & "*"
Ok, based on your comment I'm guessing you are using the query designer, I'm not sure how this can be done using the desginer, this can be done using SQL so a basic query
SELECT *
FROM TABLE
WHERE (field1 like "*" & [txtbox1] & "*" OR field2 like "*" & [txtbox1] & "*" ....for all the required columns)
AND (field1 like "*" & [txtbox2] & "*" OR field2 like "*" & [txtbox2] & "*" ....for all the required columns)
Related
Is there a way to evaluate an expression in Access like we can evaluate formulae in Excel? My Dlookup keeps resulting in the number two and I can't figure out where the hang up is.
=Nz(DLookUp("[RemanChangePoint]![ID]","[RemanChangePoint]","[NewPartNum] Like '*" & [LegacyPN1] & "*' Or [NewPartNum] Like '*" & [LegacyPN2] & "*'"),"")
There's 10 more LegacyPNs and I am expecting to get either the ID of the record that has the LegacyPN or a blank. Before, instead of doing it as above, I was doing:
Like & Chr(34) & "*" & [LegacyPN#] & "*" & Chr(34) & " Or..."
It would result in 2 every time. But now with the shortened version, it's resulting in blanks every time, even though there are records with the part number.
I have a similar expression on a different form, but only one LegacyPN to use as the lookup so it works. I assume that the problem is the numerous Or statements, but it doesn't make sense to me why it's not working, thus the desire for an expression evaluator like Excel has.
Or (and this may be a little uncouth to ask a slightly different question)
Is there a way to use an attachment's file name as the criteria for Dlookup? That may prove more effective than going by LegacyPN, I just can't figure it out via the expression builder.
Your first DLookup where clause is referring to fields on the current form since the double quotes cause the expression to break out of the where clause and into the scope of the ControlSource.
This means something like the following will be passed into the DLookup function because the [LegacyPN] portions of the expression are evaluated in the context of the form, before DLookup is called:
[NewPartNum] Like '*1*' Or [NewPartNum] Like '*2*'
...where 1 and 2 are values of [LegacyPN1] and [LegacyPN2] on the current form.
If you want the [LegacyPN] fields in the DLookup where clause to refer to the [RemanChangePoint] table being queried by the DLookup, then you need to fix your quotes:
=Nz(DLookUp("[RemanChangePoint]![ID]","[RemanChangePoint]", "[NewPartNum] Like '*' & [LegacyPN1] & '*' Or [NewPartNum] Like '*' & [LegacyPN2] & '*'"),"")
Your second syntax should have also worked because the Chr(34) added a double quote resulting in this final string being passed to the DLookup function in the where clause:
Like & "*" & [LegacyPN] & "*" & " Or..."
In which the [LegacyPN] field refers to the table within the DLookup query, not the current form. So I'm not sure why that didn't work. Note: single quotes or double quotes work okay in this context but not a mixture.
Double quotes get tricky within arguments of functions within a CountrolSource property (and other places).
I know this is probably a simple thing I'm missing, but I'm hoping you all can help. I'm trying to add a textbox to my form that allows users to type in search criteria and have the query filter the records to only display those that qualify. The trick is I want the user to be able to type in info and have it check all the fields of the form and return records for any that are valid.
I set up a query with the fields that I want checked and I watched a few tutorials on setting criteria, but they are all working with multiple search bars. Is there a way to do it with only 1?
Like "*" Or [Forms]![Publications Page]![FilterBox] OR "*"
This is the criteria expression I wrote. It returns records just not the ones I want and doesn't seem to change after I change what is in [FilterBox]. I have 4 fields I'm running this same criteria on. All thoughts and suggestions are greatly appreciated!
Thanks!
The resulting criteria should be something like
[LastName] Like '*searchtext*' Or [FirstName] Like '*searchtext*' Or ...
So, you would have to set up a single criteria like this
Dim crit As String
crit = " Like '*" & Replace(Me!FilterBox, "'", "''") & "*' "
where the replace statement replces single (') by 2. This enables the user to enter an apostrophe in the search box.
Now you must create the whole criteria
crit = "[Field1]" & crit & "Or [Field2]" & crit & "Or [Field3]" & crit & "Or [Field4]" & crit
I am trying to set up a parameter in a query which will ask the user for two different letters, and will then display all records that have info which starts with either of those letters typed in by the user. What code would I put in the criteria part to accomplish this? Thanks
Like "[" & [Enter 2 letters] & "]*"
The user would enter, for example, ad or da. They could enter more than 2 letters.
If you want specifically 2 letters, or just more control, then you'll need to use VBA, and perhaps a TextBox on a Form, rather than a simple parameter-query.
As you want two dialogs (parameter boxes) you can use:
Like [First letter] & "*" Or Like [Second letter] & "*"
Again, they can enter more than a single letter in each box - which I consider a useful feature. You could restrict it to a single letter each with:
Like Left([First letter],1) & "*" Or Like Left([Second letter],1) & "*"
If they don't enter anything into the boxes then it will show all records. As mentioned, VBA would be needed to control the criteria more precisely.
If you really wanted to restrict to a single letter each then you can use:
Like IIf(Len([First letter])=1,[First letter] & "*",False) Or Like IIf(Len([Second letter])=1,[Second letter] & "*",False)
I have created a simple search form with the ability to search for an individual Box reference number. The output is a report with the box number (or list of box numbers when the search returns multiple matches). For example searching for ABC111, returns a report like:
Box Description
ABC1110 Stuff
ABC1114 More stuff
ABC1119 Even more stuff
I use the following Criteria in my Search_Query
Like "*" & [forms]![Search_form]![Boxref] & "*"
But my customer wants to paste a list of boxes in the BOX Ref field like:
ABC1110, ADF1234, AGT2112
...and have the report display like this:
Box Description
ABC1110 Stuff
ADF1234 Cool stuff
AGT2112 More cool stuff
What criteria command do I need to write to achieve this?
You can use it this way
IN ("*ABC1110*","*ADF1234*","*AGT2112*")
or if you want you can use the textboxes of the search form
Criteria ="In ("
with [forms]![Search_form]
Criteria = Criteria & "*" & ![Boxref1] & "*"
Criteria = Criteria & ",*" & ![Boxref2] & "*"
Criteria = Criteria & ",*" & ![Boxref3] & "*"
......
end with
Criteria = Criteria & ")"
Or even write a loop to do it
Use Regular Expressions in your search criteria , go through below links you will get some idea
http://timothychenallen.blogspot.in/2006/05/ms-access-vba-regular-expressions-regex.html
http://bricestacey.com/2010/07/09/Regular-Expressions-in-MS-Access.html
I am trying to utilize a search function on a form. I based the form on a query that is a copy of the table, except the criteria are linked to a control on the form. Ex.
WHERE (((tblFamily.FamilyName) Like "*" & [Forms]![frmFamily]![cntrlFamilyName] & "*")
I want to do this in other fields such as address, city, etc. as well. However, if I apply the same logic to the address field, blank records are ignored and never returned, even if nothing was put into the control.
How do I fix it so that when nothing is put in to the cntrlAddress the search does not ignore records with blank addresses.
You can append an empty string to your field and search on that:
WHERE tblFamily.FamilyName & ""
Like "*" & [Forms]![frmFamily]![cntrlFamilyName] & "*"
This will mean that tblFamily.FamilyName will not be null and when [Forms]![frmFamily]![cntrlFamilyName] is empty, the query will read:
Where "" Like "*"
Where "bob" Like "*"
And so on.