referring to sum of elements in column with Microsoft Access 2010 - ms-access

My employee's IT department refuses to install R for me (claiming the open source version is a security risk) and I tried to create reports with Microsoft Access 2010.
I got stuck trying to create a query on a simple table of hospital wards (for the purpose of keeping track of a data collection exercise):
I did not manage to allocate each ward a sample size that would be proportional to its bed capacity (third column above) as I was not able to refer to the sum of the elements in the "bedCapacity" column. With Excel, I would try something like this:
Cell D2 in Excel contains =INT(C2/SUM(C$2:C$6)*50)+1 and cells D3 to D6 according formulae.
Is there any way I can refer to the sum in the bedCapacity column in Access 2010? I tried creating a separate query that would sum up the the 'bedCapacity` column, however could not figure out how to refer to the value in that query.
I'm trying to use Access 2010 so I can create standardised reports for the data collectors on their progress (which is not easily possible with Excel 2010, as it requires too much manual manipulation - I tried pivot tables etc.).
Would be grateful for any insights.

Create and save this query to give you the total bed capacity from all wards.
SELECT Sum(bedCapacity) AS TotalBeds
FROM YourTable;
Replace YourTable with the name of your table, of course. For the rest of this answer, I will pretend you saved that query with the name qryGrandTotalBedCapacity.
Then you can use that query in another where you cross join it with your table. The cross join gives you a result set which pairs each row of one data source with every row of the other data source.
However, since qryGrandTotalBedCapacity returns only a single row, this amounts to just making the TotalBeds value available for every row of YourTable. And, from that starting point, it is easy to translate your Excel formula to the Access SQL equivalent.
SELECT
y.ID,
y.ward,
y.bedCapacity,
(INT((y.bedCapacity/q.TotalBeds) * 50) + 1) AS sampleSize
FROM YourTable AS y, qryGrandTotalBedCapacity AS q;

My RoundSum function will do that:
Public Sub ListSampleSizes(ByVal BedCapacity As Variant, ByVal SampleSum As Long)
Dim SampleSizes As Variant
Dim Item As Long
SampleSizes = RoundSum(BedCapacity, SampleSum)
For Item = LBound(SampleSizes) To UBound(SampleSizes)
Debug.Print Item + 1, BedCapacity(Item), SampleSizes(Item)
Next
End Sub
Result:
1 22 12
2 18 10
3 20 11
4 16 9
5 19 11
Too much code to post here, but full code is for download at GitHub: VBA.Round

Related

How to clean/sort data in R with multiple entries corresponding to few of the intermittent row variables under several columns?

the data sample imageHow to clean/re-organize data in R/R Studio in case a row variable has more than one entries under the column variables ? e.g. I have a data-set that has 13 columns , and 14 rows, in each of the month tab of an excel workbook depicting a year, there are 5 workbooks like this. So, in total there are 5 * 12 = 60 tabs. In each of the month tab, before the second/third/ etc. row starts, the previous row already has multiple entries under a column head, like the one in the attached image at the beginning.
how to format/clean this whole data, including all the months in a year, and also accounting for 5 years on trot, and make this suitable for analysis ? Thanks in advance .
Are you looking to import the excel data into R studio?
Look at the library "xlsx" in order to read excel sheets in.
This will be entered into your environment as a data frame. Available for you to analyse. If you want to have R studio recognise dates, then look at the library "lubridate".
You aren't particularly clear as to what you want here, after you've done some R coding, put it up, and we can help further.

What ID does a ComboBox reference?

I am attempting to maintain and fix a horribly out-of-date CRM designed by an ex-employee ~4-5 years ago in Access 2007. I have brought it into Access 2013 and fixed a ton of stuff up, but I am still running into many problems.
I spent a good 4 hours today attempting to figure out why certain values didn't line up. These values were being pulled from a SELECT statement on a Combo Box over a stored Query which simply returns a table with a few extra rows. Great.
However this value (a number) doesn't appear to correlate with what we expect. I enter in one value, save the ticket, and a completely different value gets stored into the table. Opening up the ticket, I see the value that I expect. Digging deeper, I found the following difference:
Set value_1 = Me.RegistrationID // What's being stored in the table
Set value_2 = Me.RegistrationID.Column(0) // What we expect
Surprise surprise! This is a Combo Box and some value is being stored in the table. The Control Source is "RegistrationID" and the Row Source is the query in question.
However I do not know what it is! This specific value correlating to the Combo Box appears to pull the correct data when we later open the tickets. However I have a strong feeling that this could be why many tickets from before one of the rows was deleted all appear to have invalid RegistrationID's.
How badly can this break?
How easily can we correct tens of thousands of tickets?
How can I fix this to store the correct value?
This is what I expect is happening.
Your combo box row source is based on a Select query which returns and displays multiple rows. For example:
Select RegistrationID, CustomerID, CustomerName From MyTable;
The Control Source for the combo box is bound to RegistrationID which is part of the Forms Record Source.
The issue is the bound column. If we set the bound column in our example to 1, then we get the behavior your are describing with:
Set value_1 = Me.RegistrationID - Set's value to CustomerID (may appear correct)
Set value_2 = Me.RegistrationID.Column(0) - position 0 from our query (RegistrationID)
Further building on our query example, you can say:
Me.TextBox1 = Me.RegistrationID.Column(0) - RegistrationID
Me.TextBox2 = Me.RegistrationID.Column(1) - CustomerID
Me.TextBox3 = Me.RegistrationID.Column(2) - CustomerName
The RegistrationID is what normally should be stored in the table.
As long as your form shows any values that directly relate to this RegistrationID you're fine.
I would start by checking to see under the format setting to see if column widths are set properly and I would also check under the data section to see if the bound column is correct. I might also throw in an after update macro/vba sub routine that saves the record. Hope this helps.

Displaying Total on MS Access Form

I need to develop quite a serious database in access and start building up the Main Menu.
So far I have (I have broken the problem down to a sample DB):
a) Set up a Table of Data (Table1) (Column 1 = Name, Column 2 = No of Staff (Numeric))
So my Data looks like this
Name No of Staff
TestName1 1
TestName2 2
b) Created a Query (Table1 Query) to provide a Total of 3 as a sum total - this works as I get a total of 3.
c) I created a Form that I want as my Summary Form.
and inserted a TextBox and placed the following formulae within the Expression Builder :
So I have the following :
But the Form field yields a #Name?
Sorry for the novice question but I have read up quite a lot about this and this should work fine. I am confused why this simple little task is daunting.
Any help would be great. thxs in advance
Two variants:
Set the query as datasource for your form and select the column with sum [Sum Of No of staff] as control source for your text field
set as Control Source for text field as
= Dlookup("[Sum Of No of staff]", "[Table1 Query]")

Using running totals in MS access report cumulatively

I am developing a db (MS access 2010) to support a school with a well-defined model for tuition quotation. The list of products is assembled for each quote, then various discounts are applied. The discounts may be a percentage or an absolute dollar amount. So far, so easy. The problem is that their business logic requires:
No limit on number of discounts.
Specific discounts to be applied in a defined sequence (implemented in my case with a "discount ordinal" column, values 1 (first applied) to 100 (last applied).
Each sequential application of a discount is to the running total of the quote. Eg: Total products $1000. Discount: 50%. Value: $500. Subtotal $500.
Subtotal: $500. Discount: $25. Value: $25. Subtotal: $475.
Subtotal: $475. Discount: $10%. Value: $47.50. Subtotal: $427.50.
This appears to be a variation of the "get the value of the field in the previous row" problem, but with the added twist that the "value of the field" is actually a cumulative calculation. It has the flavor of recursion: while discounts remain, subtotal(previous subtotal).
I have no clear idea how to implement this in a report, because the calculation as noted above is self-referential. I'm not looking for code here, just guidance on the general approach to the problem (ie, some kind of global variable, using VBA - in which case, I'm not sure what the "glue" between the query in VBA and the report would be - or some trick with a calculated field although I've spent a lot of time trying to figure one out). Any thoughts?
In that kind of situations, I always create a new table, that will get filled up when the report opens, and base the report in that table, not the original one. That way I can do all the calculations I need, even making several passes. The report then is simply a "dump" of the table. Complex totals can be additional columns, that will be shown only in the totals section.
You could have table for purchase history using an integer to link each purchase since an autonumber by itself will not link each discount stage.
So in excel I would use something like this:
v = Starting Value
i = 1
Do Until i = Last Discount
d = ws.Cells(i, 9).Value
v = v * (1 - d)
ws.Range("B2").Value = v
i = i + 1
Loop
At each stage you could write to the table (using docmd.runsql) the discount applied (d) and the value (v) but it could be quite slow. You could then order the table by purchase identifier then descending by value since sequential discounts will inherently order correctly.

How to structure my query for a MS Access Chart?

This is probably an easy one for a guru here.
I have a datatable of employee results by month structured into the following fields:
Name,Metric,Jan,Feb,Mar,Apr, ...,Dec
When I take this dataset as is, and select it all as an input for a simple column graph in MS Excel, the chart figures out that each series is defined by Name+Metric, the (y-axis) is the defined by the values in Jan-Dec columns, and the columns of the graph are split by date (x-axis).
When I feed this same dataset into MS Access with a simple Select query:
Select Name, Metric, Jan, Feb, Mar... Dec from Results
I need to be able to tell the chart how to use the data. How do I do this?
Also, I know that I could flatten the dataset so the dates are in rows, and then use a Crosstab query for Access to figure it out, but it seems inefficient to condense the data so that Access can just expand it out again. I'm going to assume there is a way to tell the chart somehow how to use the data, which is what I am looking for in an answer here.
Option 1
This is a quick and dirty fix.
Excel essentially is creating 'grouped' categories of Name + Metric via concatenation. A quick and dirty solution is to use the default bar chart and set its Row Source property to:
Select Name & ' ' & Metric, Jan, Feb, Mar... Dec from Results
After that if it still needs adjustment, double click on the chart object from design view and see what chart properties you can mess around with.
Option 2
For more advanced control of the chart layout, select your table Results and mess around with the PivotChart function in Access. This chart can later be added as a subform on another report or form.
side note: you should try to avoid the field name "Name" since it is a reserved word