Access Query Parameter Filtering Form - ms-access

I am filtering a form named sfrWorklistFilter from a combo box named cboOpeningType. The recordsource is from an embedded query on the form. If I make a selection from the combo box the filter works fine with the following code:
Forms![sfrWorklistFilter]![cboOpeningType]
However I need to return all records when no selection is made in which case I use the following code:
Like Forms![sfrWorklistFilter]![cboOpeningType] & "*"
The filter then does not give exact matches, but all records that begin with the letter on the combo box.
I need exact matches for the record or if no selection is made all records.
Any suggestions?

EDIT remove double quotes
This should work - and you can do same thing with your other field/combobox searches
Like IIf([Forms]![sfrWorklistFilter]![cboOpeningType]<>"",[Forms]![sfrWorklistFilter]![cboOpeningType],"*")

Related

SQL Select Statement Column Selection Based on Checkbox TickMark Access

I would like to select specific columns based on the user checkbox selection.
I can able to achieve it using VBA but is it possible to arrive the fields in SQL itself based on forms checkbox tick status?
Two ideas:
1) create a text box that collects the values of the checkboxes:
= if(checkbox1,"Col1, ","") & if(checkbox2,"Col2, ","") & ...
Create a second one that removes the tailing comma.
Use the content of this second text box to build your SQL string.
2) Solution 1 requires you to hard code the columns in one formula. A more generic way would be to populate a list with the column names of your data source (the table). The changed event of the list would then generate the list of column names for your SQL string.
This solution involves VBA, yes but it's interactive. Guess that's what you're after.

Populating listbox from another listbox selection

I am pretty much a newbie to using VBA in Access and I'm having trouble with something that seems like it should be quite simple.
I have two listboxes (called LB1_ID and LB2_ID) on my form (MainForm) that I want to list related IDs from their respective Row Sources. I need LB2 to be populated based on the selection in LB1. They both have Row Sources from the same Table (Table1) and it is a many to many relationship of Requirement IDs ("Req ID1" and "Req ID2"). My current form, which is not working, has the Row Source of LB1 as:
SELECT Table1.ID, Table1.[Req ID1] FROM Table1 ORDER BY Table1.ID;
and the Row Source of LB2 as:
SELECT Table1.ID, Table1.[Req ID2] FROM Table1 WHERE ([Forms]![MainForm]![LB1_ID]=Table1.[Req ID1]);
When I make a selection in LB1, nothing happens in LB2. The column widths are formatted correctly and I can get it to work if I use Me.[Forms]![MainForm]![LB1_ID] but I have to type out the LB1 selection manually in a popup box if I use that.
What am I missing?
If your listbox is multi-select, you cannot use a simple form reference as query criteria. If it is not multi-select, keep in mind that its value may be a hidden column (usually an ID field), so there are two possible issues and solutions:
Possible Issues:
Single-Select listbox has an ID field that is hidden (column width = 0") and you are matching it to the wrong field in your table. To check the output of the listbox, open the VBE and type ?[Forms]![MainForm]![LB1_ID] into the immediate window and press enter when your form is open in form view and a row is selected in LB1_ID. If the returned line is what you expect, then the problem must be elsewhere.
Multi-Select listbox property is enabled. In this case, your query will not work, because the listbox will only return Null. You will need to write some VBA to loop through the rows and figure out which ones are selected, which is a bit of a pain. Ultimately you'll build some code that will alter your query with the specific criteria for each selected row. Instead of explaining here, take a look at this article for a tutorial.
The .Requery method is still important to put in the AfterUpdate event of your first listbox to refresh the second.
Your query seems to work, but you need to refresh your listbox2 whenever you make selection into listbox1, so if both listbox are in the same form add this event handler :
Private sub LB1_ID_Change()
Me.LB2_ID.Requery
End sub
Without this, your listbox2 will only get populated once on load based on the initial value of listbox1.
Also, if you have not already done it, I would recommend to add your listbox1 control as a parameter into your listbox2 query (in query builder, right click -> parameters).

Multiple Values for a field (Link Master fields)

I am trying to use Link Master Fields to get a value from my form in order to filter some of my data and then show the remaining on my chart. Everything works perfectly fine but when I want to select more than one value (using a listbox to select multiple values for a field), my chart returns nothing.
I would like to know how can I address multiple values for a field in my chart so it filters the values on my chart as an "Or" function in SQL.
Imagine I want to have an SQL with: WHERE [MyTable].[Field1]= Selected values in listbox.
It seems that my SQL selects all the values in the listbox as One Value/And function.
You will need to do this via VBA.
Use the Change event on the list box to update the subforms recordsource.
Also make sure you execute the code in the forms Current event, so that it will display correctly when moving records, and on initial opening.
So with a bit of research and trying out different ways, an easy way is to use some hidden textboxes in the form and loop through the selected values and give each textbox one of the selected values in the listbox.
From there you can write a SQL for your chart specifying the condition so your field = Condition 1 or field = Condition 2.

How to enable filtering on a field defined by DLOOKUP()?

I have a form TForm based on table T, set up as a datasheet. My goal is to add a filterable column to the datasheet where the column's value is calculated from a query using another column's value.
I tried to do this by adding a text box currentBox to T. The control source for currentBox is:
=DLookUp("name","currentStatus","itemID=" & [ID])
where [ID] is a field in T and currentStatus is an aggregate query on a table that T is related to.
I can filter on all the fields in TForm that are in T. But I can't filter on currentBox, even though it also appears as a column in the form; clicking on the column header doesn't do anything.
I'm guessing the problem is that currentBox is not bound to a field in T; is there a way to work around this?
Here's a VBA solution:
Add a combo box (aka drop-down) object to your form header. This drop-down's source will be an independent query that displays all the values your Dlookup() currently pulls (names?) and stores the itemID. Let's call it ObjPickName in this example.
Add an AfterUpdate event to ObjPickName that will filter your form for you (your form will still be based on T). The code will be something like:
Private Sub Combo_ObjPickName_AfterUpdate()
Me.Form.Filter="[itemID]='" & Me.Combo_ObjPickName.Value & "'"
Me.Form.Filteron=True
End Sub
The way that I ended up solving this was to add a field to T, and have that field updated during the AfterUpdate() event with the value from the DLookup() call. Because the field is now no longer query-based, it can be used to filter the form.

How to use a query as a source for a report in MS Access 2007?

I did the following in MS Access: I made a form which had a combo box and a button. You select an option from there and click on the button and it is supposed to open a report. Now, I wrote a query selecting a few fields from a table and in the where clause, gave the condition as where name=str(combo1.value) and the report source was specified as this query. Now, when I select the value and click on the button, it opens a blank report. How can I make it load only those particular values?
I am not saving the combo box value anywhere. It said that it would remember the value for later use. Am I doing the right thing by not saving it? What should I do to make this work? Please help me!
Edit: The combo box is using values from a column 1 in a table X. I've not bound the value to any field and am using the "Remember the value for later use" option provided. The combo box is essentially a list of hotels and the report is a list of people staying at the selected hotel. When I put the ID of the field (as defined in the X), it works. But the thing is, it should refer to the names of the hotels and not the ID, which I am supposed to enter in a popup that asks for it. What do I do?
Edit 2: The query is as follows:
SELECT Table_1.Hotel_Name, Table_2.Name_of_Delegate, Table_2.Address, Table_2.City, Table_2.Center, Table_2.Spouse_Present, Table_2.No_of_Children, Table_2.No_of_Guests, Table_2.No_of_Rooms
FROM Table_1 INNER JOIN Table_2 ON Table_1.ID=Table_2.Hotel_of_Residence
WHERE Table_1.Hotel_Name=FormName.Combo7.Text;
When I click on the button (which opens the report), it asks for the name of the hotel in a popup box. How can I avoid this? What I am doing wrong?
You can use a WhereCondition with the DoCmd.OpenReport Method as a "dynamic WHERE clause" for your report's record source. Use something like this in the click event of the command button which opens your report.
DoCmd.OpenReport "YourReport", , , "[name]=" & Me.combo1
Remove the WHERE clause you added, where name=str(combo1.value), from the report's query.
I surrounded name with square brackets because name is a reserved word. See Problem names and reserved words in Access
Edit: In a comment, you said this about the combo box:
"Row Source is SELECT [Table_Name].[ID], [Table_Name].[Name] FROM [Table_Name];. Bound Column is 1 (which I assume shows the names I wish to be displayed in the combobox.)"
When you refer to the value of a combo box, that value is the value of the "Bound Column". So in your case, the bound column is 1, which means the combo value will be [Table_Name].[ID]. However, you want to open your report based on the [Name] column of the combo. So change the bound column of the combo from 1 to 2.
To open a report using the value of your combobox, in your report query you need to do the following:
SELECT fields
FROM table
WHERE YourValue = [Form]![FormName]![ComboBox Value]
You have to specify the name of the Form, plus the value so the report query knows the value. Hope this helps.