Update Query in Access 2010-Update based on matching fields - sql

I'm an intern at a small company who is making some fixes to a 15 year old access database that is used to keep track of customers, jobs, etc. I'm trying to write an update query and can't seem to get to work the way I need it to. Here is my sitation.
I have two tables. One contains Company information. The other contains Contact information. The primary key of the company table is also in the contact table, for a one-to-many relationship. E.g., if a company has 3 contacts, the CompanyID number that is from the primary key of the Company table is associated with the three contacts in the Contacts Table.
Ok. Now the problem is that there is some legacy code in a form, called "Company", that searches simultaneously for both contacts and companies. The problem is that there is a field in the contacts table called "ContactCompany" that is being used by this old search function. If a user changes the Company name with the form, the Company name is updated on the Company table, but NOT in the Contacts table.
I decided that an update query would be the way to go, to sync up the Company Name information on both forms. I tried to design it so that if the ID numbers matched between the tables, the company name in the Company table would get copied over to the contacts table. My code is below:
UPDATE Company INNER JOIN Contacts ON (Company.ID = Contacts.CompanyID) AND (Company.ContactCompany = Contacts.ContactCompany) SET Contacts.ContactCompany = [Company].[ContactCompany] WHERE (([Contacts].[CompanyID]=[Company].[ID]));
When I run the query Access tells me that it is updating X number of records, but the records do not update.
Please note that I used query builder; the SQL code was auto-generated by access when I go into SQL view.
I'm not very experienced with VB; all of my knowledge has been from googling stuff. If someone could give me some pointers on what I'm doing wrong or how to proceed I'd be grateful. Thanks!

Because in your join condition you are selecting the Contact.ContactCompany which already has same ContactCompany name.
AND (Company.ContactCompany = Contacts.ContactCompany)
Remove that "AND" part and your query should look something like this.
UPDATE Company
INNER JOIN Contact ON Company.ID = Contact.CompanyID
SET Contact.ContactCompany = [Company]![ContactCompany]
WHERE (([Contact]![CompanyID]=[Company]![ID]));
P.S. Make sure you have backup of your database before updating

I use the query assitance from Access
You dont need AND (Company.ContactCompany = Contacts.ContactCompany) you are already saying those are different now.
UPDATE Company
INNER JOIN Contact
ON Company.CompanyID = Contact.CompanyID
SET Contact.CompanyName = [Company].[CompanyName];
You could add this line to only update those different
WHERE Contact.CompanyName <> = [Company].[CompanyName]

Related

Copy a specific table from one database to another

One of our staff members made a change on the live DB that has wiped the address details of circa 3000 users. I have attached yesterdays backup to a new DB, meaning I have a table of all the correct address on a different database.
Is there an easy way I can essentially copy this table from the backup DB to the live DB? I will of course test it first, as I wish this user did. I don't want to restore from the backup is it will erase a lot in different tables.
I have attached a backup of the DB in order to extract the records needed.
I probably should've added this, but the entire table hasn't been erased, just a number of records within the table. The table itself has
SerialNum, FirstName, LastName, Address 1, PostCode etc
The SerialNum, FirstName, LastName are all still fine, it's just the address fields that have been erased through a bad data import
You want something like this:
INSERT INTO ProdDB.dbo.[TableName]
SELECT *
FROM RestoredDB.dbo.[TableName] r
WHERE NOT EXISTS (SELECT 1 FROM ProdDB.dbo.[TableName] p WHERE p.ID = r.ID)
Of course you'll need to adjust that based on the real primary key and name of the table, and you may also need to turn on IDENTITY_INSERT.

MS Access - Query - Required Forms for Each Employee

I have 3 tables, all SharePoint lists. I am trying to create a query that will show me all of the required DQ_File Forms that do not have an attachment in the DQ_File.
DQ_File_Lookup is a lookup table for the description field in the DQ_File. It also has the "DQRequired" flag I am looking for to see all of the required fields that do not have an attachment.
I have included a screen shot showing the table layouts and relations.
Any help would be appreciated, I am sure I am just overlooking something obvious.
A example would be as follows:
Employee Name | Document Name
You would have employee Joe and he has forms A,B,D out of a possible forms A,B,C,D,E,F so he would be missing forms C,E and F.
So the employee name would come from the employee table, and the document name needs to get passed through the DQ_File Table from the DQ_File_Lookup
the way I thought to do it was to get it to show all documents from the DQ_File table that are missing, that I can do. But that only shows the information that has an entry. There are certain forms that are required for every employee that I want to be able to see if a employee is missing any of those forms.
Using what #June7 posted below I got it to work, and it now will show me all 15 documents that are required for every driver. But when I add the attachment field from DQ_File it shows them all as zero attachments, when I know some of them do indeed have attachments already.
Here is a screen cap showing this.
Williams in particular should only have about 5 documents that should be on this list, but instead it is showing like all 15 are missing.
Here is the SQL from the combined query:
SELECT [qryEmployees+DQFileLookup].Last, [qryEmployees+DQFileLookup].Description, DQ_File.Attachment
FROM DQ_File RIGHT JOIN [qryEmployees+DQFileLookup] ON DQ_File.EmployeeNo = [qryEmployees+DQFileLookup].EmployeeCode
WHERE (((DQ_File.Attachment.FileURL) Is Null) AND (([qryEmployees+DQFileLookup].CURRENT)=True) AND (([qryEmployees+DQFileLookup].DRIVER)=True) AND (([qryEmployees+DQFileLookup].DQRequired)=True));
If you want to know which required docs employees do not have, then need a dataset of all possible combinations of employees/docs. Then match that dataset with DQ_File to see what is missing. The all combinations dataset can be generated with a Cartesian query (a query without JOIN clause) - every record of each table will associate with every record of other table.
SELECT Employees.*, DQ_File_Lookup.* FROM Employees, DQ_File_Lookup;
Then join that query with DQ_File.
SELECT Query1.EmployeeID, Query1.First, Query1.Last, Query1.ID, Query1.Title, Query1.DQRequired, DQ_File.Description, DQ_File.EmployeeNo
FROM DQ_File RIGHT JOIN Query1 ON (DQ_File.EmployeeNo = Query1.EmployeeID) AND (DQ_File.Description = Query1.ID)
WHERE (((Query1.DQRequired)=True) AND ((DQ_File.EmployeeNo) Is Null));
Advise not to use exact same field names in multiple tables. For instance, Title in DQ_File_Lookup could be DocTitle and Title in Employees could be JobTitle. And there will be less confusion if ID is not used as name in all tables.
It seems unnecessary to repeat Title and [Compliance Asset ID] in all 3 tables.
Strongly advise not to use spaces in naming convention. Title case is better than all upper case.

Inserting data into two different tables with SQL Server 2008?

I have three tables in my database: Contacts (master table), Users and Staff. The Contacts table has a primary key that is referenced as a foreign key from the Users and Staff tables.
Contacts
ContactID (Primary Key)
First Name
Last Name
Email
Users:
ContactID (Foreign Key)
Username
Password
Staff:
ContactID (Foreign Key)
Position
Comments
Pulling data from these tables is fairly simple with INNER JOIN and depends what data you need from the Users or Staff table. Updating and inserting new records is something that I'm not sure which way to approach.
Since the Contacts table is my master table and holds unique ID's but at the same time is shared between Users and Staff, I'm not sure about this situation.
Let's say I want to enter new user record. I need to enter First, Last name and email into the Contacts table, and User Name and Password into the Users table. At the same time I have to check if First, Last name and email already exist in the Contacts table, since maybe this Contact record has been previously entered for a Staff record.
I'm not sure how to prevent duplicates but at the same time there might be user or staff with the same name. Should I limit/filter by email and not let them enter the records if the email already exist in contacts table? Or is there a better way to handle this?
The whole point of having three tables is to prevent redundant data since User might be a Staff, but Staff might not be a User if that make sense. If anyone can help me pick the best approach please let me know. Thanks
You have to start by using a SELECT query and whatever business rules you want to test to decide if you will perform the INSERT at all, or UPDATE an existing record instead.
Then if you are going to INSERT, you have to INSERT into Contacts first, get the newly inserted ID, and then INSERT into the other two tables.

Assistance with part of a join

I'm a college student and the database I'm working with is purely fictional but part of it requires me to make a query that is a join.
What I have are 3 tables each with part of the data but also needing to use two of those tables as conditions based off the main table. What I mean is I have an employee table, order table and customer table and the only thing any two of them have in common is the ID of either the employee or the customer is part of the order table.
Now what I am trying to do is create a join statement that will get certain information from the employee and customer tables and only those that both the employee and the customer are also on the same line in the order table. How should i make this type of conditional statement?
Any example using the same basic scenario will work I can use that to help me build my own query.
This is what I have right now:
SELECT [Customer/Vendor_Info_local].Name_of_customer,
Employee_Info_local.Employee_Name
FROM Employee_Info_local,
[Customer/Vendor_Info_local],
Order_Information_local
WHERE (([Customer/Vendor_Info_local].[Customer/VendorID] =
[Order_Information_local].[Cu‌​srtomer/VendorID])
AND
([Employee_Info_local].[EmployeeID] = [Order_Information_local].[EmployeeID]));
I keep getting a type mismatch error when i try to use it and honestly not even sure what that means.

Merge and populate tables

I have 2 tables in MS Access, that are updated externally each day (any tables I add will be deleted when the copy arrives in the morning):
Current status of a customer
All customer errors
The current status of a customer includes all customers in a particular process, and shows which status they are in today. The "all customer errors" table shows the details of customers IF they had an error at some stage. Both tables have some fields in common, but not everything (so a simple union isn't possible).
I need some help to do the following:
Join the tables and create a column stating the initial table they came from. I realise I will have some duplicates.
Taking "current status of a customer" table, populate the missing data from "all customer errors"
create an extra column - "number of errors" where I count the number of times the customer appeared in the error table
Help!
My SQL skills are a bit basic, but improving each day :-)
Thanks
Kirstin
You must have a primary key and create an inner join. Use this formula in the SQL tab. You can then go into design view and use 'make table'. Simply select what data you want to appear.
'SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;'
When you have your new table you can update it to have new columns etc.
'UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;'