DLookup: Need to be able to add to most recent entry - ms-access

I have a form that is being used to create entries with different sequential fields. I'm currently using DLookup in order to do this, but I am running into some issues.
Me.txtProgramID.Value = DLookup("ProgramID", "tblMain", "Program = Forms!Form2!Combo.Value") + 1
Me.txtProgramNumber.Value = DLookup("Number", "tblMain", "Program = Forms!Form2!Combo.Value") + 1
Me.txtSequence2.Value = DLookup("Sequence2", "tblMain", "Program = Forms!Form2!Combo.Value") + 1
Me.txtSequence1.Value = DLookup("Sequence1", "tblMain", "Program = Forms!Form2!Combo.Value") + 1
There are four different values: ProgramID, ProgramNumber, Sequence1, and Sequence2. Everytime a new record is added, based on the contents of Combo, the contents of the new field should be the previous field + 1.
Lets say the contents of Combo is A and that the values for ProgramID, ProgramNumber, Sequence 1, and Sequence 2 are all 1. The new record for A should have them all as 2. The problem I'm having is that instead of DLookup finding the most recent entry, it is capturing the original, meaning that instead of having 1, 2, 3, 4, etc I have 1, 2, 2, 2.
Really what I need to know is how to make DLookup grab the most recent record in regards to the respective profiles.

You might be able to get away with DMax, but once you have more than one user, all bets are off:
Me.txtProgramID.Value = DMax("ProgramID", "tblMain", _
"Program = Forms!Form2!Combo.Value") + 1
ProgramID should probably be an autonumber, so there is no need to get the next number, it is handled autmatically. However, there is no guarantee that an autonumber is the previous number +1. I am not sure why you would have a programID and a program number.
A proper sequential number is a but more complicated: Access VBA: Find max number in column and add 1

Related

SSRS - How to count values in a column that is generated by an expression?

In my SSRS report, I have a column named permit_status. This column has a value generated by an expression (i.e. it comes from custom code). The permit_status value is calculated row-by-row.
The requirement is to show a sum of the different values displayed in the permit_status column, and to have this export to Excel. The values are "Approved" "Pending" etc. I just need one cell showing the total for each value.
I am able to create a footer for this report that uses Sum(...), for example:
=Sum(IIF(ReportItems!permit_status.Value = "Approved", 1, 0))
However, this only shows the sum of "Approved" values for that page of the report - not for the entire report. (And I'm missing something as it doesn't export to Excel.)
What's the best way to sum ReportItems!column_name.Value into a grand total?
In your matrix add column group based upon permit_status ( so that each column contains one available unique value) . right mouse click on left most field on your row and then select add total after, amend this field expression to count(Fields!permit_status.Value)
As I understand your question you want to count the amount of "Approved", "Pending", etc... values. There are different ways to achive this. The easyest way without changeing the structure of your tablix is a expression:
Either for one Cell:
="Approved: " & Sum(IIF(ReportItems!permit_status.Value = "Approved", 1, 0)) & " / " &
"Pending: " & Sum(IIF(ReportItems!permit_status.Value = "Pending", 1, 0))
'And so on...
Or more cells:
'Cell 1
="Approved: " & Sum(IIF(ReportItems!permit_status.Value = "Approved", 1, 0))
'Cell 2
="Pending: " & Sum(IIF(ReportItems!permit_status.Value = "Pending", 1, 0))
How about you add two Extra columns "Assigned" and "Pending"
and then for those colum set Row rule as below respectively.
IIF(ReportItems!permit_status.Value = "Approved", 1, 0)
IIF(ReportItems!permit_status.Value = "Pending", 1, 0)
Then at end of data set just add Total as described here. IT will give you desired result

SSRS operations across lines of the same dataset - Subsetting - Lookup

I've got the following dataset:
I need to show in SSRS two lines: One corresponding to Sales for IsControl = 0 and the other one corresponding to IsControl = 1.
However, for the line that has a control = 1 Sales will have to be divided for the its Num_Of_Customers and multiplied for the Num_of_Customers of the same week_of_day where IsControl = 0.
In other words I'm scaling sales for IsControl = 1 to IsControl = 0. In a way that I can show the two lines on a line plot.
What is the SSRS expression to do that?
Sales and Num_Of_Customers in this case are equivalent for IsControl = 0 and IsControl = 1 but they are normally different.
If I understand the question correctly, the expression below should do what you need.
=IIf(Fields!IsControl.Value = 1,
Fields!Sales.Value/Lookup(Fields!week_of_day.Value.ToString() & "0",
Fields!week_of_day.Value.ToString() & Fields!IsControl.Value.ToString(),
Fields!Num_Of_Customers.Value,
"DataSet1"),
Fields!Sales.Value*Fields!Num_Of_Customers.Value)
If IsControl is 1, lookup the number of customers where IsControl is 0 for the same date, and divide Sales by that number. Otherwise, multiply Sales by the number of customers in the same row.
If you just need Sales for the rows where IsControl is 0, use this:
=IIf(Fields!IsControl.Value = 1,
Fields!Sales.Value/Lookup(Fields!week_of_day.Value.ToString() & "0",
Fields!week_of_day.Value.ToString() & Fields!IsControl.Value.ToString(),
Fields!Num_Of_Customers.Value,
"DataSet1"),
Fields!Sales.Value)
Whatever you need, the Lookup will get you to the row where IsControl is 0. Pull the value you need and do what ever math is necessary.
Good luck!

SSRS: Add multiple if statements

I am using SSRS to add certain values in a column together. If the Service ID is 13,15,18,or 19 I want it too add these values together. Right now I have:
=IIf(Fields!ServiceID.Value = 13,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 15,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 18,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 19,Fields!TermPrimary.Value, Nothing)
I thought This would allow me to add these values together, instead it only shows me the exact same values as the table below it showing serviceID 13. How do I add these figures together?
Your Service ID field will always be a single number and never equal to 13 AND 15 on the same line.
I think you want to Group your column by some other field and SUM the TermPrimary for that group IIF the ServiceID is one of your given values.
=SUM(IIf(Fields!ServiceID.Value = 13 OR Fields!ServiceID.Value = 15 OR Fields!ServiceID.Value = 18 OR Fields!ServiceID.Value = 19, Fields!TermPrimary.Value, 0)

MySQL query to assign values to a field based in an iterative manner

I am using a MySql table with 500,000 records. The table contains a field (abbrevName) which stores a two-character representation of the first two letters on another field, name.
For example AA AB AC and so on.
What I want to achieve is the set the value of another field (pgNo) which stores a value for page number, based on the value of that records abbrevName.
So a record with an abbrevName of 'AA' might get a page number of 1, 'AB' might get a page number of 2, and so on.
The catch is that although multiple records may have the same page number (after all multiple entities might have a name beginning with 'AA'), once the amount of records with the same page number reaches 250, the page number must increment by one. So after 250 'AA' records with a page number of 1, we must assign futher 'AA records with a page number of 2, and so on.
My Pseudocode looks something like this:
-Count distinct abbrevNames
-Count distinct abbrevNames with more than 250 records
-For the above abbrevNames count the the sum of each divided by 250
-Output a temporary table sorted by abbrevName
-Use the total number of distinct page numbers with 250 or less records to assign page numbers incrementally
I am really struggling to put anything together in a query that comes close to this, can anyone help with my logic or some code ?
Please have a try with this one:
SELECT abbrevNames, CAST(pagenumber AS signed) as pagenumber FROM (
SELECT
abbrevNames
, IF(#prev = abbrevNames, #rows_per_abbrev:=#rows_per_abbrev + 1, #pagenr:=#pagenr + 1)
, #prev:=abbrevNames
, IF(#rows_per_abbrev % 250 = 0, #pagenr:=#pagenr + 1, #pagenr) AS pagenumber
, IF(#rows_per_abbrev % 250 = 0, #rows_per_abbrev := 1, #rows_per_abbrev)
FROM
yourTable
, (SELECT #pagenr:=0, #prev:=NULL, #rows_per_abbrev:=0) variables_initialization
ORDER BY abbrevNames
) subquery_alias
UPDATE: I had misunderstood the question a bit. Now it should work

MySQL Update a field value with subquery with multiple returning rows

I have two tables "bank" and "bonds". Each user has a bank record but can have 0, 1, or more bonds records.
I want to write a script that updates the field "cash" in the "bank" table with the interests of the multiple bonds a user might hold. The interest is calculated by issuePrice * coupon fields of the "bonds" table. But since a user might hold multiple bonds, it should do this for each bond.
At the moment, I tried something like this:
$MySQL->db_Query("UPDATE bonds bo, bank ba SET
ba.cash = ROUND(ba.cash + (bo.issuePrice * bo.coupon), 2),
ba.earned = ROUND(ba.earned + (bo.issuePrice * bo.coupon), 2)
WHERE LOWER(ba.user) = LOWER(bo.holder) AND
LOWER(bo.holder) <> LOWER('Bank');");
But it doesn't give the expected outcome. I tried it with a user with 2 bonds, if both bonds should give 500 interest each, so a total of 1000, it only adds 500 like there is only 1 bond. If I set one bonds as 500 interest and the other one with an calculated interest of 1000, it suddenly adds 475.
It's probably worthwhile to ensure that your UPDATE statement is trying to update each user's row exactly once. A subquery is the best way to do this, most efficiently implemented as a joined table:
UPDATE bank
JOIN (SELECT LOWER(bonds.holder) as user,
SUM(bonds.issuePrice * bonds.coupon) as total
FROM bonds
WHERE LOWER(bonds.holder) != 'bank'
GROUP BY user
) as increments ON increments.user = LOWER(bank.user)
SET bank.cash = ROUND(bank.cash + increments.total, 2),
bank.earned = ROUND(bank.earned + increments.total, 2)
(For more optimization, the LOWER and ROUND calls should probably be eliminated, but that's another discussion.)
The most straighforward way is to use sub-selects and update the fields individually...
UPDATE bank ba1
SET ba1.cash = ba1.cash + (ROUND(SELECT SUM(bo.issuePrice * bo.coupon)
FROM bank ba2 JOIN bonds bo ON bo.user = ba2.user
WHERE ba2.user = ba1.user), 2)
...