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
Medium Priority
339 Views
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
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

LVL 15

Accepted Solution

pateljitu earned 2000 total points
ID: 37752514

``````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
``````
0

Author Closing Comment

ID: 37752681
Great, thanks....
0

## Featured Post

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…
###### Suggested Courses
Course of the Month9 days, 22 hours left to enroll