Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Insert number of rows depending on value in quantity

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
Tra71
Asked:
Tra71
1 Solution
 
pateljituCommented:
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
 
Tra71Author Commented:
Great, thanks....
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now