how to create a delete loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jasone
    New Member
    • Mar 2007
    • 66

    how to create a delete loop?

    Hi ive got checkbox options going to a page and then being listed, i now want to delete what has been selected, im guessing this needs to be done through some kind of loop?

    the code i have so far on the delete page is as follows :

    Code:
    <%
    Dim flight_id
    flight_id = Request.Form("flight_id")
    
    if flight_id="" then
       Response.Write "You did not select a name to delete!" 
    Else
    
    SQL="DELETE * FROM tbl_flight_details WHERE flight_id ='" & flight_id
    
     Set MyConn=Server.CreateObject("ADODB.Connection")
    MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\ddwassignment3\airlines.mdb"
    
    Set MyRS=Server.CreateObject("ADODB.RecordSet")
    MyRS.Open SQL,MyConn
    
    end if
    %>
    
    </p>
          <table width="37%" border="0" align="center">
            <tr>
    <td align="center"><% Response.Write("<font face=Calibri>You have deleted flight number <B><font face=Calibri color=Red>" & flight_id)%>
    the last line lists what has been selected.

    please could you suggest how to implement aloop into the delete and if this should work or not?

    kind regards jason
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    You have to pass ALL your flight_ids that were checked. Not just one.

    Then loop through all those flight ids in a For Next loop letting X be the flight id you are wanting to delete.

    The problem here is you also need to determine which ones are checked or not. So as your looping through ALL checkboxes you also need to do a check on each value before executing the SQL.

    I put in the basic idea below in code. Also here is a link that talks about checkbox arrays.


    You will NOT be able to get around doing an array like this for what you are describing.

    Originally posted by jasone
    Hi ive got checkbox options going to a page and then being listed, i now want to delete what has been selected, im guessing this needs to be done through some kind of loop?

    the last line lists what has been selected.

    please could you suggest how to implement aloop into the delete and if this should work or not?

    kind regards jason

    Code:
    <%
    Dim flight_id
    flight_id = Request.Form("flight_id")
    
    Set MyConn=Server.CreateObject("ADODB.Connection")
    MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data      Source=c:\inetpub\wwwroot\ddwassignment3\airlines.mdb"
    
       for x = LBound(flight_id) to UBound(flight_id)
          if flight_id(x) <> "" then
               SQL="DELETE * FROM tbl_flight_details WHERE flight_id ='" & flight_id(x)
    
           '     you aren't opening a recordset, you are executing an SQL action
           '     so you dont need the recordset, just this
    
                 MyConn.Execute SQL
          end if
       next
    
    %>
    
    </p>
          <table width="37%" border="0" align="center">
            <tr>
    <td align="center"><% Response.Write("<font face=Calibri>You have deleted flight number <B><font face=Calibri color=Red>" & flight_id)%>

    Comment

    • jasone
      New Member
      • Mar 2007
      • 66

      #3
      many thanks for your response, though im getting the error :

      Type mismatch: 'LBound'
      /ddwassignment3/delete_tester.a sp, line 8

      Comment

      • jasone
        New Member
        • Mar 2007
        • 66

        #4
        problem solved... though i did it a different way, i will post the working script here shortly... a bit of work to do first

        Comment

        • jeffstl
          Recognized Expert Contributor
          • Feb 2008
          • 432

          #5
          Originally posted by jasone
          many thanks for your response, though im getting the error :

          Type mismatch: 'LBound'
          /ddwassignment3/delete_tester.a sp, line 8
          Yeah. Likely the variable isnt being passed as an array. You would have to basically name each checkbox according to its id or something and loop through them to build an array.

          Like this looping through all the records for you display page writing a checkbox for each one.

          Code:
          While Not RS.EOF 
          response.write "<input type=checkbox name=chkFlight" & flight_id & ">"
          Wend

          Then like this on the other side. With loop through the same recordset that was being display on the previous page (While Not RS.EOF)

          Code:
          While Not RS.EOF
          'Checking each checkbox to see if its checked then adding it to the array
          if (chkFlight & RS("flight_id")) = 1 then
               FlightIDArray(x) = RS("flight_id")
          end if
          
          Wend
          Then when your done with that you loop through the FlighIDArray with that for next loop using the DELETE sql.

          This is why I was thinking it would be much easier to have a delete link next to each record and just let them delete one at a time but at a very fast pace.

          Comment

          Working...