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

Inserting record in group batches

Status
Not open for further replies.

Ken011

MIS
Oct 13, 2006
66
US
Greetings experts:

How can I modify this code so that it allows me to new records?

Right now, it is over-writing existing records.

Just a brief history: There are 5 categories, category 10, 5,3,2,1. Each category, with the exception of category 2, has 6 subCategories.

The way it works currently is that, if I select a category and click "Submit", the subCategories associated with that category are displayed. I can tben make changes and then submit to the db.

What I would like to do is rather than make changes, I want the subCategories to be submitted as new records.

Any assistance is appreciated.

here is entire working code:

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
Conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("Database/stm.mdb")
If Len(Request.Form) > 0 Then
For Each i In Request.Form("SubCategory")
errMsg = ""
SubCategory = i
BeginCount = Request.Form("BeginCount_" & i)
EndCount = Request.Form("EndCount_" & i)
If Not IsNumeric(SubCategory) Then
errMsg = errMsg & "SubCategory not valid<br>"
End If
If Not IsNumeric(BeginCount) Then
errMsg = errMsg & "BeginCount not valid<br>"
End If
If Not IsNumeric(EndCount) Then
errMsg = errMsg & "EndCount not valid<br>"
End If
If errMsg = "" Then
BeginCount = CDbl(BeginCount)
EndCount = CDbl(EndCount)
If EndCount > BeginCount Then
errMsg = errMsg & "End Count must not be greater than Begin Count<br>"
End If
End If
msg = msg & "<p>SubCategory: " & SubCategory & "<br>"
sql = "UPDATE Lottos SET BeginCount = " & BeginCount &_
", EndCount = " & EndCount &_
", TodayDay = Now()" &_
" WHERE SubCategory = " & SubCategory
'response.write sql
'response.end
If errMsg = "" Then
Conn.Execute(sql)
msg = msg & sql
Else
msg = msg & errMsg
End If
msg = msg & "</p>"
Next
End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "(URL address blocked: See forum rules)">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>New Tickets</title>
</head>
<body>
<div><%=msg%></div>
<%
Set rsCat = Server.CreateObject("ADODB.Recordset")
rsCat.Open "SELECT DISTINCT Category FROM lottos WHERE Category",conn
%>
<form name="form1" method="post" action="">
  Choose Category
  <select name="Category">
    <option value="-1">Choose</option>
	<% While Not rsCat.EOF %>
	<option value="<%=rsCat("Category")%>"><%=rsCat("Category")%></option>
	<% rsCat.MoveNext
	Wend

	rsCat.Close
	set rsCat = Nothing
	%>
  </select>
  <input type="submit" name="Submit" value="Submit">
</form>
<p>
  <%
If Request.Form("Category") > 0 Then
	cat = Replace(Request.Form("Category"),"'","''")

	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.Open "SELECT * FROM lottos WHERE Category = " & cat,conn
%>
<form name="Category<%=rs("Category")%>" method="post" action="">
<p>Working on SubCategoryegories from Category <%=cat%></p>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<td>SubCategory</td>
<td>BeginCount</td>
<td>EndCount</td>
</tr>
<% While Not rs.EOF %>
<tr>
<td><input type="text" name="SubCategory" value="<%=rs("SubCategory")%>" readonly="readonly"></td>
<td><input type="text" name="BeginCount_<%=rs("SubCategory")%>" value="<%=rs("BeginCount")%>"></td>
<td><input type="text" name="EndCount_<%=rs("SubCategory")%>" value="<%=rs("EndCount")%>"></td>
</tr>
<% rs.moveNext
Wend
%>
</table>
<input type="submit" name="submit" value="Submit">
</form>
<%
End If
'rs.Close
set rs = Nothing
conn.close
set conn = Nothing
%>
</body>
</html>
 
Code:
sql = "[b]UPDATE[/b] Lottos SET BeginCount = " & BeginCount &_
", EndCount = " & EndCount &_
", TodayDay = Now()" &_
" WHERE SubCategory = " & SubCategory

You may want to look at this bit of code - then refer to a SQL reference manual / site that explains the difference between SELECT, UPDATE, INSERT and DELETE.

This site may help:



A smile is worth a thousand kind words. So smile, it's easy! :)
 
Maybe I should have been a bit clearer.

The code I posted is using an *update* statement. I know that much. However, I want to change that update statement to insert statement but my big issue is that given the fact that a category can have 6 or more sub categories, how do I create such an insert statement.

I was thinking of using the split() function I can insert a category and its associated sub categories. I just don't know how to do that.

I hope this is clearer.

Thanks,
 
Maybe this link would help:

You don't specifically state what db you are using, but from the looks of your connection string it is MS Access. The best solution for you in that case is likely to be the UNION ALL version of the INSERT which you can dynamically create with a loop in your ASP code. I haven't used MS Access as a db for ASP for a very long time, so I'm not 100% on it's support of the different methods - I doubt it will allow the multiple VALUES sets approach like DB2, MySQL etc do, and I don't think it will allow multiple INSERT Statements in a single command - but I could be wrong! - so your easiest option for a single command multi-insert would be the SELECT ... UNION ALL approach. Either that or just create individual inserts and execute separately.



A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top