Solved

Insert number of rows depending on value in quantity

Posted on 2012-03-22
2
328 Views
Last Modified: 2012-08-14
Hi,

I need to insert records from one table to another depending on what the value is the Quantity field.

At the moment, it's only inserting one record for each line.  I would like to, if there is a value of more than one in the Quantity field, insert that number of rows.

Below is the code so far.

Many thanks :)

strSQL = "SELECT * FROM [Job Quotes Details] WHERE [SubID] = '" & nParentID & "';"
            objRS.Open strSQL, objConn
                  If Not objRS.EOF Then
                        Do Until objRS.EOF
                              strSQL = "INSERT INTO [Job Cost Sheets Details] ([Item],[Quantity],[Days],[Unit Price], Total, SubID) SELECT '"
                              strSQL = strSQL & Replace(objRS("Item"), "'", "''") & "', " & objRS("Quantity") & ", " & objRS("Days") & ", " & objRS("Unit Price") & ", "
                              strSQL = strSQL & objRS("Total") & ", " & nID & ";"
                              objRS2.Open strSQL, objConn
                              objRS.MoveNext
                        Loop
                  End If
            objRS.Close
0
Comment
Question by:Tra71
2 Comments
 
LVL 15

Accepted Solution

by:
pateljitu earned 500 total points
ID: 37752514
Please try this code:

strSQL = "SELECT * FROM [Job Quotes Details] WHERE [SubID] = '" & nParentID & "';"
objRS.Open strSQL, objConn
      If Not objRS.EOF Then
            Do Until objRS.EOF
                
                  objRSQuantity = objRS("Quantity")
                  
                  for i=1 to objRSQuantity
                      strSQL = "INSERT INTO [Job Cost Sheets Details] ([Item],[Quantity],[Days],[Unit Price], Total, SubID) SELECT '"
                      strSQL = strSQL & Replace(objRS("Item"), "'", "''") & "', " & objRS("Quantity") & ", " & objRS("Days") & ", " & objRS("Unit Price") & ", "
                      strSQL = strSQL & objRS("Total") & ", " & nID & ";"
                      objRS2.Open strSQL, objConn
                      objRS2.Close
                  next
                  
            objRS.MoveNext      
            Loop
      End If
objRS.Close

Open in new window

0
 

Author Closing Comment

by:Tra71
ID: 37752681
Great, thanks....
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

9 Experts available now in Live!

Get 1:1 Help Now