Functions suitable for adding, deleting, and modifying all tables

Source: Internet
Author: User
< %
' ========================================================== ===
' Function: adds, deletes, and modifies any table in the database.
' Author: wangsdong
' Site: http://www.aspprogram.cn
'  ArticleFor Original Author, please indicate the source of the article and retain the author's information. Thank you for your support!
' It indicates that tablename is the table name, and STR values are insert, delete, and update.
' The statements to be executed are add, delete, and modify. ID is an automatically numbered field.
' Examples
' ========================================================== ===

Function Add_del_update (tablename, STR, ID)
Select   Case Str
  Case   " Insert " :
SQL = " Select * from [ " & Tablename & " ] Where id = NULL "
Rs. Open SQL, Conn, 1 , 3
Rs. addnew
For   Each Key in request. Form
RS ( CSTR (Key )) = Request (key)
Next
Rs. Update
Rs. Close
  Case   " Update " :
SQL = " Select * from [ " & Tablename & " ] Where id = " & ID
Rs. Open SQL, Conn, 1 , 3
For   Each Key in request. Form
If Key <> " ID "   Then
RS ( CSTR (Key )) = Request (key)
End   If
Next
Rs. Update
Rs. Close
  Case   " Delete " :
SQL = " Delete from [ " & Tablename & " ] Where ID in ( " & ID & " ) "
Rs. Open SQL, Conn, 1 , 3
  Case   ""
  End   Select
  End Function
% >

Prerequisites: The table must have an automatically added field with the field name ID. The form name before submission must be the same as the field name in the database, and the name attribute cannot be used as a button.

Use Case:

< %
' Add a record to Table 1
  Call Add_del_update ( " Table1 " , " Insert " , "" )
' Modify the id = 5 (the ID is placed in the previous action or a hidden form) record in table 1.
ID = Request ( " ID " )
Call Add_del_update ( " Table1 " , " Update " , ID)
' Delete some records in table 1. The ID is in the previous action or a form.
ID = Request ( " ID " )
Call Add_del_update ( " Table1 " , " Delete " , ID)
% >

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.