Access Update on Query Table - sql

I'm currently in the process of converting an access database to SQL. While going through it, I found this.
Update EmployeeCustomerOrderDetail set Valid = -1;
This is strange because EmployeeCustomerOrderDetail is a query, defined below.
Select *
From Employee
inner join Order on Employee.EmployeeID = Order.EmployeeID
inner join Customer on Order.CustomerID = Customer.CustomerID
inner join OrderDetail on Order.OrderID = OrderDetail.OrderID
I thought that this achieved nothing since an Update on a Select wouldn't work, but I tested it with
Update (Select * From Employees) as Emp set Emp.WorkPhone as 'Random Value'
And it worked.
My question then, is how is this processed. Does the update traverse backwards through all query tables until it finds a dataTable? Will it change the value of a reference column, or the actual data in the associated table?

This only works with updateable recordsets. It's the same as just changing a value directly in the Access query.
In an updateable recordsets, each field in the dataset (query) is directly bound to a field in a table. This means updates directly affect the underlying tables of a query. You can review this answer for rules about which recordsets are and aren't updateable.
Another really contra-intuitive thing about this is that recordsets with outer joins can be contain updateable unmatched records. In this case, a record is created in the table that doesn't contain a matched record, then a new one gets created.

Related

Using a record to choose a field to update a query shows a popup

UPDATE tblInstance
(INNER JOIN Master_Table ON tblInstance.[WorkOrder] = Master_Table.[Work_Order_number])
INNER JOIN tblCustomer_New ON Master_Table.[Customer_Name] = tblCustomer_New.[Customer_Name]
SET tblInstance.to_test_date = [Master_Table].[tblCustomer_New.Inital_Invoice];
I use a record from one table to choose a field in another table to update a record in a third table. I have all the table relationships set within Access.
Edit:
A value box pops up when trying to run it. It isn't pulling any data from the master table.
When MS Access does not find a certain identifier in your query it shows a value box asking for the value. See: Why does Access want me to enter a parameter value?
To solve this you can take the following steps:
Look at the parameter name it is asking for; it could be that the column name or a table name is incorrectly written. If you see an incorrect name which is not in the database, fix it.
Otherwise try the same query but as a select statement:
SELECT *
FROM tblInstance
(INNER JOIN Master_Table ON tblInstance.[WorkOrder] = Master_Table.[Work_Order_number])
INNER JOIN tblCustomer_New ON Master_Table.[Customer_Name] = tblCustomer_New.[Customer_Name];
And then try just the subqueries, starting with, for example:
SELECT *
FROM tblInstance
(INNER JOIN Master_Table ON tblInstance.[WorkOrder] = Master_Table.[Work_Order_number])

Cannot update a table using a simple inner join

I have 2 tables in access 2007.
See attached picture to see the structure of the tables and the expected result.
I am trying to update the quantity field (ITQTY) in TABLE_BLNC by summarizing all the quantity field (LOCQTY) from TABLE_DTL for same items (LOITNBR=ITNBR).
In TABLE_BLNC, the item is unique while in TABLE_DTL, the item can be in multiple records.
My query is:
UPDATE TABLE_BLNC INNER JOIN
(
SELECT LOITNBR, Sum(LOCQTY) AS SumOfLOCQTY FROM TABLE_DTL GROUP BY LOITNBR) AS DTL
ON TABLE_BLNC.ITNBR=DTL.LOITNBR SET TABLE_BLNC.ITQTY = DTL.SumOfLOCQTY;
I am getting the error:
Operation must use an updateable query.
Domain Aggregate functions can be useful when Access complains that an UPDATE is not updateable. In this case, use DSum() ...
UPDATE TABLE_BLNC
SET ITQTY =
DSum("LOCQTY", "TABLE_DTL", "LOITNBR='" & ITNBR & "'");
Index TABLE_DTL.LOITNBR for optimum performance.
One of the great annoyances of Access SQL is its inability to update a table from an non-updatable source. Non-updatable sources include read-only links to ODBC tables, and GROUP BY (summary) queries.
What I always do is:
Copy the structure of TABLE_BLNK to a temp table: TABLE_BLNK_temp.
In your code, first delete the temp:
DELETE * FROM TABLE_BLNK_temp;
Insert the result of your summary query into temp:
INSERT INTO TABLE_BLNK_temp (ITNBR, ITQTY)
SELECT LOITNBR, Sum(LOCQTY) AS SumOfLOCQTY
FROM TABLE_DTL GROUP BY LOITNBR;
Update TABLE_BLNK from TABLE_BLNK_temp:
UPDATE TABLE_BLNC INNER JOIN TABLE_BLNK_temp AS t
ON TABLE_BLNC.ITNBR = t.ITNBR
SET TABLE_BLNC.ITQTY = t.ITQTY;
While it is an extra step or two, this approach:
Always works
Is more performant than Domain Aggregate functions for larger datasets

Delete Duplicate Row sql oledb in vb.net

I am using this sql statement to join two tables together where the return_date in one table is null.
SELECT
Customers.CustomerID, Customers.First_Name,
Customers.Surname, Customers.House_No,
Customers.Street, Customers.County, Customers.Postcode,
IssueBook.Return_Date
FROM
Customers
INNER JOIN
IssueBook ON Customers.CustomerID = IssueBook.CustomerID
WHERE
((IssueBook.Return_Date IS NOT Null));
This statement is then used to fill a datagrid on a form. However when I view the datagrid they're could be duplicated rows as the customer may have taken more than one book out( just so you know I'm creating a library system). This means there is duplicated data. I know I can do a SELECT DISTINCT statement and delete the other rows and put the DISTINCT rows back in but I'm not sure how to do this as can I use a DISTINCT statement on the datagrid or will it have to be on the INNER JOIN sql statement? Please help?
Just put DISTINCT in the SQL query right next to the SELECT. You should have no problems as the DataGridView is GUI object from .NET and is completely independent from the database. It can show any tabular data you provide to it, whether it contains duplicates or not.
SELECT DISTINCT
Customers.CustomerID, Customers.First_Name,
...

Query with multiple INNER JOINS, difficulties in avoiding duplicates

I have an ACCESS BD with a main table ('Principale') having a many-to-many connection with another ('TabRegisti'), with a join table between them ('TabRegisti_Unione').
I've a form for 'Principale' where there's a Search Button that, after the user click, creates a query for a listbox in another form.
The query that I'm using is the following:
SELECT Principale.ID, Principale.CodiceDVD, Principale.Cofanetto,
Principale.TitoloDVD, TabRegisti.NomeRegista, TabRegisti.CognomeRegista,
TabCategoria.Categoria, Principale.Durata, Principale.AttoriPrincipali,
Principale.Produzione, Principale.AnnoDVD, Principale.AnnoFilm,
Principale.DataACQ, Principale.Ubicazione
FROM TabRegisti
INNER JOIN (TabCategoria
INNER JOIN ((Principale INNER JOIN TabUbicazione
ON Principale.Ubicazione = TabUbicazione.Ubicazione)
INNER JOIN TabRegisti_unione
ON Principale.ID = TabRegisti_unione.IDrifPrincipale)
ON TabCategoria.CategorieID = Principale.CategorieID)
ON TabRegisti.IDRegista = TabRegisti_unione.IDregistaUnione
WHERE (((TabRegisti.NomeRegista) Like "*anc*")
AND ((TabRegisti.CognomeRegista) Like "*"))
ORDER BY Principale.CodiceDVD;
I'm using the query, with the correct syntax, in VBA (the Where clause showed is just an example, of course).
It works fine but leaves duplicates of the main table's records.
I've tried to insert the clause DISTINCT and, after, the clause DISTINCTROW, but nothing chanced (duplicates are still there).
Otherwise, the use of the clause GROUP BY (i.e. 'GROUP BY Principale.CodiceDVD' before the clause ORDER BY) gives an error. Suggstions?
Hope the explanation of the problem is clear enough (I'm not english motherlanguage, so be indulgents).
DISTINCT will show only rows with distinct values in your selection fields. Your selected fields row must not be actually identical. You need to remove from the SELECT fields the once that you probably don't need and that generate a row that you don't want to see.

Update Query from a Lookup Query

I have a spreadsheet that I am converting to an Access DB. I have a column of typed out customer names that I want to replace with the appropriate customer number from our accounting system.
I have created a table with the customer info, and a query that shows what ID needs to be inserted into the source data. What I'm looking for is:
UPDATE tblStarting_Data
SET CustomerID=x
WHERE TEMPCustomer=y
Where X and Y come from qryIDPerCustomer.
Can I use a loop? How do I reference another query?
Another possibility in MS Access (object names borrowed from Tomalak answer):
UPDATE tblStarting_Data, qryIDPerCustomer
SET tblStarting_Data.CustomerID=qryIDPerCustomer.CustomerID
WHERE tblStarting_Data.TEMPCustomer=qryIDPerCustomer.CustomerName
I think a JOIN will help you:
UPDATE
tblStarting_Data AS sd
INNER JOIN qryIDPerCustomer AS qc ON sd.TEMPCustomer = qc.CustomerName
SET
sd.CustomerID = qc.CustomerID;
This can be expressed as a correlated sub-query as well (though the join syntax is preferable):
UPDATE
tblStarting_Data
SET
CustomerID = (
SELECT CustomerID
FROM qryIDPerCustomer
WHERE CustomerName = tblStarting_Data.TEMPCustomer
)
No need for a loop, both statements will update all records in tblStarting_Data in one step.