Inserting Form Content into Database In this article we will create a simple HTML Form and an ASP page. Any data which is entered by the user in that HTML Form will be received by the ASP action page and will enter that data into the Access database. After that we will create an ASP page to show all the records entered and to delete the specific records if wanted. HTML Form Page Create a new HTML page and name it 'form.htm'. Copy the following code and paste it into the body section of 'form.htm' page : Inserting Records : <form action="form_ac.asp" method="post"> Name : <input type="text" name="name"><br> Email : <input type="text" name="email"><br> Country : <input type="text" name="country"><br> Comments : <textarea name="comments" cols="20" rows="5"></textarea><br> <input type="submit" value="submit"> </form> The above HTML code creates a simple Form asking for user name, email, country and comments. You can later change them according to what you want your users to enter. Although the above code does what it is supposed to do, to see a better looking page, you might want to download the zip file at the end of this article. ASP Action Page Create a new ASP page 'form_ac.asp' in the same directory as the above HTML page. Copy and paste the following ASP code into 'form_ac.asp' page : <% ' Declaring variables Dim name, email, country, comments, data_source, con, sql_insert ' A Function to check if some field entered by user is empty Function ChkString(string) If string = "" Then string = " " ChkString = Replace(string, "'", "''") End Function ' Receiving values from Form name = ChkString(Request.Form("name")) email = ChkString(Request.Form("email")) country = ChkString(Request.Form("country")) comments = ChkString(Request.Form("comments")) data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ Server.MapPath("form.mdb") sql_insert = "insert into users (name, email, country, comments) values ('" & _ name & "', '" & email & "', '" & country & "', '" & comments & "')" ' Creating Connection Object and opening the database Set con = Server.CreateObject("ADODB.Connection") con.Open data_source con.Execute sql_insert ' Done. Close the connection con.Close Set con = Nothing %> Our above ASP page 'form_ac.asp' will receive values entered by user in the HTML Form and will first check to see if some fields have been left empty. Then it will insert those values into our Access database. Simple. Simple Access Database Now we will create a simple Access database with just one table 'users' containing five fields, 'id', 'name', 'email', 'country' and 'comments' respectively. If you are not comfortable with creating simplest of Access databases then you might want to read the database tutorial I have compiled for you. Note that at the end of this article you can download the sample database and code mentioned in this article. Open Microsoft Access and create a database 'form.mdb' and save it in the same directory where you are creating other HTML and ASP files for this tutorial. In 'design view' of Microsoft Access our 'users' table will look like : We will now move on to create ASP pages where we can see all the records entered into the database and from where we can delete any records we want. Lets first create an ASP page to show the records entered by users. You will also be able to delete records by pressing the 'delete' button on the corresponding record. Showing All Records Create a new 'showall.asp' ASP page and copy paste the following code into it : <% ' Declaring variables Dim rs, data_source, no no = 0 data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ Server.MapPath("form.mdb") ' Creating Recordset Object and opening the database Set rs = Server.CreateObject("ADODB.Recordset") rs.Open "users", data_source ' Looping through the records to show all of them While Not rs.EOF Response.Write "<form action='del.asp' method='post'>" Response.Write "Name : " & rs("name") & "<br>" Response.Write "Email : " & rs("email") & "<br>" Response.Write "Country : " & rs("country") & "<br>" Response.Write "Comments : " & rs("comments") & "<br>" Response.Write "<input type='hidden' name='id' value='" & rs("id") & "'>" Response.Write "<input type='submit' value='Delete'>" & "<br>" Response.Write "</form>" no = no + 1 rs.MoveNext Wend ' Done. Now close the Recordset rs.Close Set rs = Nothing Response.Write "<p>Total Records Found : " & no %> Above ASP page 'showall.asp' shows all the records in the database. It also allows you to delete the specific record if you want to. We are now almost done except that we also need to create the 'del.asp' page which we mentioned in the above code so that you can delete the records if you want. Records Deletion Page Create a new 'del.asp' ASP page and save it in the same directory where you have been saving other HTML and ASP pages discussed in this article. Copy and paste the following code into it : <% If Len(Request.Form("id")) Then ' Declaring variables Dim form_id, data_source, sql_delete, con form_id = CInt(Request.Form("id")) data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ Server.MapPath("form.mdb") sql_delete = " delete from users where id = " & form_id ' Creating Connection Object and opening the database Set con = Server.CreateObject("ADODB.Connection") con.Open data_source con.Execute sql_delete con.Close Set con = Nothing Response.Write "Record No. : " & form_id & _ " was successfully deleted from the database." Else Response.Redirect "showall.asp" End If %> The above ASP code deletes the records based on the 'id' field received from the 'showall.asp' page.