Add "All" option to ComboBox used to filter for report in MS Access - ms-access

I'm trying to follow Microsoft's example on how to add an "All" option to a ComboBox in Microsoft Access, but their article does not do an adequate job of providing guidance, aside from specifying the code.
What I'm trying to do is build a form that allows a user to select an option from a ComboBox (the options are generated from records in a table), and then build a report filtered based on the user's selected option. The ComboBox consists of 2 columns: the primary key/ID of the records and their displayable names.
I can't understand the VBA code Microsoft provides enough to figure out what is going on, but I would like the "All" option in my ComboBox to either have a blank primary key/ID, or one that = 0. That isn't the case, as selecting the "All" option when using the form results in the error message "The value you entered isn't valid for this field". This leads me to believe that the "All" text is getting filled into the primary key/ID column instead of the display column. The example instructs me to assign the display column number as the "Tag" property of the ComboBox - and in this case, my display column number is 2. However, this (and pretty much any other value I add) results in the aforementioned error message.
Any idea if Microsoft's example is even applicable to my case, or do I need to adjust their code somehow?

Check the Control Source property of your combo box. Sounds like it may be bound to a field in the form's record source. If you make it an unbound control (nothing in the Control Source property) you should be able to select any item from the combo's Row Source without Access complaining at you.
Say your combo's Row Source is a query like this:
SELECT id, disp_name
FROM YourTable
ORDER BY disp_name;
You can add an "all" row with a UNION query:
SELECT id, disp_name
FROM YourTable
UNION ALL
SELECT TOP 1 0, "**ALL**"
FROM AnyTable
ORDER BY disp_name;
AnyTable can be just that. If you happen to have a table which contains only a single row, use that one ... and you wouldn't even need the TOP 1 part. Just try not to use some ReallyBigTable as AnyTable.
Edit: Actually some ReallyBigTable would be fine if it has a primary key or other unique field which you can use in a WHERE clause to retrieve a single row:
SELECT id, disp_name
FROM YourTable
UNION ALL
SELECT 0, "**ALL**"
FROM ReallyBigTable
WHERE pk_field = 1
ORDER BY disp_name;
UNION ALL will return all combined rows. If you have any duplicate rows, you can thin them out by using just UNION instead of UNION ALL.

Related

SSRS Multiple Parameters in a single dropdown

I have a report I need to develop where all Quotes should be listed where a specific Product is present. The product can be identified based on Product ID/Price/Product Name etc. I was looking to develop one report where all there fields are present as part of a dropdown. When the user selects one value such as Product ID and enters a value in the text box next to the ID, the report is filtered on this parameter. If the user selects name, the name can be entered in the text box and the report is filtered based on the Vendor ID etc.
Is there a way to do this? I have the parameters showing up next to each other instead.
It is doable if I understand correctly and here are the steps for achieving your report purpose.
I will use a simple query as an example data set for your reference.
1.Create source and datasets, in this example I skip the data sources and here is the short query for my exmaple
SELECT * FROM Table
WHERE
(ID = #ID or #ID = '')
and
(Name = #Name or #Name = '')
2.Add another dataset named Control, the query for Control is:
SELECT 'ID' as option
UNION
SELECT 'Name' as option
The purpose of creating this dataset is to provide the available values later when you need to choose either name or ID.
3.After step1, you should already have two parameters generated by system, which are ID and Name, if not, create them by yourself and go to each parameter page, DO MAKE SURE CHECK Allow blank value''
4.Create the 3rd parameter called Control, go to parameter setting page, go to Available Values, Choose Control dataset, Choose option (which is the customize column name you set in Controldataset) for both Value field and Label field
5.Go to ID parameter setting page, go to Default Values, choose Specify values, click fx, type this:
=Switch(Parameters!Control.Value="Name","")
Click ok
6.Go to Name parameter setting page, go to Default Values, choose Specify values, click fx, type this:
=Switch(Parameters!Control.Value="ID","")
Click ok
The ID and Name expression are correct, not set wrong
7.Change the parameter order to make Control on the VERY TOP, no matter ID or Name comes for the 2nd, but Control should be in the 1st place.
8.Drag and drop a table, place Name and ID. Preview the report, at this time, only Control will be available for you to choose value, choose either ID or Name to see the difference. When you choose ID, Name will be gray out, only ID light up and type any ID you want, actually, what the system does internally is passing the "" (blank value) to Name, that is why you need to make sure the Name parameter could accept blank value. Same for selecting Name
Update me if you have any run time issue

Parameter query doesn't return the value of an auto number control

I have a form linked to a table. The form has 4 text boxes: one linked to the autonumber field, and the other three to text fields.
There is also a subform, from which I wish to launch a query (via button and macro) combining results from the subform and a control on the main form. When I specify any of the three text-based controls in a parameter query, this works fine, but asking for the value of the first (autonumber) control results in a symbol being displayed instead of a value.
I wasn't sure what specific information/images would be helpful. Please ask for specific information if you feel it would help.
I've been given the answer elsewhere. I had to implicitly convert the results of the batch field into an int.
INSERT INTO heat_treat_jobs ( card_id, batch ) SELECT atheattreat.id, CInt([Forms]![heat_treat_loads]![batch]) AS Expr1 FROM atheattreat WHERE (((atheattreat.index)=[Forms]![heat_treat_loads]![atheattreat subform].[Form]![index]));

Control Column Visibility using DataSet values

My client wants to be able to show/hide columns in a report based on a Parameter (ReportType). The report columns are static, they will just be controlling which ones are hidden/shown which creates a different "View" of it. Currently there are only 2 views, but we are about to add several more.
At the moment column visibility is controlled by simple expression: =Parameters!ReportType.Value = "SOMEVALUE"
and only 1 report type hides columns so this expression is fine.
Now we're moving into a situation where a column may be hidden in multiple ReportTypes and i want to avoid getting into: IF report type = VAL1 or VAL2 or VAL3 THEN HIDE as it will make it very hard to see for any given ReportType what columns are meant to be shown (as all the logic is in each column visibility expression)
I found a post and used the basic elements of it:
http://sql-bi-dev.blogspot.co.uk/2010/10/displaying-dynamic-columns-in-ssrs.html
What im trying to do is define a Dataset, something like:
SELECT * FROM
(
select 'Rpt Type1' ReportType, 'Units' ColumnName UNION
select 'Rpt Type1' ReportType, 'Price' ColumnName UNION
select 'Rpt Type2' ReportType, 'Units' ColumnName
) ReportColumns
WHERE ReportType = #ReportType
And then in the Column Visibility expression check to see if the column name exists in the dataset. This way visible columns for a report type are defined in a single place and are easy to manage/maintain.
In the post i linked, hes getting the user to select the Column names in a parameter. I want to lookup the Names in my dataset based off the ReportType param thats selected.
Im stuck on getting the column names into a parameter and then using that in the column visibility expression. Any help would be much apreciated :)
Doh, i was pretty much there.
I created a parameter and set its default values to "Get From Query" and selected my dataset and ColumnName field... job done!
Then i was just writing visibility expressions for each column using the code in the article i was following.

How can I display a *foreign* field value in a text box?

How do I bind a text box with a field, which doesn't belong to form's "Record Source" table, through the Design View?
Example: I have "Order.cust_id" (Record Source=Order) and I want to display "Customers.name". I believe it is trivial but I have no experience with MS Access. I tried to use the text box "Control Source" property but no luck.
One method would be to convert the text box to a combo box. Then set the row source to include both the cust_Id and the Customer.Name from the customer table. SQL statement example
Select Cust_ID, Name From Customer
Order By Name;
By setting the number of columns to 2 and the column widths; the first column as zero (i.e. "0;6") then the foreign key would be hidden from the user and the customer name would be displayed.
Note this method does force you to have limit to list set to true.
Also you do end up with a drop down list which may not be what you want.
You can use DlookUp as the control source of a textbox:
=DlookUp("[Name]", "Customer", "ID=" & Cust_ID)
Syntax: What to look up, table name, where statement
The Where statement should follow the rules for Jet SQL, which means that you must use delimiters if the field is text or date format.
Note that Name is a very bad name indeed for anything. I suggest you rename the field immediately before things get worse.
It can be useful to know the error(s).
You could create a new View (e.g. OrdersAndCustomerNames), select all the columns you want to use in the form, then instead of using the Order table as Record Source, you would just switch to OrdersAndCustomerNames. You say you have no experience with MS Access, so I am guessing you are not building anything huge and overly complicated, so I would do it this way. I am quite sure it can be done more elegantly but this will do for now.

MS Access Dropdown List/Combo Box

This should probably be pretty simple but my Google-Fu is as yet unable to find an answer. I simply want to create a dropdown list in Access so that upon selection I can perform some action based on the value of the selection. For instance, I have a list of people and I would like to populate the combo box so that their names appear in the list but the "value" is set to their ID (the primary key).
It sounds like you might be asking how to display something in the dropdown other than the ID while keeping the ID as the returned data from the dropdown. If that's the case set the Bound Column to the ID field (usually 1) and (assuming the name field is next) set the Column Count to be 2 and the Column Widths to be 0";1" or 0";[whatever width you need].
You will need to hook into the onchange event for the dropdown list.
and from MSDN
How have you set the properties for your combo box?
Perhaps you could try setting (assuming you are pulling data from Table1 with fields ID and Field1
Row Source: SELECT [Table1].[ID], [Table1].[Field1] FROM Table1;
Row Source Type: Table/Query
Bound Column: 1
Column Count: 2
Column Widths: 0", 1"
and then hook into the onchange event as Chris Ballance suggests. The value property of the combo box is ID; the text will be what is in Field1.
OK, I figured it out even though it was a bit counter-intuitive. An Access Combobox can have as many values as you want (instead of just one key on value). By default all of the values are are shown in the list so you need to hide certain columns by setting their widths to 0. That is done via the ColumnsWidths property in the property pane. ColumnWidths takes a comma separated list of values which corresponds to the order of the columns in the list. I hope this helps someone.