These are some easy ASP vbscript examples for manipulating data in a database

The ASP pages must be in the same directory as the database for these examples to work

Insert Function Sample

This example writes a new record to the Topic and Question fields in the Topics Table from information on a form page

<%

' Dimension your variables

Dim sTopic
Dim sQuestion
Dim sSql
Dim Conn
Dim ConnStr

Set conn = Server.CreateObject("ADODB.Connection") ' Create your connection object
connStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("YourDatabase.mdb")  ' Get database path
Conn.Open connStr  ' Open the connection to the database

sTopic=Request.form("Topic") ' Set variable to form field information
sTopic=Replace(sTopic,"#","No")  ' Replace the # with the number abbreveation
sTopic=Replace(sTopic,"'",",")  ' Replace the ' with  ,
sQuestion=Request.form("Question")  ' Set variable to form field information
sQuestion=Replace(sQuestion,"#","No")   ' Replace the # with the number abbreveation
sQuestion=Replace(sQuestion,"'"," ")
 ' Replace the ' with  a space

'Create your SQL insert statment

sSql= "Insert Into Topics (Topic,Question) "
sSql=sSql & " Values('" & sTopic & "', '" & sQuestion & "') "

' It would look something like

'Insert Into Topics (Topic,Question)  Values (' ASP Examples ', 'How do I add a record to a database ' )

Conn.Execute sSql  ' insert the new record
Conn.Close
 ' Close the database connection ( this is always a good practice )
Set Conn=Nothing  'Clear the connection vaiable

%>

Update Function Sample

This example updates information in the Topic and Question fields in the Topics Table from information on a form page

<%

' Dimension your variables

Dim sTopic
Dim sQuestion
Dim sSql
Dim Conn
Dim ConnStr

Set conn = Server.CreateObject("ADODB.Connection") ' Create your connection object
connStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("YourDatabase.mdb")  ' Get database path
Conn.Open connStr  ' Open the connection to the database

sTopic=Request.form("Topic") ' Set variable to form field information
sTopic=Replace(sTopic,"#","No")  ' Replace the # with the number abbreveation
sTopic=Replace(sTopic,"'",",")  ' Replace the ' with  ,
sQuestion=Request.form("Question")  ' Set variable to form field information
sQuestion=Replace(sQuestion,"#","No")   ' Replace the # with the number abbreveation
sQuestion=Replace(sQuestion,"'"," ")
 ' Replace the ' with  space

'Create your Sequel insert statments

sSql="Update Topics Set Topics.Topic=' " & sTopic & " ',  Topics.Question= ' " &  sQuestion  & " ' "

' It would look something like

'Update Topics Set Topics.Topic='ASP Examples' , Topics.Question= 'How do I update a record in a database'

Conn.Execute sSql  ' Update the record
Conn.Close
 ' Close the database connection ( this is always a good practice )
Set Conn=Nothing  'Clear the connection vaiable

%>

Retrieve Function Sample

This example gets records from the Topics table then inserts them in a table on an HTML page

<%

'Demension your variables

Dim sSql
Dim Conn
Dim ConnStr
Dim StrRS

Set conn = Server.CreateObject("ADODB.Connection") ' Create your connection object
connStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("YourDatabase.mdb")  ' Get database path
Conn.Open connStr  ' Open the connection to the database

'Create your Sequel select statments

sSql="Select * From Topics " ' Get all the fields from the database

Set StrRS = Server.CreateObject("ADODB.Recordset") 'Create your recordset object
StrRS.Open sSql,Conn 'Open the recordset

%>

<!-- Now lets put the information on an HTML page -->

<html>
<head>
<title>Your Title</title>
</head>
<body>

<!-- Start the Table -->
<Table>
<TR>
<TD><B>Topic</B></TD>
<TD><B>Question</B></TD>
</TR>

<!-- Now we will loop through the records till we get to the last record -->

<% Do While Not StrRS.EOF %>
<TR>
<TD>=<%=StrRS("Topic")%></TD>
<TD>=<%=StrRS("Question")%></TD>
</TR>
<% StrRS.MoveNext %>
<% Loop %>
</Table>

<%
StrRS.Close  ' Lets close the recordset object
Conn.Close  ' Lets close the connection object
' Clear the variables
Set StrRS=Nothing
Set Conn=Nothing
%>

<!--------  Hide this from the browser ----------

The table would look like this

Topic Question
ASP Examples How do I update a record in a database
ASP Examples How do I update a record in a database

--------- Stop hiding here ------------------->

</body>
</html>

 

Delete Function

This will delete a record from the Topics Table based on a selection from a form page then redirect you to another page

<%

'Demension your variables
Dim Conn
Dim ConnStr

Set conn = Server.CreateObject("ADODB.Connection") ' Create your connection object
connStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("YourDatabase.mdb")  ' Get database path
Conn.Open connStr  ' Open the connection to the database

'Create your Sequel delete statments

Sql="DELETE Topics.* FROM Topics WHERE (((Topics.TopicID)=" & Request.QueryString("TopicID") & "));"

' It would look something like

'DELETE Topics.* FROM Topics WHERE (((Topics.TopicID)=1

Conn.Execute sSql ' Update the record
Conn.Close
 ' Close the database connection ( this is always a good practice )
Set Conn=Nothing  'Clear the connection vaiable

' Lets go to another webpage after we delete this record

Response.Redirect("YourWebPAge")

%>

Those are the four basic functions for working with a database

Oh here's the code for the back button below, nothing to do with database functions but a simple back button

<CENTER>
<INPUT type="Button" Value="Back" OnClick="javascript:history.back(1)">
</CENTER>