 
DELETE multiple records |
Dreamweaver / ASP |
How to delete multiple records using a series of checkboxes.
Example
Explanation
This is actually very simple. First create a recordset to retrieve the fields
you wish to display and the record identity field.
Create a table within a form and drag the dynamic text elements into the relevant
fields, in one of the columns put a checkbox. Now set the value of the checkbox
to that of the record identity field from your recordset. Add a Submit button
to the form, now use Dreamweaver's Repeat Region server behaviour to create a
list of records.
If you look at the page in code view it should look something like this:
<%
While ((Repeat1__numRows <> 0) AND (NOT recordsetName.EOF))
%>
<tr>
<td><%=(recordsetName.Fields.Item("Field1").Value)%></td>
<td><%=(recordsetName.Fields.Item("Field2").Value)%></td>
<td><input type="checkbox" name="checkboxName"_
value="<%=(recordsetName.Fields.Item("RecordIdentity").Value)%>"></td>
</tr>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
recordsetName.MoveNext()
Wend
%>
Note that when the page is viewed in the browser each checkbox will have the
same name, so when the form is submitted and we retrieve the values using Request("checkboxName")
we will have a comma delimited list of all the values of the checkboxes that
were ticked, something like this: 2, 5, 11, 15 etc. which we can use for a delete
command.
Use the Dreamweaver Command behaviour to create a DELETE command. Click the +
symbol on the server behaviours palette and select Command. Name your command,
select the connection and select DELETE from the Type list. Dreamweaver can assist
you in building the delete command, click here
for more
information, or you can type the command directly in the SQL box. Your DELETE
command should look something
like this:
DELETE FROM TableName WHERE RecordID IN (intRecordIDs)
You will need to add a variable, name it what you wish, I have named it varRecordIDs,
and set up a run-time value of Request("checkboxName")
Now if you look at the page in code view you should see the delete command, something
like this:
<%
if(Request("checkboxName") <> "") then commDelete__intRecordsIDs_
= Request("checkboxName")
%>
< %
set commDelete = Server.CreateObject("ADODB.Command")
commDelete.ActiveConnection = MM_YourConnection_STRING
commDelete.CommandText = "DELETE FROM TableName WHERE RecordID_
IN (" +
Replace(commDelete__intRecordsIDs, "'", "''") + ")"
commDelete.CommandType = 1
commDelete.CommandTimeout = 0
commDelete.Prepared = true
commDelete.Execute()
%>
The command will run as soon as the page loads, to prevent this place it within
a
conditional
region
at the top of the page, click here for more
information. |