Solved

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

Posted on 1998-10-23
4
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

751 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