By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,237 Members | 517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,237 IT Pros & Developers. It's quick & easy.

ASP form page

P: 1
Hi everyone

I'm learning asp, and i downloaded this script to teach me how to post form data from a webpage to an access database. I put it on the server so i could make sure that it worked, and everything was fine, so i changed the variable names (the names of the form elements, and the names of the rows in access) so that it would be more correct to what i am using it for. Now once I did that, it gave me an error on line 24 which was the SQL insert command. I didnt change that, and I cannot figure out why just changing the names would cause this problem.

This is the form that works:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <title>Test ASP to database and reporting</title>
  3. <body bgcolor="#bae8f9">
  4. <img src="itform.jpg" border=0>
  5. <script language="JavaScript"><!--
  6. function stamp() {
  7.     document.form.email.value = new Date();
  8. }
  9. //--></script>
  10.  
  11. <form action="form_ac.asp" method="post" name="form" onSubmit="stamp()">
  12. <table border=0>
  13. <tr>
  14. <td>
  15. Station:
  16. </td>
  17. <td>
  18. <input type="text" name="name"><br>
  19. </td>
  20. </tr>
  21. <tr>
  22. <td>
  23. <input type="hidden" name="email">
  24. <input type="hidden" name="country" value="No">
  25. Problem:
  26. </td>
  27. <td>
  28. <textarea name="comments" cols="20" rows="5"></textarea><br>
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>
  33. <input type="submit" value="Submit">
  34. </td>
  35. <td>
  36. <input type="reset" value="Reset">
  37. </td>
  38. </tr>
  39. </table>
  40. </form>
  41. </body>
  42. </html>
  43.  
and this is the asp page that it goes through to send it to the database:
Expand|Select|Wrap|Line Numbers
  1. <%
  2. ' Declaring variables
  3. Dim name, email, country, comments, data_source, con, sql_insert
  4.  
  5. ' A Function to check if some field entered by user is empty
  6. Function ChkString(string)
  7. If string = "" Then string = " "
  8. ChkString = Replace(string, "'", "''")
  9. End Function
  10.  
  11. ' Receiving values from Form
  12. name = ChkString(Request.Form("name"))
  13. email = ChkString(Request.Form("email"))
  14. country = ChkString(Request.Form("country"))
  15. comments = ChkString(Request.Form("comments"))
  16. data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ 
  17. Server.MapPath("form.mdb")
  18. sql_insert = "insert into users (name, email, country, comments) values ('" & _
  19. name & "', '" & email & "', '" & country & "', '" & comments & "')"
  20.  
  21. ' Creating Connection Object and opening the database
  22. Set con = Server.CreateObject("ADODB.Connection")
  23. con.Open data_source
  24. con.Execute sql_insert
  25.  
  26. ' Done. Close the connection
  27. con.Close
  28. Set con = Nothing
  29.  
  30. Response.Write "Your request has been submitted. Thank you."
  31.  
  32. Response.AddHeader "Refresh", "2;URL=form.htm" 
  33. %>
  34.  
that one works, but all the names are country email name and comments. This isint really a problem, but I would like to know what is wrong, so i can add other elements to the form, and change their names.
Here is the one i changed, and doesnt work:
Expand|Select|Wrap|Line Numbers
  1.  <html>
  2. <title>Test ASP to database and reporting</title>
  3. <body>
  4.  
  5. <script language="JavaScript"><!--
  6. function stamp() {
  7.     document.form.datetime.value = new Date();
  8. }
  9. //--></script>
  10.  
  11. Information to be entered to access
  12. <form action="form_ac.asp" method="post" name="form" onSubmit="stamp()">
  13. Station : <input type="text" name="station"><br>
  14. <input type="hidden" name="datetime"><br>
  15. Problem : <textarea name="problem" cols="20" rows="5"></textarea><br>
  16. <input type="hidden" name="fixed" value="No">
  17. <input type="submit" value="Submit"><input type="reset" value="Reset">
  18. </form>
  19. </body>
  20. </html>
  21.  
and here is the changed asp file
Expand|Select|Wrap|Line Numbers
  1. <%
  2. ' Declaring variables
  3. Dim station, datetime, problem, fixed, data_source, con, sql_insert
  4.  
  5. ' A Function to check if some field entered by user is empty
  6. Function ChkString(string)
  7. If string = "" Then string = " "
  8. ChkString = Replace(string, "'", "''")
  9. End Function
  10.  
  11. ' Receiving values from Form
  12. station = ChkString(Request.Form("station"))
  13. datetime = ChkString(Request.Form("datetime"))
  14. problem = ChkString(Request.Form("problem"))
  15. fixed = ChkString(Request.Form("fixed"))
  16. data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ 
  17. Server.MapPath("form.mdb")
  18. sql_insert = "insert into users (station, datetime, problem, fixed) values ('" & _
  19. station & "', '" & datetime & "', '" & problem & "', '" & fixed & "')"
  20.  
  21. ' Creating Connection Object and opening the database
  22. Set con = Server.CreateObject("ADODB.Connection")
  23. con.Open data_source
  24. con.Execute sql_insert
  25.  
  26. ' Done. Close the connection
  27. con.Close
  28. Set con = Nothing
  29. %>
  30.  
I went through both databases, and all of the fields are set to text, and everything is the same case, no misspellings or anything.
Please help, this was intended for me to learn from, and it doesnt make any sense to me why something so simple wont work properly.
Thank you,
Dan
Apr 18 '07 #1
Share this Question
Share on Google+
1 Reply

P: 36
Choose an other name for the DATETIME variable because it is a reserved ACCESS word.

http://www.bairdgroup.com/reservedwords.cfm

In my opinion it is good practice to prefix variable names with their "type" (Although ASP-script is not strongly typed). E.g. dtDateTime, strName, intCounter, objRecordSet etc.
Apr 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.