How to simulate a Group Header in an Access Continuous Form? - ms-access

I have a form (not a report) in Access displayed in Continuous View that has alot of repeat data. i.e. it looks like:
State Names
FL Abe
FL Bart
FL Charlie
GA Deleanor
GA Mary
This needs to be interactive (there's some command buttons on each row as well as in the form header), but there's alot of repetitive data. Is there a way to add a group header by state? i.e. make it look like:
State Names
FL
Abe
Bart
Charlie
GA
Deleanor
Mary

I found a number of problems with the solutions posted. Most obviously there are issues (as noted) if there are duplicate entries. But I found there was a deeper issue which is that you can't do this and still sort the data on yet another field (suppose you wanted to sort by the date by which each name was first registered in each state, and yet NOT rank them that way).
A better solution is found in the Using Code section of the Microsoft KB:
http://support.microsoft.com/kb/q101081
The code from that section retrieves the actual data from the Previous field, in whatever sort order you define.
Computationally this can be a bit intense, so it works only for small datasets, but I achieved good results by first testing the function:
=PrevRecVal([Forms]![myForm],"ID",[ID],"Names")=[Names])
then putting it inside the IIF function:
=IIf(PrevRecVal([Forms]![myForm],"ID",[ID],"Names")=[Names],"",[FullName])

Have you looked at subdatasheets? If you have a state table (or just query your existing data for the unique states), you could use that in a non-editable parent datasheet, and then the subdatasheet would display the people data, and you could make it editable.
I'd implement this with datasheet forms, not with the actual table or query datasheets. This gives you a lot more control over the subdatasheet linking and formatting, as well as making interacting with the datasheet events easier (it can be done with a table or query datasheet, but only through Screen.ActiveDatasheet.

The closest I can get is to add a field to your form's record source which numbers each row within a group. Then you could add a text box in the detail section of your form with this expression as its control source.
=IIf(rank = 1, [State], ""]
The text box would display the first occurrence of each state, and a zero length string otherwise.
I tested with this query for the record source.
SELECT
m1.state,
m1.names,
(
SELECT Count(names)
FROM MyTable AS m2
WHERE
m2.state=m1.state
AND m2.names<=m1.names
) AS rank
FROM MyTable AS m1
ORDER BY m1.state, m1.names;
And it worked, but is not editable. If you need to be able to edit, try a query which uses DCount to generate the rank.
SELECT
m1.state,
m1.names,
DCount("names", "MyTable",
"state ='" & state & "' AND " & "names <= '" & names & "'") AS rank
FROM MyTable AS m1
ORDER BY m1.state, m1.names;
Both those queries give this result set with your sample data:
state names rank
FL Abe 1
FL Bart 2
FL Charlie 3
GA Deleanor 1
GA Mary 2
Note, I assumed unique combinations of state and name.

Related

Populate two rows of a combobox with a query Access 2007

I'm working on an Access 2007 database and I'm having a problem with a query.
I have a table named Vehicles, which contains data such as ID, license plate and fuel type for each vehicle in it.
I'm trying to make a query which will populate a combobox in a Form with each vehicle's fuel type, based on the license plate chosen by the user beforehand.
The thing is, we have some some cars that work with two types of fuel and I cant' find a way to display them separately in the combobox.
So far it kinda works, code follows:
CheckDiesel: IIf([Diesel]="Yes";"Diesel";IIf([Gasoline] AND [Ethanol]="Yes";"Gasoline"+ "Ethanol";IIf([Ethanol]="Yes";"Ethanol";IIf([Gasoline]="Yes";"Gasoline";""))))
If you look at the second IIf, I have a condition for a bi-fuel car. I want to display Gasoline and Ethanol separately, each one in a row.
I've tried using "&Chr(10) Chr(13)&" and "\r\n" but I had no success so far.
Can anyone help me?
Storing multiple pieces of data in a single field rarely ends well. A few options I see
A series of binary fields for gasoline type. So you'd have a True/false for gasoline, ethanol, and diesel. This is easy to show with checkboxes on the form.
If you know there will only be certain combinations, like if there will not be a 'diesel and ethanol' without gasoline, you can build it as a single value combobox.
You will probably need some VBA to do what you need to do. Try something like this:
Dim R as String
R=""
if (Me.Diesel) then R = R & "Diesel;"
if (Me.Gasoline) then R = R & "Gasoline;"
… {add any other types of fuel}
Me.MyComboBox.Rowsource = R
Me.MyComboBox.Requery
This can be added tot he form's OnCurrent event so it will update the combo box every time a new record is displayed.
If your form does not include the various fuel types as fields, either add them as hidden fields or use DLookup to read them off the table.

How to set a Filter for the ComboBox List Items Edit Form?

Short form of the question:
How can I set a filter for the form which is used by the "List Items Edit Form" property of a ComboBox?
Long description of the environment:
In my database, there is a growing number of structurally similar values which describe something. I collated all of these in one table named ComboTexts, and added a second table ComboTextTypes to customize the field names on the user side. Example:
ComboTexts table:
ID s1 s2 s3 TypeID
1 1 First Floor Ground Floor 2
2 2 Second Floor Null 2
3 AOX DIN 1485 determination of organic components 3
ComboTextTypes table:
ID formtitle ch1 ch2 ch3
2 Floor Floor Number Floor Name Alternate Name
3 Process Process name Standard Description
In order to edit entries in ComboTexts, I provide two forms: The form CoreData displays the list of formtitles from ComboTextTypes and an "edit" button. When the user selects a fomtitle and clicks "edit", the form EditComboTexts is called with a filter for TypeID set. EditComboTexts extracts the TypeID from its filter and modifies itself with the information from ComboTextTypes.
So far there exist 14 ComboTextTypes in the database, and that number is growing. I simply didn't want to have 14 or more tables and forms, which basically all do the exact same thing. Instead I just have two tables and forms, although a little more complicated ones.
The above mechanic is all set up and works fine.
Description of the problem:
The users want to be able to modify the 14th ComboTextType from inside the combobox. The detour through CoreData is three clicks too many, they know that ComboBoxes can offer an edit button for their value list, and want to use it at that point.
Access offers the "List Items Edit Form" for this purpose. When I enter the EditComboTexts form there, it's working in principle, but (of course) the filter is wrong.
How can I set a filter for that form?
You can filter the rowsource using SQL and simply change the rowsource value of the combo box
Me.Combo0.RowSource = "SELECT myfield FROM Table2 WHERE Table2.myfilter = 'value' ; "

How to filter specific records using a combobox and display in a subform

I am trying to filter records from a combobox, if I select Cape Town only Cape Town records must show in the subform but if I select Durban only Durban records must display in the subform. I dont get any errors but if I Select Cape Town only Cape Town records shows and as soon as I select Durban the the Cape Town records still display it doesn change to show the Durban records in the subform. Heres my code below any help will be much appreciated Thanks
If Me.Combo0.Value Then
strFilters = "Sites = 'Cape Town'"
DoCmd.OpenForm "Home"
Forms!Home![Plant Transaction subform].Form.Filter = strFilters
Forms!Home![Plant Transaction subform].Form.FilterOn = True
ElseIf Me.Combo0.Value Then
strFilters = "Sites = 'Durban'"
DoCmd.OpenForm "Home"
Forms!Home![Plant Transaction subform].Form.Filter = strFilters
Forms!Home![Plant Transaction subform].Form.FilterOn = True
End If
As long as your tables are set up right you should be able to do this with little / no coding at all.
Lets create a table called destinations and put 3 fields in: Unique ID, Townname and Area: example:
ID | TownName | Areas
CT | CapeTown | PlaceWithinCapetown
CT | CapeTown | AnotherPlace
D | Durban | PlaceHereAswell
so on and so on.
Lets assume you have 2 elements on your form. A Combobox and a Listbox.
ComboBox - List of Towns
ListBox - List of Areas Within Town.
Lets say the combobox is called cboTowns and the ListBox is called lstAreas
Click on the combobox and in the Row Source place your query, Something like:
SELECT DISTINCT destinations.ID, destinations.TownName FROM destinations;
Here i have asked for the Unique ID and Town name and set my bound column to 2 (So Only The Town Name Shows when user selected one), Now your combobox is populated with the towns, You will note the towns are listed more than once. You need to change the property's of your query to allow Unique values only Go to Row Source, Click on 3 dots this take's you to query builder select a blank area and on the right find the setting Unique Values set this to Yes and the combobox is complete.
Now i have the list of Towns and the Unique ID we can get the listbox to start working. Before you move on if you List all of your information all in 1 table as detailed above it is easier to lookup the data. If its over 2 tables you will need to create a relationship in Access. before moving on.
Once done go to the list box and again in your row source you want to put something like:
SELECT DISTINCT destinations.ID, destinations.Areas FROM destinations WHERE (((destinations.ID)=[Forms]![FormName]![cboTowns])) ORDER BY destinations.Areas;
So here i told it to look for Unique ID that's been set via combobox and then look for areas attached to that Unique ID.
Every time the user selects a an entry from the drop-down box the list box updates straight away. no other coding needed. If you have more information and need to display other things you can then use the DLookup function to populate any other fields you might have.
I will also say if you don't want to restructure what you already have looking at the code you supplied I noticed you are not requerying anything. You will need a Me.requery after end if in order to refresh the form. otherwise it will not update anything.

SSRS: How to select two or more values from a long filtered multiple selection list with different search strings

Let's consider a multiple selection parameter on a report: Employee
This parameter has a lot of possible values. Initially nothing is shown on the list and there is a textfield search parameter associated, that updates the Employee selection list with top n matches for the searched string.
If the entered search query is John Doe we can imagine that now the selection list shows:
John Doe
...
Xavier John Doesson
Now I can select as many items as I want from this filtered list, but if I want to select both John Doe and Alicia Keys happens the following:
First when I enter the search string "John Doe" the selection list gets populated accordingly
I select John Doe - OK
I enter search string "Alicia Keys", the selection list gets populated also
Selection of John Doe is gone - I want to be able to select both Alicia and John at the same time, but I don't want to go through a thousands of names long selection list
Update:
Forgot to mention that we have an OLAP cube in the background with dimension 'Employee'. This dimension is used as the source of the parameter and the param dataset uses MDX to fetch the values, therefore the SQL solution cannot be applied here.
The current solution creates an custom set with MDX Filter and Head functions and then this set is used in the ROWS-part of the MDX query.
Here is how the set created:
SET setEmployees AS {
HEAD(
FILTER( [Employees].[Employees].ALLMEMBERS,
INSTR([Employees].[Employees].CURRENTMEMBER.Name,#EmployeeSearch,1 >= 1 )
)
,100)
}
Basically the problem with this solution is that how do you add multiple search strings to the instr function
Is there a common solution to this kind of situation? Am I approaching the problem from wrong direction?
What you could do is make the search parameter more flexible, so you can handle input such as:
John OR Jane
If "OR" queries are more common than "AND" queries you could support it with queries such as:
John Jane
Note that this may throw people off, because the search features they're used to (such as Google search) typically tend interpret multiple words in the "AND" sense.
Anyhow, the tricky bit of course is the SQL behind the Employee data set. This should use the search parameter in a more flexible way. You haven't specified how that's currently working, but I imagine you may be using something like:
WHERE Employee.FullName LIKE '%' + #SearchParameter + '%'
You would need to extend that to support "OR" queries. There's a whole range of solutions for that, from quick 'n dirty handmade SQL (e.g. string split combined with WHERE...IN) to full-text querying. Choose a solution that's best for your situation.
If you have a fixed number of search terms than you can do something like the following.
FILTER( [Employees].[Employees].ALLMEMBERS,
INSTR([Employees].[Employees].CURRENTMEMBER.Name,#EmployeeSearch1,1 >= 1) OR
INSTR([Employees].[Employees].CURRENTMEMBER.Name,#EmployeeSearch2,1 >= 1)
)
Even if you can do that, I do not recommend it. You don't have the luxury to index Analysis Services like you do SQL. A better possible approach would be to query your data warehouse for the employees and return the appropriate keys, and then filter by those keys in your MDX statement.

ms access: retrieving latest value

I have a table Project.
It has the following column: Project ID, projectName, UpdateTime
The data in the table is as follows:
Project ID projectName UpdateTime
1 abc 12-2-2009 01:10:00
1 abc 12-2-2009 04:18:00
2 xyz 17-7-2009 08:45:00
2 xyz 17-7-2009 12:21:00
i want the result set to display the latest update project information based on the update time.
for the above example , it should display
Project ID projectName UpdateTime
2 xyz 17-7-2009 12:21:00
1 abc 12-2-2009 04:18:00
This ought to do the job (as edited to reflect the information #Remou pointed out that I failed to note in your original question):
SELECT [Project ID], ProjectName, Max(UpdateTime)
FROM Project
GROUP BY [Project ID], ProjectName
ORDER BY Max(UpdateTime) DESC
If that doesn't do it, either I've made a mistake, or there's other information not included in your question.
SELECT P.[Project ID]
, P.projectName
, Max(P.UpdateTime) AS [Latest Update Time]
FROM Project AS P
GROUP BY P.[Project ID]
, P.projectName
ORDER BY Max(P.UpdateTime) DESC;
I have an answer which uses the graphical interface of a query which may be easy for you to implement. It is presumed that all 3 fields are contained in a single table. Create a new query by clicking "CREATE" and thereafter "Query Design". Right click in the upper section of the graphical interface that opens and click on "Show Table" Select the table that contains these three fields and double click on each field, one at a time so that it appears in the lower section. If you have an auto-number field please ensure that you do not include it in the lower section since this will cause all the table records to display. Any other additional field included in the query could have the same undesired result. Next, on your ribbon click "DESIGN" and after that click on the "Totals" icon. Focus attention on your "UpdateTime" field in the lower section. Change "Group By" to "Max" by clicking into that field and selecting from the drop-down list. To not make ay changes to the other two fields. Save the query and open it in Datasheet view (Do this by clicking "HOME", "View", "Datasheet View"
You should get the results you required. Please let me know whether this has now worked for you.
Ps. Spelling errors in the project name will also give you inaccurate results. To reduce the likelihood of this possible problem have your users to select project names from a combo-box that that has a drop-down list of project names. It is also possible to devise a means whereby the Project name is auto entered when a user selects a Project ID from a drop-down list.
I am not sure why everyone seems to want to group the projects. The question simply seems to ask to display the results in descending order of UpdateTime
SELECT * FROM Project
ORDER BY UpdateTime DESC
I also notice however, that in his example, the results that he anticipates has the records grouped by Project ID and Project Name, in which case the answer provided by #David-W-Fenton must be sufficient.
In Conclusion, he should have asked his question more clearly.