johnson00
asked on
rs.AddNew is adding my record to the db, and also adding a blank record each time
Please help. I have changed and changed the recordset properties and can't get it to stop inserting a blank record for every new record I create. Here's my code. All suggestions are appreciated!
<%@ LANGUAGE="VBSCRIPT" %>
<!--#INCLUDE FILE="call_dataconn_config _inc.asp"- ->
<html>
<head>
<title><% =App_Name %></title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='" +args[i+1] +"'");
}
//-->
</script>
</head>
<BODY <%=BodyTag%>>
<!--#INCLUDE FILE="header_inc.asp"-->
<p align="right"> </p>
<form method="POST" action="Add_Test.asp">
<div align="center"><center><ta ble border="0" cellspacing="1" bgcolor="#ECECE6" width="90%">
<tr>
<td colspan="2" bgcolor="#FFFFFF">
<p align="center"><font face="Arial">
<strong>Maintain Tests <br>
</strong></font></p>
</td>
</tr>
<tr>
<td valign="top" align="right"><strong><fon t face="Arial">Test ID</font></strong> </td>
<td valign="top"> <input name="Test_ID" type="text" id="Test_ID" size="30"></td>
</tr>
<tr>
<td valign="top" align="right"><strong><fon t face="Arial">Test Name</font> </strong></td>
<td valign="top"><font face="Arial">
<input name="Test_Name" type="text" id="Test_Name" size="30">
</font></td>
</tr>
<tr>
<td valign="top" align="right"><strong><fon t face="Arial">Test Description </font></strong></td>
<td valign="top"><font face="Arial">
<input name="Test_Desc" type="text" id="Test_Desc" size="30">
</font></td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFFFF">
<p align="center"><input type="submit" value="Add Record"> &n bsp;
<input name="Submit" type="submit" onClick="MM_goToURL('paren t','defaul t.asp');re turn document.MM_returnValue" value="Home">
</p>
</td>
</tr>
</table>
</center></div>
</form>
<%
Set ConnPasswords = Server.CreateObject("ADODB .Connectio n")
Set CmdEditTest = Server.CreateObject("ADODB .Recordset ")
ConnPasswords.Open ConnectionString
SQL = "SELECT * FROM TESTS"
CmdEditTest.Open SQL, ConnPasswords, 0, 2
CmdEditTest.addnew
CmdEditTest("Test_ID") = request("Test_ID")
CmdEditTest("Test_Name") = request("Test_Name")
CmdEditTest("Test_Desc") = request("Test_Desc")
CmdEditTest.update
CmdEditTest.close
set CmdEditTest = nothing
%>
<!--#INCLUDE FILE="footer_inc.asp"-->
</body>
</html>
<%@ LANGUAGE="VBSCRIPT" %>
<!--#INCLUDE FILE="call_dataconn_config
<html>
<head>
<title><% =App_Name %></title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"
}
//-->
</script>
</head>
<BODY <%=BodyTag%>>
<!--#INCLUDE FILE="header_inc.asp"-->
<p align="right"> </p>
<form method="POST" action="Add_Test.asp">
<div align="center"><center><ta
<tr>
<td colspan="2" bgcolor="#FFFFFF">
<p align="center"><font face="Arial">
<strong>Maintain Tests <br>
</strong></font></p>
</td>
</tr>
<tr>
<td valign="top" align="right"><strong><fon
<td valign="top"> <input name="Test_ID" type="text" id="Test_ID" size="30"></td>
</tr>
<tr>
<td valign="top" align="right"><strong><fon
<td valign="top"><font face="Arial">
<input name="Test_Name" type="text" id="Test_Name" size="30">
</font></td>
</tr>
<tr>
<td valign="top" align="right"><strong><fon
<td valign="top"><font face="Arial">
<input name="Test_Desc" type="text" id="Test_Desc" size="30">
</font></td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFFFF">
<p align="center"><input type="submit" value="Add Record"> &n
<input name="Submit" type="submit" onClick="MM_goToURL('paren
</p>
</td>
</tr>
</table>
</center></div>
</form>
<%
Set ConnPasswords = Server.CreateObject("ADODB
Set CmdEditTest = Server.CreateObject("ADODB
ConnPasswords.Open ConnectionString
SQL = "SELECT * FROM TESTS"
CmdEditTest.Open SQL, ConnPasswords, 0, 2
CmdEditTest.addnew
CmdEditTest("Test_ID") = request("Test_ID")
CmdEditTest("Test_Name") = request("Test_Name")
CmdEditTest("Test_Desc") = request("Test_Desc")
CmdEditTest.update
CmdEditTest.close
set CmdEditTest = nothing
%>
<!--#INCLUDE FILE="footer_inc.asp"-->
</body>
</html>
ASKER
Yes, this form submits to itself. I'm not using insert command, as I was told this was a bad programming choice, and to use rs.addnew instead. Here is the portion of the code that does the addnew and update:
<%
Set ConnPasswords = Server.CreateObject("ADODB .Connectio n")
Set CmdEditTest = Server.CreateObject("ADODB .Recordset ")
ConnPasswords.Open ConnectionString
SQL = "SELECT * FROM TESTS"
CmdEditTest.Open SQL, ConnPasswords, 0, 2
CmdEditTest.addnew
CmdEditTest("Test_ID") = request("Test_ID")
CmdEditTest("Test_Name") = request("Test_Name")
CmdEditTest("Test_Desc") = request("Test_Desc")
CmdEditTest.update
CmdEditTest.close
set CmdEditTest = nothing
%>
<%
Set ConnPasswords = Server.CreateObject("ADODB
Set CmdEditTest = Server.CreateObject("ADODB
ConnPasswords.Open ConnectionString
SQL = "SELECT * FROM TESTS"
CmdEditTest.Open SQL, ConnPasswords, 0, 2
CmdEditTest.addnew
CmdEditTest("Test_ID") = request("Test_ID")
CmdEditTest("Test_Name") = request("Test_Name")
CmdEditTest("Test_Desc") = request("Test_Desc")
CmdEditTest.update
CmdEditTest.close
set CmdEditTest = nothing
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>I'm not using insert command, as I was told this was a bad programming choice<<
When you find that person again, tell them they don't have a clue.
When you find that person again, tell them they don't have a clue.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
something here doesn't make sence... this is how you should be using AddNew method
Recordset.AddNew(field,val ue)
or
Recordset.AddNew("field1, field2, field3", "value1, value2, value3")
Recordset.AddNew(field,val
or
Recordset.AddNew("field1, field2, field3", "value1, value2, value3")
in your example you can try this
ConnPasswords.Open ConnectionString
CmdEditTest.Open "myTable", ConnPasswords, adOpenKeyset, adLockOptimistic
CmdEditTest.addnew
CmdEditTest.Fields("Test_I D") = request("Test_ID")
CmdEditTest.Fields("Test_N ame") = request("Test_Name")
CmdEditTest.Fields("Test_D esc") = request("Test_Desc")
CmdEditTest.update
CmdEditTest.close
Set CmdEditTest = nothing
ConnPasswords.Open ConnectionString
CmdEditTest.Open "myTable", ConnPasswords, adOpenKeyset, adLockOptimistic
CmdEditTest.addnew
CmdEditTest.Fields("Test_I
CmdEditTest.Fields("Test_N
CmdEditTest.Fields("Test_D
CmdEditTest.update
CmdEditTest.close
Set CmdEditTest = nothing
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, I've changed my code to the following, and it's not entering the blank record anymore, but also not entering the input from the field:
If Request.Form("add new") = "Add New Test" then
Set ConnPasswords = Server.CreateObject("ADODB .Connectio n")
Set CmdEditTest = Server.CreateObject("ADODB .Recordset ")
ConnPasswords.Open ConnectionString
SQL = "SELECT * FROM TESTS"
CmdEditTest.Open SQL, ConnPasswords, 0, 2
CmdEditTest.addnew
CmdEditTest("Test_ID") = request("Test_ID")
CmdEditTest("Test_Name") = request("Test_Name")
CmdEditTest("Test_Desc") = request("Test_Desc")
CmdEditTest.update
CmdEditTest.close
set CmdEditTest = nothing
ConnPasswords.Close
Set ConnPasswords = Nothing
end if
If Request.Form("add new") = "Add New Test" then
Set ConnPasswords = Server.CreateObject("ADODB
Set CmdEditTest = Server.CreateObject("ADODB
ConnPasswords.Open ConnectionString
SQL = "SELECT * FROM TESTS"
CmdEditTest.Open SQL, ConnPasswords, 0, 2
CmdEditTest.addnew
CmdEditTest("Test_ID") = request("Test_ID")
CmdEditTest("Test_Name") = request("Test_Name")
CmdEditTest("Test_Desc") = request("Test_Desc")
CmdEditTest.update
CmdEditTest.close
set CmdEditTest = nothing
ConnPasswords.Close
Set ConnPasswords = Nothing
end if
ASKER
Correction: It's adding ONLY the blank record now.
check the form element values
Test_ID = request("Test_ID")
Response.Write (Test_ID)
'etc ...
If Request.Form("add new") = "Add New Test" then ' is this form element correct?
maybe like this
If Request.Form("submit") = "Add New Test" then
Test_ID = request("Test_ID")
Response.Write (Test_ID)
'etc ...
If Request.Form("add new") = "Add New Test" then ' is this form element correct?
maybe like this
If Request.Form("submit") = "Add New Test" then
can you try this? maybe you need to change recordset type
ConnPasswords.Open ConnectionString
CmdEditTest.Open "myTable", ConnPasswords, 1, 3 '// you had --> 0, 2
CmdEditTest.addnew
CmdEditTest.Fields("Test_I D").Value = request.form("Test_ID")
CmdEditTest.Fields("Test_N ame").Valu e = request.form("Test_Name")
CmdEditTest.Fields("Test_D esc").Valu e = request.fom("Test_Desc")
CmdEditTest.update
CmdEditTest.close
Set CmdEditTest = nothing
http://www.w3schools.com/ado/met_rs_open.asp#CursorTypeEnum
[CursorTypeEnum Values]
Const adOpenUnspecified=-1
Const adOpenForwardOnly=0
Const adOpenKeyset=1
Const adOpenDynamic=2
Const adOpenStatic=3
[LockTypeEnum Values]
Const adLockUnspecified=-1
Const adLockReadOnly=1
Const adLockPessimistic=2
Const adLockOptimistic=3
Const adLockBatchOptimistic=4
ConnPasswords.Open ConnectionString
CmdEditTest.Open "myTable", ConnPasswords, 1, 3 '// you had --> 0, 2
CmdEditTest.addnew
CmdEditTest.Fields("Test_I
CmdEditTest.Fields("Test_N
CmdEditTest.Fields("Test_D
CmdEditTest.update
CmdEditTest.close
Set CmdEditTest = nothing
http://www.w3schools.com/ado/met_rs_open.asp#CursorTypeEnum
[CursorTypeEnum Values]
Const adOpenUnspecified=-1
Const adOpenForwardOnly=0
Const adOpenKeyset=1
Const adOpenDynamic=2
Const adOpenStatic=3
[LockTypeEnum Values]
Const adLockUnspecified=-1
Const adLockReadOnly=1
Const adLockPessimistic=2
Const adLockOptimistic=3
Const adLockBatchOptimistic=4
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry try this
CmdEditTest.Open "myTable", ConnPasswords, 2, 3
CmdEditTest.Open "myTable", ConnPasswords, 2, 3
ASKER
It works it works it works! Here's the final code:
<%
If request.form("mybutton") = "Add Record" Then
Set ConnPasswords = Server.CreateObject("ADODB .Connectio n")
Set CmdEditTest = Server.CreateObject("ADODB .Recordset ")
ConnPasswords.Open ConnectionString
SQL = "SELECT * FROM TESTS"
CmdEditTest.Open SQL, ConnPasswords, 2, 3
CmdEditTest.addnew
CmdEditTest.Fields("Test_I D").Value = request.form("Test_ID")
CmdEditTest.Fields("Test_N ame").Valu e = request.form("Test_Name")
CmdEditTest.Fields("Test_D esc").Valu e = request.form("Test_Desc")
CmdEditTest.update
CmdEditTest.close
set CmdEditTest = nothing
ConnPasswords.Close
Set ConnPasswords = Nothing
end if
<%
If request.form("mybutton") = "Add Record" Then
Set ConnPasswords = Server.CreateObject("ADODB
Set CmdEditTest = Server.CreateObject("ADODB
ConnPasswords.Open ConnectionString
SQL = "SELECT * FROM TESTS"
CmdEditTest.Open SQL, ConnPasswords, 2, 3
CmdEditTest.addnew
CmdEditTest.Fields("Test_I
CmdEditTest.Fields("Test_N
CmdEditTest.Fields("Test_D
CmdEditTest.update
CmdEditTest.close
set CmdEditTest = nothing
ConnPasswords.Close
Set ConnPasswords = Nothing
end if
yeaeeeeeeee!!! :)
I am glad that you have this sorted out. Please change this line:
SQL = "SELECT * FROM TESTS"
to this:
SQL = "SELECT * FROM TESTS WHERE 1=0"
it will be much more efficient especially as your table grows.
FtB
SQL = "SELECT * FROM TESTS"
to this:
SQL = "SELECT * FROM TESTS WHERE 1=0"
it will be much more efficient especially as your table grows.
FtB
OR
don't use SELECT statement at all like this
CmdEditTest.Open "tableName", ConnPasswords, 2, 3
don't use SELECT statement at all like this
CmdEditTest.Open "tableName", ConnPasswords, 2, 3
Or:
Skip using a recordset in the first place and use Insert/Update/Delete statements instead.
Skip using a recordset in the first place and use Insert/Update/Delete statements instead.
@davidlars99 --
>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>
don't use SELECT statement at all like this
CmdEditTest.Open "tableName", ConnPasswords, 2, 3
<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<
My understanding is that will bring over the whole table--the same as a SELECT without a WHERE clause.
FtB
>>>>>>>>>>>>>>>>>>>>>>>>>>
don't use SELECT statement at all like this
CmdEditTest.Open "tableName", ConnPasswords, 2, 3
<<<<<<<<<<<<<<<<<<<<<<<<<<
My understanding is that will bring over the whole table--the same as a SELECT without a WHERE clause.
FtB
no, it will just make reference to that table and won't keep any data in the recordset
Not to be contrary, but I just ran this code below against the Northwind.mdb and it returned all records:
<%@ Language = VBScript %>
<%Option Explicit%>
<%Response.Buffer = True%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
dim strDataPath, strConnectString, objConnection, objRS
'set connection strings for entire application
strDataPath = server.MapPath("NorthWind. mdb")
strConnectString = "Provider=Microsoft.Jet.OL EDB.4.0;Us er ID=Admin;"_
+ " Data Source= " & strDataPath & ";"_
+ " Mode=Share Deny None;User Id=admin;PASSWORD=;"
if not IsObject("ojbConnection") then
set objConnection=Server.Creat eObject("A DODB.Conne ction")
objConnection.ConnectionTi meout = 15
objConnection.CommandTimeo ut = 10
objConnection.Mode = 3 'adModeReadWrite
if objConnection.state = 0 then
objConnection.Open strConnectString
end if
end if
set objRS=Server.CreateObject( "ADODB.Rec ordSet")
objRS.Open "Customers",objConnection, 3,3
%>
</HEAD>
<BODY>
<table>
<%
do while not objRS.EOF
Response.write(objRS("Coun try") & "<br>")
objRS.MoveNext()
loop
%>
</table>
</BODY>
<%
objRS.Close
set objRs = Nothing
objConnection.Close
set objConnection = Nothing
%>
</HTML>
<%@ Language = VBScript %>
<%Option Explicit%>
<%Response.Buffer = True%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
dim strDataPath, strConnectString, objConnection, objRS
'set connection strings for entire application
strDataPath = server.MapPath("NorthWind.
strConnectString = "Provider=Microsoft.Jet.OL
+ " Data Source= " & strDataPath & ";"_
+ " Mode=Share Deny None;User Id=admin;PASSWORD=;"
if not IsObject("ojbConnection") then
set objConnection=Server.Creat
objConnection.ConnectionTi
objConnection.CommandTimeo
objConnection.Mode = 3 'adModeReadWrite
if objConnection.state = 0 then
objConnection.Open strConnectString
end if
end if
set objRS=Server.CreateObject(
objRS.Open "Customers",objConnection,
%>
</HEAD>
<BODY>
<table>
<%
do while not objRS.EOF
Response.write(objRS("Coun
objRS.MoveNext()
loop
%>
</table>
</BODY>
<%
objRS.Close
set objRs = Nothing
objConnection.Close
set objConnection = Nothing
%>
</HTML>
If you are going to do that (not that I would recommend it) than at least add the option as in:
CmdEditTest.Open "tableName", ConnPasswords, 2, 3, 512
CmdEditTest.Open "tableName", ConnPasswords, 2, 3, 512
For the record (and completeness):
adCmdTable 2 Evaluates CommandText as a table name whose columns are all returned by an internally generated SQL query.
adCmdTableDirect 512 Evaluates CommandText as a table name whose columns are all returned. Used with Recordset.Open or Requery only.
adCmdTable 2 Evaluates CommandText as a table name whose columns are all returned by an internally generated SQL query.
adCmdTableDirect 512 Evaluates CommandText as a table name whose columns are all returned. Used with Recordset.Open or Requery only.
My comments were posted in response to davidlars99's comments not fritz_the_blank's example.
By defintion both adCmdTable and adCmdTableDirect (when supported by the provider) return all the rows in the table. The difference is that adCmdTable is just basically "Select * From " prefixing the recordset's Source property.
@ ftb
try like this --> CmdEditTest.Open "tableName", ConnPasswords, 2, 3, 128
try like this --> CmdEditTest.Open "tableName", ConnPasswords, 2, 3, 128
For the record, you cannot use adExecuteNoRecords with the recordset's Open method only with the Command object's Execute method.
Which brings me back to the point of using an INSERT/UPDATE/DELETE SQL statement with adExecuteNoRecords <g>
As ACPerkins indicated, no dice:
ADODB.Recordset (0x800A0BB9)
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
@ACPerkins:
I did suggest what you say above! http:#13954064
Once you showed me how do to do that properly, I have been using it much more often in my coding.
FtB
ADODB.Recordset (0x800A0BB9)
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
@ACPerkins:
I did suggest what you say above! http:#13954064
Once you showed me how do to do that properly, I have been using it much more often in my coding.
FtB
perkins, explaining the specifics of the recordset will require you a lot of writting :^) would be lot easier if could just provide a link --> http://www.w3schools.com/ado/met_rs_open.asp
I have never used recodset in that fashion anyway, I just blury remembered some basics and thanks to everyone for refreshing my mind. I do agree with everyone though! :)
I have never used recodset in that fashion anyway, I just blury remembered some basics and thanks to everyone for refreshing my mind. I do agree with everyone though! :)
>>perkins, explaining the specifics of the recordset will require you a lot of writting <<
No need, I don't use recordsets.
>>would be lot easier if could just provide a link <<
But your link does confirm:
adExecuteNoRecords 128 The command text is a command or stored procedure that does not return rows. If any rows are retrieved, they are discarded and not returned. adExecuteNoRecords can only be passed as an optional parameter to the Command or Connection Execute method.
>>I have never used recodset in that fashion anyway<<
Same here.
No need, I don't use recordsets.
>>would be lot easier if could just provide a link <<
But your link does confirm:
adExecuteNoRecords 128 The command text is a command or stored procedure that does not return rows. If any rows are retrieved, they are discarded and not returned. adExecuteNoRecords can only be passed as an optional parameter to the Command or Connection Execute method.
>>I have never used recodset in that fashion anyway<<
Same here.
yep!
where is the insert part >??