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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database addnew record

Status
Not open for further replies.

bjzielinski

IS-IT--Management
Nov 8, 2001
93
US
I am creating a front end for a MSAccess database. I have a form that inputs data into the database. What I want to do is have the user input the data into the fields, press the Add button and then a message box pops up saying the information was added, then clear out the boxes so another record can be added. Here are a few lines of my code:

<script language=&quot;VBScript&quot;>
<!-- start of VBscript hide from old browsers

Sub add_OnClick()
set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & server.mappath(&quot;../fpdb/tax.mdb&quot;) & &quot;;&quot;
strsql = &quot;SELECT * FROM forclosed WHERE recNo=0;&quot;
set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open strsql, conn, 1, 2
...
rs.AddNew
rs.Fields(&quot;ID1&quot;).Value = newForm.ID1.value
rs.Fields(&quot;ID2&quot;).Value = newForm.ID2.value
...
End Sub
-->
</script>
...
<Form name=newForm>
...
<TD vAlign=top align=right>Minimum Bid:</TD>
<TD><input type=text name=minbid size=30></TD>
...
<TD><input type=button name=add value=Add><input type=reset name=reset value=Clear>


Unfortunately this does nothing. The debugger says something about the Server mode not being handled when the sub procedure runs. However when I run it in just IE, nothing happens, no error, the sub procedure doesn't even seem to run. If anybody has an easy way to clear out the fields let me know. There's probably a simple command I'm missing, other than pressing the Clear button. Bill Zielinski
bzielinski@co.midland.mi.us
County of Midland, Michigan
 
I would use two pages to do this rather than one (although you could do it with one if you really want). One page - input.asp - has the form which the user fills in and then the other - insert.asp - actually inserts the record into the db, displays a confirmation message and redirects them back to the input page.

Code:
-- input.asp --
<form name=&quot;form1&quot; action=&quot;insert.asp&quot; method=&quot;post&quot;>
First Name: <input type=&quot;text&quot; name=&quot;FName&quot; size=&quot;20&quot;><br>
Last Name: <input type=&quot;text&quot; name=&quot;LName&quot; size=&quot;20&quot;><br>
<input type=&quot;submit&quot; value=&quot;Add record&quot;>
</form>

-- insert.asp --
<%
Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.Open <conn string>

strSQL = &quot;INSERT INTO users (firstname, lastname) &quot; &_
  &quot;VALUES ('&quot; & Request.Form(&quot;FName&quot;) & &quot;', '&quot; & Request.Form(&quot;LName&quot;) & &quot;')&quot;

conn.Execute(strSQL)
%>

<script language=&quot;javascript&quot;>
<!--
alert(&quot;Record inserted.  Click OK to add another&quot;);
location.href = &quot;input.asp&quot;;
//-->
</script>
--James
 
There is an error in your code between the two lines in red :

<script language=&quot;VBScript&quot;>
<!-- start of VBscript hide from old browsers

Sub add_OnClick()
set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)

conn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & server.mappath(&quot;../fpdb/tax.mdb&quot;) & &quot;;&quot;
strsql = &quot;SELECT * FROM forclosed WHERE recNo=0;&quot;
set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)


The first line defines a client side script so you can't do a Server.CreateObject(&quot;ADODB.Connection&quot;).

Replace this line by &quot;set conn = CreateObject(&quot;ADODB.Connection&quot;)&quot; if you want your script to run client side or replace the <Script></script> tags by <% and %> if you want to run server side (ASP code).
Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top