How can you find the number of cells changed from an SQL UPDATE statement? - sql

Say you have the following table:
If the image doesn't load, the table contains car names, their brand and whether they're in a sale
Now, let's say the car dealership decides to put all Skodas and Minis on sale, this SQL command can be used:
UPDATE cars SET InSale=True WHERE Brand in ("Skoda", "Mini")
Words in code are me trying to put ` around them
Which works. However, it would be useful to return how many cells changed value, so in this case 3, because the Rapid and Countryman were already in the sale, so they don't count
Is there any command I can run in/after the SQL above to return cells changed? The only idea I have is using some form of COUNT command, but I'm not sure how to implement such a thing

You can use SQL%ROWCOUNT to get the count of records updated by your UPDATE statement.
You may have to modify your UPDATE statement so that correct number is captured by SQL%ROWCOUNT.
UPDATE cars SET InSale=True WHERE Brand in ("Skoda", "Mini") and not InSale

Related

SQL Database "Operation must use an updateable query" Workaround

So my basic goal is to create a database for a shopsystem, which is my task to do for my IT course. I tried to create a UPDATE-Query, that collects all the Sale Positions ("tblPosition.PositionAnzahl") ordered with a SELECT-Query and groups it by the products ordered, to have an overview about how often each product has been sold.
I want to do this to keep track of how many items are still left in the inventory.
The Query was supposed to update 1 field ("tblArtikel.ArtikelVerkauft") in my table "tblArtikel", in which all my articles and their information is stored.
However, i just found out that you cannot run UPDATE-Queries, that use SELECT-Query data, as i get a error, that says "Operation must use an updateable query".
This is the code i used for the query:
UPDATE tblArtikel as a JOIN
(SELECT p.PositionArtikelID, Sum(p.PositionAnzahl) AS SumOfPositionAnzahl
FROM tblPositionen as p
GROUP BY p.PositionArtikelID
) p
ON a.ArtikelID = p.PositionArtikelID
SET ArtikelVerkauft = p.SumOfPositionAnzahl;
Is there another way to keep track of all the Items left in my inventory, apart from doing what i did?
Here are screenshots of the 2 tables (the depending fields are circled red):
tblPositionen with field PositionAnzahl
tblArtikel with field ArtikelVerkauft
I have not worked with SQL before and only learned about it during 45 min, so ther emight be an easy way for this, but i would still appreciate every answer from you guys.

How do I set up a MS Access form to create a new row in table and set one field as a default value?

So I am not positive I am even asking the correct question, but here it goes. I currently have a MS Access Form built so that someone can enter in a new work order. You are able to set the company, the part number wanted, quantity, and the Work Order number is an auto generated value that I use for my primary key. All of that works great and successfully adds a new row to the table "Work Orders". However, when this form is used to create a new work order I want the last field in the table "Work Orders" which is called "Status" to be set to "Not Started".
I successfully made an update query that asks for the Work Order Number, and will set the "Status" field to "Not Started". Here is the code for that:
UPDATE 03A_WorkOrderList
SET 03A_WorkOrderList.Status = "Not Started"
WHERE ((([03A_WorkOrderList].WO_Num)=[WO_Num:]));
If you give the update query work order number everything works great and the "Status" field is updated.
So back to the form I decided to attach the update query to the build event where the update happens after the new line is created. That seems to have worked too except it asks for the work order number. I totally understand why because it is the code that is in the update query of : WHERE ((([03A_WorkOrderList].WO_Num)=[WO_Num:]));
What I cannot figure out is how to have it pull the work order number that was automatically generated and use that for the update query.
If I am going about this all wrong, please let me know. TIA.
In Ms Access, open your [Work order] table in design mode. Select the Status field and in the property section below, you can set the Default value to be Not started. This way you don't need to perform an update and all new orders will automatically have Not started status.
or in your add order form, On before update event, you can set the status = 'Not started'
I was able to get the functionality that I was looking for with the following UPDATE Query:
UPDATE 03A_WorkOrderList SET 03A_WorkOrderList.Status = "Not Started"
WHERE [03A_WorkOrderList].WO_Num=(SELECT MAX(WO_Num) FROM 03A_WorkOrderList);
Since the WO_Num is auto generated and I want to edit the very same one I was working on, I could just look for the MAX(WO_Num). I then made this UPDATE query apart of the build event of the order form.

MS Access manual Auto incrementing field

Im building a system for my company to keep track of internal orders, inbetween our warehouses, we have material that goes out warehouse 1 to warehouse 2 and we kind of lose track of how much of "x" is in warehouse 1 and how much in warehouse 2, so i want to implement this access db where a user fills a form and says: order 1: 500 of "x" order 2: 300 of "y". then another user fills an exit form where he says 1 of "x" going out, so i would need the program to keep track of total order and how much as gone out to fill order 1 and so on...
My idea here is to have both an order number and an id number for each of "x" everytime someoneone assembles 1 "x" they fill the form and print a label directly from the access (i have this part working already) while keeping a record of when it was assembled, who verified and what was verified (it will work as a quality control also).
What i dont know is how to program the db so when it reaches 500 of "x", the id number for "x" starts again from 1
This is the one major issue with my program right now, i'm not experienced in access db's or vba, but im getting there with a tip and a trick from here and there, so, no need to be careful with the technical language, i will google it if i have to :p
EDIT:
The table structure goes as follows:
1 table as the main table where I record the check that is made for every product, where I include the model of the product, the said ID that I want to reset after a number of products checked, and a concatenated field that includes most of this information to generate a qr code.
Then there is a table for the Order Number, which is connected to a form to record each new order with a date/time field, the order number itself and the number of products. This number of products must then be called from the code that will count how many products have been checked to date and keep the order number field updated so we can keep track of the order.
Then there is another minor table just to get values for the form, the product models
Thank you for your answers ;)
See this MSDN Documentation
Unfortunately in Access, you cannot 'reset' an ID field, unless you move the records to a newly created table and use that table for every 500 records.
As for the user control and login form, I'm afraid those are separate questions that must be asked in a different thread.
To get you started:
You can set the RecordSource of a form to a table, and when users make entries, the data will be saved to the table. You can also use a form with controls (text boxes, comboboxes, etc.) and create a button that runs a query to insert these records into a table.
The login piece - you can encrypt the database with a password. That may/may not be sufficient.
I would suggest you change your schema, if possible. Something like the following:
Orders
OrderID (Autonumber)
ProductID (link to your Products table)
QuantityRequested
Deliverables
DeliverableID (Autonumber)
OrderID (link to your Orders table)
SequenceNumber: in the BeforeInsert event set this value equal to:
DCount("*", "Deliverables", "OrderID=" & Me.OrderID) + 1
I'm assuming that your form has a control named OrderID that is bound to the OrderID field of the Deliverables table.
The code uses the DCount() function to get the count of all the other deliverables that have already been created for this order. If this is the first deliverable, DCount() will return 0. It then adds 1 to this count to get the sequence number of the next deliverable.
If the new SequenceNumber is greater than the quantity requested, you could display a message saying that the order has been filled and cancel the creation of the Deliverable record.
This is just one approach and it is not a complete solution. I'm assuming that once assigned a sequence number a deliverable cannot be deleted. You might need to make allowances for deliverables that get lost or damaged. You could incorporate a status field to the Deliverable table to deal with this, but you would still need to make a decision about what to do with the SequenceNumber.

VBA, SQL, Queries

I have an access form with few controls on it, like start-date, end-date, move_type, mover_name etc, when user fills this field n clicks on query command button, a select query is run and it fetches records from various tables depending upon the criteria mentioned by the user.
Later the same records are exported to excel file and a report is generated for user.
Now I need to do more with this, my select query has a field "quoted-price", depending upon the price quoted the records need to be sorted out.
EX : if quoted_price < 500 then it is a "domestic apartment1", if quoted-price is >500 And <1500 it is "domestic Apartment1" etc.
Now I need to do more with this, my select query has a field "quoted-price", depending upon the price quoted the records need to be sorted out.
EX : if quoted_price < 500 then it is a "domestic apartment1", if quoted-price is >500 And <1500 it is "domestic Apartment1" etc. and when all the records generated through select query are searched on this criteria, the records need to be grouped accordingly. Later the count for individual category is multiplied by a unit value and total charge is calculated.
I tried building many queries to do this and tables to refer to value but of no use. Any help will be much appreciated.
are you looking to create a new field depending on the condition? That may be step one of what you are trying to do, If so that is pretty easy. See here: http://allenbrowne.com/casu-14.html

SQL query in Access

Basically, I've been trying to make this query work for a while in Access and it's really frustrating me so instead of playing around with the criteria, I've decided to just do it in SQL instead but I can't quite figure out how to do this bit.
What I need to do is create a query that shows which members haven't returned an item that they're currently taking out on loan. If possible I'd like to include a calculated field to state the date is was due back and how many days late it is.
The fields I'm using are as follows;
Table = Loan
Toy Name
Hire Date
Duration (in days)
Returned Date (if it hasn't been returned, the cell is blank)
Table = Toy
Purchase Price
Hire Price
you wrote:
a query that shows which members haven't returned an item
your table does not have members in it. secondly table toy have nothing related to toy like toy name or something. Please provide further detail of tables.
one more question what is the meaning of blank? Is it Null or blank string?