How to make a table on Access using "Count" and Age Range? - ms-access

I downloaded a record of all the people on the first fleet from this website. http://firstfleet.uow.edu.au/download.html
I downloaded the Excel and imported it into Access. In the document, certain people’s age is not known so they are simply put as -1.
I am trying to make this table but I am struggling.
I’ve tried doing it by creating a table but that was too hard so I decided to create a query instead. I am using “Crosstab” but failing to use it successfully.
When I do try to sub the -1 for unknown:
Messages like this pop up:
How do we add the table in but sub the -1 for unknown while keeping the layout the same? I am hard stuck at a place where I cannot even view how my query looks.
How do I even make the layout the same as the picture shown above?
UPDATE:

Calculate the age groups with Switch() function and use that calculated field for CROSSTAB RowColumn. I renamed the Age field in table from the original import name to shorten the expression.
TRANSFORM Count(Convicts.ID) AS CountOfID
SELECT Switch([Age]=-1,"Unknown",[Age]<15,"10-14",[Age]<20,"15-19",[Age]<25,"20-24",[Age]<30,"25-29",[Age]<35,"30-34",[Age]<40,"35-39",[Age]<45,"40-44",[Age]<50,"45-49",[Age]<60,"50-59",[Age]<100,"60-99") AS AgeGrp
FROM Convicts
GROUP BY Switch([Age]=-1,"Unknown",[Age]<15,"10-14",[Age]<20,"15-19",[Age]<25,"20-24",[Age]<30,"25-29",[Age]<35,"30-34",[Age]<40,"35-39",[Age]<45,"40-44",[Age]<50,"45-49",[Age]<60,"50-59",[Age]<100,"60-99")
PIVOT Convicts.Gender;
The total row is added by clicking the Sigma(Totals) icon on the ribbon when query is in Datasheet view.
A simpler calculation will generate more groups:
TRANSFORM Count(Convicts.ID) AS CountOfID
SELECT Partition([Age],0,100,5) AS AgeGrp
FROM Convicts
GROUP BY Partition([Age],0,100,5)
PIVOT Convicts.Gender;
But a little adjustment to the SELECT clause will get same output as the first:
SELECT IIf([Age]=-1,"Unknown",IIf([Age]<50,Partition([Age],0,50,5),IIf([Age]<60,"50:59","60:99"))) AS AgeGrp
Could add a field in table and use one of those expressions in UPDATE action SQL to add calculated AgeGrp, which would simplify the CROSSTAB and other queries needing that group identifier.

Related

MSACCESS Report - Show or Hide "Group By Header"

Goal - The report has calculated fields in the HEADER of the GROUP on Reinsurer
The goal is to only print this header when there is MORE THAN ONE "Issuing_Dealer_Name"
In the data, Corinne has 2 dealers. Therefore I want to print the HEADER of the GROUP on Reinsurer
Adam has 1 dealer. Therefore, don't print the Header of the Group on Reinsurer
Therefore, what code goes into the "On Format" event of this header?
Here's a link to a jpg of the data -
View the Dataset
The two suggestions above solved it. Thanks!
I resolved this by creating a make-table query with a select distinct clause. Then I referenced this new table in the DCount function. It works. Once I have time, I'd rather use an array to minimize report creation resources used. BTW, I created a table vs. query since the DCount would be called about 600 times.

Testing if a row exists in a table for a report

I'm trying to write a report that lists all the rows from a master table and inserts a field ("Y"/"N") if the key exists in a current-use table. For example, the COLOR_MASTER table has 256 colors by COLOR_NAME (field/key). The CURRENT_PROJECTS has a row for each project and the COLOR_NAME (field). I want to list all the COLOR_MASTER rows and then on the same print line a "Y" or "N" if that COLOR_NAME is used in the CURRENT_PROJECTS table.
I've tried to mess around with it in the Design View and have had no luck. The JOIN that was created looks basically like this and how I want the report is following the dashes:
RIGHT JOIN COLOR_MASTER ON CURRENT_PROJECTS.COLOR_NAME =
COLOR_MASTER.COLOR_NAME ON CURRENT_PROJECTS.COLOR_NAME =
COLOR_MASTER.COLOR_NAME;
--------
Color Used
BLUE
RED Y
YELLOW
I have no expertise in JOINs and I don't understand why this JOIN was created or what I need to do to fix it. Based on my reading, I guess it's trying to do an outer join.
Currently, I'm just trying to show the COLOR_NAME if used as I don't know how to test that it is used and convert it to "Y". I don't care if the color is used once or twenty times and I don't really want any data from the CURRENT_PROJECTS table.
Under the "Used" column I now have "#Error" on all lines. So, I'm figuring that the RIGHT JOIN has an error.
Any guidance is appreciated.
Thanks
I expect the RIGHT JOIN is defined in the table Relationships builder and a query using the tables adopts the established link. If you want to change the join type in a query then with query in design view, double click on the line linking tables to open the Join Properties dialog.
If there is only one record per project in Current_Projects and a color can be used only once, simply:
SELECT Color_Master.Color_Name, IIf([Current_Projects].[Color_Name] Is Null,"N","Y") AS Used
FROM Color_Master LEFT JOIN Current_Projects ON Color_Master.Color_Name = Current_Projects.Color_Name;
However, it sounds like each color can be used for multiple projects. So will need to use GROUP BY or DISTINCT query joined to the Master_Colors table:
SELECT DISTINCT Current_Projects.Color_Name FROM Current_Projects;
SELECT Color_Master.Color_Name, IIf([Query1].[Color_Name] Is Null,"N","Y") AS Used
FROM Query1 RIGHT JOIN Color_Master ON Query1.Color_Name = Color_Master.Color_Name;
Here is all-in-one SQL:
SELECT Color_Master.Color_Name, IIf([Query1].[Color_Name] Is Null,"N","Y") AS Used
FROM (SELECT DISTINCT Current_Projects.Color_Name FROM Current_Projects) AS Query1
RIGHT JOIN Color_Master ON Query1.Color_Name = Color_Master.Color_Name;
If you don't want anything displayed for the N result then use Null without quote marks in the expression.
An alternative is DLookup() but domain aggregate functions can perform slowly in query or textbox expression.
Advise not to save the calculated value to table, just calculate when needed.
You don't have to do it all in one query. Start by building a query that as the IDs of all the colors currently in use. Name it something like ColorsInUse and save it.
It could be something as simple as:
SELECT [COLOR_NAME] FROM [CURRENT_PROJECTS] GROUP BY [COLOR_NAME]
Then all you have to do is run the following queries:
UPDATE [COLOR_MASTER] SET [MyField]="N"
UPDATE [COLOR_MASTER] SET [MyField]="Y" WHERE [COLOR_NAME] IN [ColorsInUse]
Another way of doing it is by using DLookup. Something like this:
UPDATE [COLOR_MASTER] SET [MyField]=IIF(NZ(DLookup("[COLOR_NAME]","[CURRENT_PROJECTS]","[COLOR_NAME]='" & [COLOR_NAME] & "'"),"")="","N","Y")
Some notes: You should not use a text field (like a name) as your key. You should always use numerical IDs. You should also never use a text Y/N. Use an actual Yes/No field instead.

Aggregate function in group header?

I have a really simple recordset by the name of qryUserFiles:
string: [UserID], [ftype]; Long: [fsize]
(records are file-information details of each file in each users' userfolder on the file-server)
I have created a report which lists the sum of [fsize] for each [UserID] by grouping on [UserID] and putting =Sum([fsize]) in a textbox in the header of the group. So far so good. So that shows me the total size of that user's user-share on the fileserver. (Helpful with respect to both convincing the users to clean up their stuff, and convincing the executives that we need to buy more storage!)
Now the headbanging part.
I want to add, in that same header, various textboxes containing the sum of the [fsize] for various values of [ftype]. So, for example, I'd want the sum of [fsize] for files where ftype="jpg" for each UserID. (And then another sum of [fsize] for files where ftype="mov" for each UserID, and so on for various 'problematic' file-types!)
I tried putting a
Dsum("fsize","qryUserFiles","ftype='jpg'")
in the group header, but, as expected, it is looking at the entirety of the 'qryUserFiles' recordset, and giving me the domain-total of size for jpg, not the this-user-total of size for jpg.
I considered trying to add something to the 'where' clause of the dsum function that would include 'this' user as criteria, but how would I refer to 'this' userID, with respect to the grouping-pointer?
Or is there a way easier way to do what I want without going nuts!? Would this be easier in another query? I feel like there's a simple, obvious answer just out of reach!
(I'm hoping to avoid brute-force VBA code to step through the table record by record, and calculate the stats with Dsum and add them to a new table. But if that's how to do this....)
I think - judging from what I read - you just need to let the report do its stuff. You can group on user and ftype - including the totals in the details and the totals in the Group Footer
You definitely should not use a DSum formula

MS Access Query using IFF to compare values

I am trying to build a query which will look at the data in two fields in two different tables and check to see if the data is the same, if it is I want it to return the number of times it is matched, if it isn't I simply want it to return the text saying "No viewings".
I have constructed this query in my access database which has the field from the first table "Property" and the second field I want it to compare the data with, "Viewings". I have build the following expression using the build tool, however I am stuck to make it work since every time I get this error message when trying to run the query: "Your query does not include the specified expression 'Property Viewed' as part of an aggregate function."
totalViewings: IIf([Viewings]![Property Viewed]=[Property]![ID],Count([Viewings]![Property Viewed]=[Property]![ID]),"No Viewings")
Any help how to overcome this error would be very appreciated.
Thanks
I would suggest doing something like this:
1) Assuming this is something you are developing yourself, make sure your data structure is all in order first. Since I dislike relatively code-hostile identifiers, I'd have the tables as so -
Properties - PropertyID (AutoNumber, primary key), HouseNumberOrName, Street, etc.
Viewings - ViewingID (AutoNumber, primary key), PropertyID (Number/Long Integer), ViewingDate, etc.
In the Relationships view, Properties.PropertyID would then be set up to point to Viewings.PropertyID in a one-to-many relation.
2) Your actual query I would then break into two, the first to compile the data and the second to format it for display. The first would go like this, saved as ViewingCounts...
SELECT Properties.PropertyID, Count(Viewings.PropertyID) As ViewingCount
FROM Properties LEFT JOIN Viewings ON Properties.PropertyID = Viewings.PropertyID
GROUP BY Properties.PropertyID;
... and the second like this, saved as ViewingCountsForDisplay:
SELECT Properties.*, IIf(ViewingCount = 0, 'No viewings', ViewingCount) AS Viewings
FROM Properties INNER JOIN ViewingCounts ON Properties.PropertyID = ViewingCounts.PropertyID
ORDER BY Properties.PropertyID;

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.