Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Insert number of rows depending on value in quantity

Posted on 2012-03-22
2
Medium Priority
?
339 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
[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
2 Comments
 
LVL 15

Accepted Solution

by:
pateljitu earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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