+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 23

Thread: For Next Loop Within another For Next Loop

  1. #1
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    For Next Loop Within another For Next Loop

    Hello,

    I have an insert statement

    Code:
    Dim SQL
    For SQL=1 to 10
    sql="INSERT INTO [performances] (showID, SeatNumber, SeatPrice, performance_time, ShowDate, ShowStatus)"
    sql=sql & " VALUES "
    sql=sql & "(" & ("showID") & ","
    
    For SeatNumber=1 to 10
    sql=sql & "" & ("SeatNumber") & ","
    Next
    
    sql=sql & "" & Request.Form("SeatPrice") & ","
    sql=sql & "" & Request.Form("performance_time") & ","
    sql=sql & "" & Request.Form("ShowDate") & ","
    sql=sql & "'" & Request.Form("ShowStatus") & "')"
    Next 
    
    'on error resume next
    conn.Execute sql,recaffected
    
    I want to run the insert code 10 times but i also want to run another loop which changes the SeatNumber for each insert.

    I am getting this error:

    Code:
    Microsoft VBScript runtime error '800a000d' 
    
    Type mismatch: '[string: "INSERT INTO [perform"]' 
    
    /insertShow.asp, line 65
    

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    VBScript is not case sensitive and you've used SQL for both your for next integer variable and the string to build your SQL statement, hence the type mismatch error.

    Change one of the variables to another name.

    You also don't execute the query inside the loop, so it would only call the SQL query once the value reaches 10 on both and would only insert 1 record.

  3. #3
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Also, you are (or would be) generating many more VALUES that you have fields in your INSERT statement with the above logic.

    This may be something better done with a stored procedure as well.

    [edit]
    Your VALUE statement is also going to contain the literal strings seatID and SeatNumber, which is probably not what you intended.
    [/edit]
    Last edited by Wolffy; January 20th, 2010 at 05:38 PM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    And another thought; if you need to have all of the INSERT statement work or none of them, you will need to do this inside of an SQL TRANSACTION -- otherwise you could have the first 9 inserts work, but the 10th fail, which might leave your database in an inconsistent state.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  5. #5
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Try this:

    Code:
    For x = 1 To 10
    	strSQL = "INSERT INTO [performances] (showID, SeatNumber, SeatPrice, performance_time, ShowDate, ShowStatus) " & _
    		"VALUES ( " & _
    		showID & ", " & _
    		x & ", " & _
    		Request.Form("SeatPrice") & ", " & _
    		Request.Form("performance_time") & ", " & _
    		Request.Form("ShowDate") & ", " & _
    		"'" & Request.Form("ShowStatus") & "')"
    	conn.Execute strSQL
    Next
    
    Last edited by BLaaaaaaaaaarche; January 21st, 2010 at 10:51 AM.
    "You'll never be as perfect as BLaaaaaaaaarche."

  6. #6
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by BLaaaaaaaaaarche View Post
    Try this:

    Code:
    For x = 1 To 10
    	strSQL = "INSERT INTO [performances] (showID, SeatNumber, SeatPrice, performance_time, ShowDate, ShowStatus) " & _
    		"VALUES ( " & _
    		showID & ", " & _
    		x & ", " & _
    		Request.Form("SeatPrice") & ", " & _
    		Request.Form("performance_time") & ", " & _
    		Request.Form("ShowDate") & ", " & _
    		"'" & Request.Form("ShowStatus") & "')"
    	conn.Execute strSQL
    Next
    
    Thats what i was thinking! but it seems to throw up a syntax error on this line

    Code:
    conn.Execute strSQL
    

  7. #7
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Response.Write(strSQL) and see what is being produced.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  8. #8
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by Wolffy View Post
    Response.Write(strSQL) and see what is being produced.
    nothing...

    Microsoft JET Database Engine error '80040e14'

    Syntax error in INSERT INTO statement.

    /insertShow.asp, line 62


    it crashes on the conn.execute strSQL

  9. #9
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Are you writing it out before you attempt to execute it?

    Replace the conn.execute statement with the response.write statement.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  10. #10
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by Wolffy View Post
    Are you writing it out before you attempt to execute it?

    Replace the conn.execute statement with the response.write statement.
    it doesnt seem to like the time.. hmm it should still work as its a text field...

    Syntax error (missing operator) in query expression '12:00'.

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. if loop problem
    By todd2006 in forum ASP Development
    Replies: 5
    Last Post: July 13th, 2009, 05:01 PM
  2. For Each loop in report
    By tuxalot in forum Microsoft Access
    Replies: 8
    Last Post: March 7th, 2009, 07:21 PM
  3. loop through a collection
    By Shem in forum .NET Development
    Replies: 15
    Last Post: October 17th, 2008, 11:54 AM
  4. Loop / Summary help
    By Rebelle in forum ASP Development
    Replies: 7
    Last Post: April 11th, 2008, 11:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO