Set up a parameter with multiple criteria in a query? - ms-access

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)

Related

MS Access 365, Using DLookup to look up info on a non combo box

helping my dad program a temp database in the meantime for our HVAC business (we used to use paradox but that mess has finally stopped working).
What he is looking for is the ability to type in a phone number and pull up the customer's name (First name, Last name) and Cust ID (Our identifying value for the customers). We have a lot of phone numbers so a combo box displaying them all would not be ideal. Any ideas?
What I have currently is 5 boxes that have
=DLookup("CustID","CustT","Phone=" & PhoneLookupF)
But they don't seem to update when I change the value on the PhoneLookupF box by typing stuff in.
If object (field, table, query, form, report) names have spaces, then enclose in [ ]. Advise not to use spaces nor punctuation/special characters (underscore only exception) in naming convention. If Phone is a text field, need apostrophe delimiters, if not text then remove apostrophes from example.
=DLookup("[First name]", "CustT", "Phone='" & [PhoneLookupF] & "'")
=DLookup("[Last name]", "CustT", "Phone='" & [PhoneLookupF] & "'")
=DLookup("[Cust ID]", "CustT", "Phone='" & [PhoneLookupF] & "'")
This will not recalc until PhoneLookupF loses focus and therefore commits the entered value.
Might find the following tutorials interesting:
http://allenbrowne.com/ser-32.html
http://allenbrowne.com/AppFindAsUType.html

How to evaluate an expression in Access like evaluating a formula in excel

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).

Best practice for working with long texts in ms-access

I would be very thankful if somebody resolves my problem.
I'm new in working with Ms Access and I still gain experience on its basic functionality.
I have a table MyItems. 2 of its fields are: ItemCode and ItemName. ItemName is a very long text (Memo type). I have also a query and a form with many fields. The form's record source also consists of many fields. All these things (associated with 1 field) have the same or similar names so I can't differentiate them quite well.
What I want is when I set the value of ItemCode (in a not bound Combobox or Listbox with name ItemCode) the value of ItemName to be displayed in a control - maybe TextBox.
I can display its value in a ListBox (by sql query in its row source), I have no problems with this, I have no problems with managing events, but the text is very long and is cut. I understood that unfortunately ListBoxes don't have multiline property. So maybe the most appropriate control to deal with is a TextBox. And maybe the most appropriate way to display the value is using DLookUp function in the TextBox's control source. But in this sea of items with similar or the same names I just can't deal with its syntax, I was trying again and again for a very long time. So I have 2 questions:
Are the TextBox control and DLookUp function in its control source the best way to extract long texts from a table without binding or there are more suitable controls (which directly work with sql query)?
What is the right syntax of DLookUp? - where exactly are there ' ', " ", [ ], .Value, =, &, where must I write the path to the table or the form and where it would be mistake? If I just write [ItemCode] what it would be associated with - the form record source, the table, the form control or anything else? I would be grateful if someone writes the correct syntax for my case or if he shares a link with plenty of examples for using DLookUp. Those that I found didn't satisfy me.
Either a bound control, or an unbound one. If unbound, you need to load the text with VBA code or with DLookup in the control source. There are no other options.
Personally I'd rather use the AfterUpdate event of ItemCode, and call DLookup there, but that's a matter of preference.
2.
It's not that complicated. You basically have the SELECT, FROM, WHERE parts of an SQL query in the 3 arguments. [] are needed for all identifiers containing spaces or other special characters, and when refering to form controls.
=DLookup("ItemName", "[my Table]", "ItemCode = '" & [ItemCode] & "'")
The single quotes '' are needed if ItemCode is text, not when it is a number.
You could also use doubled (escaped) double quotes, but that is much less readable.
=DLookup("ItemName", "[my Table]", "ItemCode = """ & [ItemCode] & """")
Now where does [ItemCode] come from?
Access first looks for a control on the form with the name ItemCode.
If there isn't one, it looks for a field ItemCode in the form's RecordSource.
These are the only ways [ItemCode] can be evaluated. To avoid confusion, it is recommended to name bound controls with the same name as their source field.
The syntax above is only valid if everything is on the same form. If [ItemCode] is on a different form, or you refer to it from a query, you use
=DLookup("ItemName", "[my Table]", "ItemCode = '" & Forms![my Form]![ItemCode] & "'")
For more complicated cases with subforms, see Refer to Form and Subform properties and controls
And to use it in VBA (in ItemCode_AfterUpdate):
Me!ItemName = DLookup("ItemName", "[my Table]", "ItemCode = '" & Me![ItemCode] & "'")

Access search form - search a delimited string

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

Search Function Using Queries and Controls

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.