Solved

Easy way to update products quantity in Invoice to Stock with SQL command

Posted on 1998-10-23
4
317 Views
Last Modified: 2013-12-25
Currently, I am developing a commercial app with VB5. I want to update the quantities in the stock table when the user confirms a sale in the invoice, but would like to know if there is a simple way to do this with the SQL command, not using Find or Seek command with Looping where the programmers usually do.

Please give me an example with coding.

Thanks.
0
Comment
Question by:poodle
4 Comments
 
LVL 1

Expert Comment

by:CarlosJac
ID: 1497169
Dim Sql as string
Dim DB as Database

Set DB = CurrentDatabase()
Sql = "UPDATE Stock SET Quantity = Quantity - 15 WHERE ProductID = 1"
DB.Execute Sql



0
 

Author Comment

by:poodle
ID: 1497170
Thanks for your answer, but I already knew the command that you've send it to me.

The example that you wrote will update only one product which productID = 1, but if the invoice had 3 different product IDs like 1,2 and 3, I have to make a looping 3 times to update each products...... because in real bussiness, one client can buy several DIFFERENT products in a single store at a time as below:

=============================================================
INVOICE:00001                                 DATE:10/25/1998
CLIENT:John Smith
-------------------------------------------------------------
ProductID    Description       Unit Price     Qty.      Total
-------------------------------------------------------------
       1     HB Pencil           1.00          3         3.00
       2     Notebook            3.50          1         3.50
       3     PC Disquete         1.50         10        10.50
-------------------------------------------------------------
                                              Subtotal: 17.00
=============================================================

The invoice example as above, the client John Smith bought 3 different products. There are three tables involved: "Invoice", "InvoiceProducts" and "Stock". The "Invoice" table has the one to many relation with the "InvoiceProducts" table.

How can I update these products to the stock table with single line SQL command? Is there a way to do it or it must use the looping command? Because by my logic, I think there must be a way to make a recordset combining the "InvoiceProducts" and "Stock" table then update the stock similar to the command you wrote.

Please Help!!!




0
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1497171
It looks like you want to update the database's information all at one time. Have you thought about using database transactions? I have used transactions in situations like this so the update happens all at once, or is cancelled as a group. This allows you to loop through all the table updates before commiting the transaction.

Sample Code:

Dim WS As Workspace
Dim DB AS Database
Dim Rst As Recordset

Set WS = DBEngine.createWorkspace("", "Admin", "")
Set DB = WS.OpenDatabase("C:\mydata.mdb")
Set Rst = DB.openrecordset("tblStock")

Rst.index = "PrimaryKey"

WS.BeginTrans


for each line in orderlines
  Rst.Seek "=", LineStockID
  Rst.Edit
  Rst!fldQty = Rst!fldQty - 5
  Rst.update
next line

WS.commitTrans


This will allow you to perform data operations between the WS.Begintrans and the WS.commitTrans and execute them together as a group.

-Note- In the case of an error you can use WS.rollback to cancel a transaction.

I hope this works for you!
0
 
LVL 3

Accepted Solution

by:
TheAnswerMan earned 50 total points
ID: 1497172
why cant you do this>
Set DB = CurrentDatabase()
Sql = "UPDATE Stock SET Quantity = Quantity - 3 WHERE ProductID = 1" _
&  "UPDATE Stock SET Quantity = Quantity - 1 WHERE ProductID = 2" _
&  "UPDATE Stock SET Quantity = Quantity - 10 WHERE ProductID = 3"
DB.Execute Sql
 
DB.Execute Sql

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now