Access 2003 - Running an update query based on select query results - ms-access

I currently have a subform that displays a select query. I want to update all the records of Table B that are showing in the subform with information from the form. The subform is not necessary. I was just using it to make sure my select query was displaying correctly.
Table A has 3 columns (OID, Project_Number, Landowner)
Table B has 4 columns (OID, PhoneNum, Address, Year)
These tables have a one to many relationship. One OID in Table A relates to many in Table B
Table A
1 A10 Bill
2 B10 Sally
3 A10 Bill
Table B
1 555 123 blah st 2012
1 2013
2 111 456 aaa st 2012
3 2012
The form allows the user to enter information that populates Table B.
The subform displays a list of records where Project_Number, Landowner, and Year are equal to the record showing on the form
For example. If the form is showing
1 A10 Bill
the subform is showing
1 A10 Bill 2012
3 A10 Bill 2012
When I click a save command button I would like it to run the update query but I'm having issues with the SQL command.
My Select query is as follows:
SELECT B.Project_Number, A.LANDOWNER, B.Year
FROM A INNER JOIN B ON A.OBJECTID = A.OBJECTID;
The subform is setup
Link Child Fields: Project_Number; Year; Landowner
Link Master Fields: B.Project_Number; Year; A.Landowner
I would like:
UPDATE B.PhoneNum, B.Address, B.Year
WHERE items found in my subform
WITH information from my form
Is it easier to forget the subform and do it all through a single update query?
UPDATE B SET B.phonenum = [New_Info]![PhoneNumCtrl], B.Address = [New_Info]![AddressCtrl]
WHERE [A]![Landowner] = The same landowner as the OID selected, [A]![Project_Number] = The same project number as the OID selected, [New_Info]![Year] = [B]![Year]
Thanks in advance for any help!
Everything is working now. I wanted to add to djphatic's answer.
When doing this make sure to add [Forms]![formname]![controlname]

Depending on where the controls are on your form you may need to change the control reference.
Use the query builder GUI to create a select query which has the columns you wish to update and filter the records using the controls on your form. Once you have this you can change the query to an update query and set the values the controls on your form.
UPDATE B
SET B.phonenum = [formname]![controlname], ...
FROM B JOIN A ON B.OID = A.OID
WHERE A.PROJECTID = [formname]![controlname]
AND B.YEAR = [formname]![controlname]

Related

Return all rows from table and add extra column to indicate pivot table inclusion

I'm trying to create the data for a form with some grouped checkboxes, which includes the user's previous selections. To prepare the data, I'm trying to return all the rows from my checkbox visibility table and add an extra column which indicates whether or not a row from the visibility table appears in the pivot table users_visibility. How do I do this?
Here is what I have. It returns what the two tables have in common based on a user's id.
select
visibility.id as visibility_id,
visibility.title as visibility_title,
users_visibility.users_id as checked
from visibility
left join users_visibility on users_visibility.visibility_id = visibility.id
where users_visibility.users_id = 2
Tables example
visibility
=======================
id title
-----------------------
1 Drivers licence
2 No Criminal record
3 Senior volunteer
users_visibility
===========================
id users_id visibility_id
---------------------------
1 2 3
What I would like returned
========================================
visibility_id visibility_title checked
----------------------------------------
1 Drivers licence NULL
2 No Criminal record NULL
3 Senior volunteer 2 (user_id or whatever indicator)
Thanks.
For a left join, conditions on all but the first table should be in the on clause.
Table aliases would also make the query easier to write and to read:
select v.id as visibility_id, v.title as visibility_title,
uv.users_id as checked
from visibility v left join
users_visibility uv
on uv.visibility_id = v.id and uv.users_id = 2;

SSRS lookup with source not in current tablix

I have 3 tables that I want to show in a tablix SSRS report in 3.0.
Table 1 - policy ID, amt paid by company
Query - Select * from table1 where amt paid by company <> 0
Table 2 - policy ID, policy number, previous policy number
Query - Select * from table2 where previous policy number <> ' '
Table 3 - previous policy number, paid under prior company
Query - Select * from table3 where paid under prior company <> 0
I want to display the following columns on one tablix row per entry in table 1:
Policy number
amt paid by company
previous policy number
paid under prior company.
I created a tablix. I can display everything from table 1 and use lookup for table 2 items but when I do a lookup for item from table 3 it gives me an error.
From my research about this error I understand it to mean I cannot use the source in lookup from any table but table 1 in my case. And I can only do one level in lookup.
I have looked and can find no example for this anywhere and I have tried other methods and cannot figure out how to get to that piece of data in table 3.
Is my only choice to combine tables 2 and 3 together then use in this report with the lookup?
You can use INNER JOIN to get a dataset that contains all fields you require to show in the tablix.
SELECT
table2.PolicyNumber,
table1.AmntPaidByCompany,
table2.PrevPolicyNumber,
table3.PaidUnderPriorCompany
FROM table2
INNER JOIN table3
ON table2.PrevPolicyNumber = table3.PrevPolicyNumber
INNER JOIN table1
ON table1.PolicyID = table2.PolicyID
WHERE table1.AmntPaidByCompany <> 0
AND table2.PrevPolicyNumber <> ''
AND table3.PaidUnderPriorCompany <> 0
Live Demo
Let me know if this helps.

Build reports dynamically in ssrs

I have data in my database table which looks similar to the data below :
Time Name Sales
Aug-11 A 33
Aug-12 B 34
Aug-13 C 31
Aug-14 D 39
Sep-11 A 99
Sep-12 B 34
The requirement is that I need to build a report for A details ,another report for B details ,and another for C details and so on. There Could be a D and E added to the database table next month and some more data the month later...
I want to know if there is a way that I can create the reports dynamically , rather than creating a report for A, B and C now and then going into the report and creating another report for D and E next month and soo on..
Please let me know.
Create a report with a parameter #Name , Create a Drop down List using query something like
SELECT DISTINCT Name
FROM Table_Name
for user to select A, B or C and in parameter properties you can choose it to be a multiple selection and create a query something like
SELECT Time, Name , Sales
FROM Table_Name
WHERE (Name IN (#Name_Pram))
You will have to create 2 Data sets one for the drop down list from where users will select Name parameter, selected value(s) will be passed to the above query , One report will show details for any selected values.

Populating with '0' when Data in SSRS Does not exist

I'm trying to create a report in SSRS where I have a matrix, which has gender as the column headings and specifically defined agegroups as the rows. The report is sorted by date (ie, the records being displayed are filtered by the modifedAt value). My problem is that i wish for all of the age group categories to be displayed, even if the dataset does not return any data for that row.
So, for example, if i set the date to be a date where there are no db rows where there are Age5-16 children in - I still want to display the category name, but just have the cells related to that row to display '0'. Instead, the report just drops the whole row because, obviously the query returns no data.
Is the solution to have a separate dataset that brings back the entire list of categories and then somehow fit them together? I'm stuck here so any help is appreciated!
I can think of a few ways to do this:
DataSet level
Instead of just returning the relevant data in the underlying data in the DataSet, include all the categories you want to display in all cases.
e.g. For a database query it might be the difference between an inner and left join, i.e. going from something like:
select *
from AgeGroup
inner join MyData on ...
to:
select *
from AgeGroup
left join MyData on ...
So the report always has all the age groups to display. Where there are NULL values, just display 0.
I think this is the best option if you have control over the DataSet - you won't have to update your report at all, with luck the actual DataSet changes should be minimal, there is still only one DataSet call, and it's by far the simplest to maintain.
Hard code groups into the report
Here you include a table header row for each group you want to display, so these are always displayed in all cases.
Here you have some sort of conditional expression to display the values, e.g. For each group row it will be tailored to that group:
=Sum(IIf(Fields!AgeGroup.Value = "5-16", Fields!Amount.Value, Nothing)
This is not too flexible and will need updates as you change groups, and doesn't have as many options for layout. There is still only one DataSet call, so that is a plus.
Subreports
You can have a parent DataSet that displays one row for each age group, then embed a subreport in each row that displays the data you want for that row.
This allows you flexibility in layout but it will add complexity to the report(s) and will mean that you make a lot of DataSet calls that could be avoided with other options.
I know this is old, but I wanted to elaborate on Ian's section 1 above using joins at the dataset level. (His answer was super helpful to me for a report I'm working on.)
per op:
Is the solution to have a separate dataset that brings back the entire list of categories and then somehow fit them together?
That is how I've handled it successfully, but you can do so without actually creating a separate dataset by using common table expressions (or temp tables, of course).
For these example tables:
AGE_Table
ID Group Group_Desc Toys
1 A 00-10 Teddy Bear
2 B 11-20 Video Game
3 C 21-30 Sports Car
4 D 31-40 Mansion
5 E 41-50 Jewelry
People_Table (filtered for whatever date)
ID Name Age Gender Age_Group
1 Ariel 07 F A
2 Brandon 23 M C
3 Chelsea 27 F C
4 Derek 06 M A
You want to see 2 results for the 00-10 row, 2 for the 21-30 row, and then still see rows for the other age groups even if there aren't any results.
We want to create a dataset with all the different age groupings and then join on it. Behold a solution using common table expressions:
with CTE_Age AS
(SELECT Distinct Age_Group from AGE_Table)
SELECT ID, Name, Age, Gender, CTE_Age.Age_Group FROM People_Table
RIGHT JOIN CTE_Age ON
People_Table.Age_Group = CTE_Age.Age_Group
This will return:
ID Name Age Gender Age_Group
1 Ariel 7 F A
4 Derek 6 M A
NULL NULL NULL NULL B
2 Brandon 23 M C
3 Chelsea 27 F C
NULL NULL NULL NULL D
NULL NULL NULL NULL E
Once you have that in your dataset, you can change NULL values to 0 on the report builder side -- I think in 2008R2 the default is just blank.

How to SELECT MySQL 1-to-LAST on 1-to-Many Relationship

I have 2 Tables -- Table 1 is a master file and Table 2 is an activity file.
The relationship is 1-to-Many.
I am producing a report and all I want to return is every master file row joined to only the last activity row for the related master id.
I am unsure on how to request the last activity row. My code below returns every activity row (rightfully so).
Thank you for your help.
SELECT *
FROM master_file AS master
INNER JOIN activity_file AS activity ON activity.id = master.id
ORDER BY master.display_name
The activity file has a column called entry_date. It is a date and time stamp recording every activity. I simply want to select the last entry_date.
For example:
Table 2 - Activity looks like this
ID ACTIVITY ENTRY_DATE
1 Update 2012-08-01 09:00:00
1 Edit 2012-08-01 13:45:15
3 Create 2012-07-15 10:09:52
3 Delete 2012-07-22 23:02:00
3 Add 2012-08-05 04:33:00
4 Edit 2012-08-03 15:12:00
One standard way to solve this is to create an inline view that finds the last entry_date per ID.
SELECT *
FROM master_file AS master
LEFT JOIN activity_file AS activity ON activity.id = master.id
LEFT JOIN (select id , max(entry_date) entry_date
From activity_file
group by id) last_activity
ON activity_file.id = last_activity.id
and activity_file.entry_date= last_activity.entry_date
ORDER BY master.display_name
The one problem with this approach is that you may have more than one record with max(entry_date) for a given id. You'll either need to have your business rules handle this (e.g. simply display more than one record for that case) or you'll need to figure out a tie breaker. The last thing you want is to make it non-deterministic