Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting form values into Access Database

Status
Not open for further replies.

SSJpn

Technical User
Oct 7, 2002
259
US
Im new to all this and need some help.

I can get this to work (code below) if all my form's fields are textboxes. But when I have listboxes,checkboxes, and/or radio buttons, it does not work.

In my access database, I leave all field value types as "text" except for the fields where I plan to store checkbox values. These fields I put as "ON/OFF"
Is this right? Is this where my problem is?

Maybe my problem is the value I pass my Request.Form statement. For radio buttons I pass the 'name' attribute of the radio button grouping. And I'm assuming that the 'value' it passes is the value of the radio button selected.
Maybe this is where my problem is?

Please show me how to store listbox, radiobutton, and checkbox values into my database.

Thanks

HERE IS MY CODE

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\blah\db\myDB.mdb"))

sql="INSERT INTO myTable(field1,field2,field3)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("field1") & "',"
sql=sql & "'" & Request.Form("field2") & "',"
sql=sql & "'" & Request.Form("field3") & "')"

conn.Execute sql,recaffected
if err<>0 then
Response.Write(&quot;Failed&quot;)
else
Response.Write(&quot;Worked&quot;)
end if
conn.close


PS - do I need &quot;conn = Nothing&quot; or something like that?
 
When I use radio buttons I set the values to 0 and 1 and pass those values to the database. I haven't had problems with option lists, as long as the options are valid data types. Yes/No fields seem to work best with 1's and 0's but return true/false values from the db.

Someone else might have a better answer, I am a newbie too.
 
I got it to work but now I have another problem.

In my sample code (on the bottom of my first post) I only try to insert 3 values (field1, field2, field3) into my database.

My problem now is that my real form has about 40 fields.When I run this code, I can't even view the page. I get a &quot;Page Cannot Be Displayed&quot; error.

Does this have anything to do maybe with the fact that the string I'm trying to put in variable 'sql' is too long? Whats the max lenght of the string I can have.

If this is the problem, can anyone show me how to fix my code so that I don't have this problem?

Thanks!
 
A couple of things I have done wrong in the past....

I am not sure, but I don't think the length of your string is too long.

Are your using code to trap and display errors? This might help find the error.

One thing I have done is to insert:

response.write sqlStmt 'your sql string
response.end

This will let you view your sql string in its entirety to try to find syntax errors.

Also, make sure that you have single quotes around text fields, brackets around fieldnames with spaces in them, an equal number of field names and values, and replace any apostrophes with double apostrophes. It could just come down to a comma out of place, an invalid data type, etc. I have made all of these mistakes and more. I am learning ASP like My Cousin Vinnie learned law. Spent a few nights in the slammer....

Just guessing, but you might want to double-check your data types, and use some form validation to make sure the entered values will be valid.
 
you need to turn that page cannot be displayed error off

you do that by going in your browser, tools, advanced and there is an option for show friendly http error message
make sure that is UNCHECKED

then the browser will have more specific error messages for you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top