Solved

Insert number of rows depending on value in quantity

Posted on 2012-03-22
2
336 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Designing forms 3 42
sql2016-WIn10: standard,for SQL servc-account.. 51 42
Using this function 4 40
Applying Roles in Common Scenarios 3 17
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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