Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkboxes and multiple delete question

Status
Not open for further replies.

777axa

Technical User
Jul 30, 2002
48
US
How can I delete multiple records ?
I'm not sure about what asp code should look like.
I'm sorry - I'm new to this.
Thanks
 
Let's suppose that your checkboxes all have the name car_id and the values are VINs and the used car lot manager checks off the cars he sold today and that means the car is deleted from his inventory table.

Upon submitting the form the program retrieves all the cars listed on the form into a recordset named rsCarsForSale.

So you loop through the Form items named car_id

Code:
 . . .
Dim adAffectCurrent
adAffectCurrent = 1

rsCarsForSale.MoveFirst

If Request.Form("car_id").Count > 0 Then

   FOR iCar = 1 to Request.Form("car_id").Count
   
      rsCarsForSale.Find "VehicleIDNumber = " & Request.Form("car_id")(iCar)
      rsCarsForSale.Delete adAffectCurrent
      rsCarsForSale.Update
      
      rsCarsForSale.MoveFirst

   Next

End If

 . . .

So these are the pieces. Not sure about putting the Update inside the loop; it may be possible to do the update one time. That might require a different value for the parameter used with the Delete.
 
I tested this and found that the Update is not needed.
The follwing CursorType and LockType will work.

Const adOpenDynamic = 2
Const adLockOptimistic = 3

rsCarsForSale.CursorType = adOpenDynamic
rsCarsForSale.LockType = adLockOptimistic

In my example it would be necessary to put the VIN in quote marks like so

Code:
rsCarsForSale.Find "VehicleIDNumber = '" & Request.Form("car_id")(iCar) & "'"
 
Thank you for your answer.
How do I put only checked boxes into an array? - so what would be a code for evaluating if the box is endeed checked?
Reagrds,
777axa
 
Are we still working on your original question,
"How can I delete multiple records?"

There is no need to put the checked boxes into an array because Request.Form("car_id") will only have the values for the boxes that were checked. They can be accessed similar to an array, Request.Form("car_id")(1),Request.Form("car_id")(2), etc.

The information that an item was checked exists in the document model on the client computer. So for example you can have a Javascript that tests whether a box is checked like this -

Code:
if( document.forms[0].elements[i].name == "car_id" &&
   document.forms[0].elements[i].checked == true ) {}

But that runs in the browser, not on the web server, and is not somethng that can be processed by ASP.


Your form must have checkboxes with VALUE attributes and all with the same NAME like this -
Code:
<input type=&quot;checkbox&quot; name=&quot;car_id&quot; value=&quot;12346&quot;>Blue Chevy
<input type=&quot;checkbox&quot; name=&quot;car_id&quot; value=&quot;7890&quot;>Red Ford
<input type=&quot;checkbox&quot; name=&quot;car_id&quot; value=&quot;8901234&quot;>Green Jaguar

Suppose the Chevy and the Jag are checked. Then this is the data that will be sent to the server.

car_id=12346,8901234

So you know that Chevy and Jaguar were checked because you got the car_id values for those two cars.
 
The code for evaluating that a checkbox was checked would be
Code:
//JScript
if(Request.Form(&quot;field_name&quot;) != &quot;&quot;) {
   //The box named field_name was checked.
}

Or
Code:
'VBScript
If Not IsEmpty( Request.Form(&quot;field_name&quot;) )  Then
   'The box was checked.
End If
In other words if you get a value for the field name then it was checked; if the value is empty then it was not checked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top