Asp/Access Php/MySql

Example of An Insert Into Query From Form and

Jet 4.0 Database Select @@Identity Query

Using VBScript, ASP, ADO and a OLEDB Connection

Prepared by Lon Hosford

 
Back to index
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<%
'
'
'   Purpose:      Example of Insert SQL Query using data passed from a form
'                   and capturing new autonumber value with 
'                   Jet 4.0 databases such as for Access 2000 
'
'   Version:      1.0.0
'
'   Date:         3/4/2004
'
'   Name:         WebClubMemberAddNewResponse.asp
'
'   Changes:
'      1.   Original programming
'
'   Programmer:      Lon Hosford   lon  @  hosfordusa.com
'
'
'

'
'  Use the ASP Server class CreateObject action to gain access
'  to the ADODB library on the web server to
'  create a connection object from the ADODB library
'  Connection class.
'  A connection object contains all data and actions for a database
'  connection via various techniques.
'  We are using the name oConn to represent our connection object.
'

Set oConn = Server.CreateObject("ADODB.Connection")

'
'  Use a variable to represent the connection string required
'  by the connection object. The variable is optional and
'  is a discretion to make futher lines in the code needing this
'  easier to read and maintain.
'  The connection string is made of three parts.
'      1. PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=
'      2. The server path to our Access database
'          Use the ASP Request object
'          ServerVariables("APPL_PHYSICAL_PATH")
'          to dynamically get the database path.
'      3. The name of the access database

ConnString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" 
ConnString = ConnString & Request.ServerVariables("APPL_PHYSICAL_PATH")
ConnString = ConnString  & "WebMaster.mdb"

'
'
'  Testing feedback of the ConnString variable contents.
'

Response.Write "<center>Connection String = " & ConnString & "</center>"

'
'  Use the Connection object open action to establish a connection
'  to the Access database.
'

oConn.Open ConnString


'
'  If needed uncomment next line to see any error messages
'  that might be suppressed.
'Response.Write err.number & "- " & err.description
'
'  Use a variable to represent the SQL statement.
'  The variable is optional and is a discretion to make further
'  lines in the code needing this easier to read and maintain.
'  SQL Insert query

SQL="INSERT INTO WebClubMembers "
SQL = SQL & "("
SQL = SQL & "FirstName, "
SQL = SQL & "LastName, "
SQL = SQL & "WebAddress, "
SQL = SQL & " EmailAddress "
SQL = SQL & ") "
SQL = SQL & "VALUES "
SQL = SQL & "('" 
SQL = SQL & Request.Form("FirstName") & "',"
SQL = SQL & "'" & Request.Form("LastName") & "',"
SQL = SQL & "'" & Request.Form("WebAddress") & "',"
SQL = SQL & "'" & Request.Form("EmailAddress") & "'" 
SQL = SQL & ") "

'
'  Testing feedback of the SQL variable contents.
'

Response.Write "<center>Insert SQL Statement = " & SQL & "</center>"

'
'  Use the Connection object execute action to submit the
'  SQL statement to the database and receive an ADO Recordset
'  object back with a pointer to the first record.

Set oRs = oConn.Execute (SQL)

' Special SQL Statement to get the new auto number value (sMemberNumber field)

SQL = "SELECT @@Identity"

'  Use the Connection object execute action to submit the
'  SQL statement to the database and receive an ADO Recordset
Set oRs = oConn.Execute (SQL)

%>

<html>
<head>
   <title>WebMasters Club Join Request</title>
</head>

<body>

      <table border = "1" align = "center">
         <tr>
            <td align = "center" colspan = "2">
               <b>WebMasters Club Join Response</b>
            </td>
         </tr>
         <tr>
            <td>
               <%= Request.Form("FirstName") %> 
               <%= Request.Form("LastName") %> was added.
            </td>
         </tr>
         <tr>
            <td>
               Record number: <%= oRs(0).Value %>.
            </td>
         </tr>
         <tr>
            <td>
               <a href = "WebClubMemberAddNewRequest.asp">Join Another</a>
            </td>
         </tr>
      </table>
<%

' Issue close action on ADODB RecordSet object
oRs.Close
' Release memory resources
' Done automatically when ASP processing ends
' but included as good programm form.
Set oRs = Nothing
' Issue close action on ADODB Connection object
oConn.Close
' Release memory resources
' Done automatically when ASP processing ends
' but included as good programm form.
Set oConn = Nothing

%>

</body>
</html>