Classes that automatically get table data to add and modify database data

Source: Internet
Author: User
Tags datetime numeric numeric value table name access database
Data | Database This kind of personal thought is very useful!!! For the later submission of the form data, do not have to write a single one, directly referencing the class, then save or edit can be more simple, but of course, because it is not intelligent, so for some data format is not to do verification!! Use the sample code as follows:
<%
Dim Action
Action=request ("Action")
If action= "Save" Then
Dim Objclass
Set objclass=new Saveformdata
Objclass.mdbfile=server.mappath ("Data/data.mdb")
' Objclass.saverecord ' number, ' * ', ' ID '
Objclass.saverecord "Number", "Num1,text", ""
' Objclass.editrecord ' number ', ' * ', ' id=15 ', ' ID '
Set objclass=nothing
End If
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"Http://www.w3.org/TR/html4/loose.dtd" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Untitled Document </title>

<body>
<form name= "Form1" method= "Post" action= "Index.asp?action=save" >
<table width= "100%" border= "0" cellspacing= "2" cellpadding= "0" >
<tr>
&LT;TD width= "19%" height= ">Num1</td>"
&LT;TD width= "81%" ><input name= "NUM1" type= "text" id= "NUM1" ></td>
</tr>
<tr>
&LT;TD height= ">Num2</td>"
<td><input name= "num2" type= "text" id= "num2" ></td>
</tr>
<tr>
&LT;TD height= ">Num3</td>"
<td><input name= "num3" type= "text" id= "num3" ></td>
</tr>
<tr>
&LT;TD height= ">Text</td>"
<td><input name= "text" type= "text" id= "text" ></td>
</tr>
<tr>
&LT;TD height= > </td>
<td><input type= "Submit" name= "Submission" value= "submitted" ></td>
</tr>
</table>
</form>
</body>




Description of Class:

If you are using an Access database, you first specify the absolute address of the database! [OBJ]. mdbfile= "Database file Address"
If it is not an Access database, specify the Conn property of the Class object, example: Set [OBJ]. Conn=[connection Object]

You can then use
[OBJ]. Saverecord ("Table name", "Fill in the list of field names", "Do not fill in the list of field names") = Add new data
Note: This method returns TRUE or False,true new data is successful or fails!
Table name: Data table name to fill in the new record data
Fill Field Name list: field names when new data is added, separated by commas, such as: "Name,word,sex"
"Do not fill in field Name list": The fields included in this field list are not added when data is added.
[OBJ]. Saverecord "User", "*", "id"

[OBJ]. EditRecord (table name, modify field names list, modify criteria, do not modify field list) = Modify a piece of data
"Table name", "Modify Field List" and "Do not modify field list" are the same as the Saverecord Method!
"Modify condition": SQL query condition, without "WHERE"
Example: [OBJ]. EditRecord "User", "Pass,sex", "id=22", ""

Note: The Fill field List and modify field list can use the value or the "*" value, indicates that all fields are modified, but it is recommended that you write "Automatically add" field names in the "Do not fill in field Name list" and "Do not modify field names list", or you will fail to fill in or modify data!


Class Code:
<%
'/*****************************************************************************************
Class Saveformdata
Private c_errnumber ' ERROR return code
Private c_numericallownull ' Numeric field is allowed to receive non-numeric values, True = Can (Non-numeric value with 0 in lieu of) False = Can not (non-numeric value will be wrong, Errnumber = 9)
Private C_datetimeallownull ' Date-time field is allowed to receive non datetime, True = Can (now) instead of datetime value False = Cannot (non datetime value will be wrong, Errnumber = 10)
Private c_conn ' Database Conn Connection object
Private c_mdbfile ' Access database file address, if not set Conn property, this property must not be empty!

Private Sub Class_Initialize
C_conn=null
C_numericallownull=true
C_datetimeallownull=true
End Sub

Private Sub Class_Terminate
If not IsNothing (c_conn) Then
C_conn.close
Set c_conn=nothing
End If
End Sub
'/***************************************************************************
'/* Set/Return Conn Object
'/* Description: Add this for other databases (e.g., MSSQL)
'/***************************************************************************
Public Property Set Conn (Snewvalue)
On Error Resume Next
If not IsNothing (snewvalue) Then
C_conn.close
Set c_conn=nothing
End If
Set C_conn=snewvalue
End Property
Public Property Get Conn
If not IsNothing (c_conn) Then
Set Conn=c_conn
Else
Conn=null
End If
End



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.