<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("TestDB.mdb") & ";Persist Security Info=False"
If Request("Submit") <> "" Then
intRecIDs = Replace(Request("hidRecIDs"), "*", "") ' remove all the asterisks, to create a list like this: 2, 5, 8, 9 etc.
arrRecIDs = Split(intRecIDs, ", ") ' Create an array, wich will contain just the IDs of the records we need to update
For intCount = 0 to Ubound(arrRecIDs) ' Loop trough the array
strText = Replace(Request("txtText" & arrRecIDs(intCount)), "'", "''")
intNum = Replace(Request("txtNum" & arrRecIDs(intCount)), "'", "''")
set commUpdate = Server.CreateObject("ADODB.Command")
commUpdate.ActiveConnection = strConnection
commUpdate.CommandText = "UPDATE TestTable SET RecText = '" & strText & "', RecNum = " & intNum & " WHERE RecID = " & arrRecIDs(intCount)
commUpdate.CommandType = 1
commUpdate.CommandTimeout = 0
commUpdate.Prepared = true
commUpdate.Execute()
Next
strMessage = intCount & " Records Updated"
Response.Redirect("MultiUpdateDemo.asp?Message=" & strMessage)
End If
%>
<%
Dim Recordset1
Dim Recordset1_numRows
Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = strConnection
Recordset1.Source = "SELECT RecID, RecText, RecNum FROM TestTable"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()
Recordset1_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index
Repeat1__numRows = -1
Repeat1__index = 0
Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
%>
<html>
<head>
<title>Update Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
<!--
// When the value in a textfield is changed, notice the onChange="RecUpdate('<%= intRecID %>')"
// on each of the textfields, the value of the Record ID associated with that field
// is passed to the RecUpdate function. First the value is surounded with 2 asterisks e.g. *6*
// This is so that *1* can be distinguished from *10*, *11* etc.
function RecUpdate(RecID){
var ThisID = "*" + (RecID) + "*"
if (document.form1.hidRecIDs.value == ""){ // If the hidden field is empty
document.form1.hidRecIDs.value = (ThisID) // Store the value in the hidden field (hidRecIDs) as it is.
}
if (document.form1.hidRecIDs.value != ""){ // If the hidden field isn't empty
var str = document.form1.hidRecIDs.value; // Store the contents of the hidden field in the variable str
var pos = str.indexOf(ThisID); // Search str to see if this RecID is allready in it.
if (pos == -1) { // If the position returned is -1 it isn't allredy in there,
document.form1.hidRecIDs.value = document.form1.hidRecIDs.value + ", " + (ThisID)
} // so add ", " and this ID to what is already in hidRecIDs
} // to create a list like this *2*, *5*, *8* etc.
}
//-->
</script>
</head>
<body>
<%= Request.QueryString("Message") %>
<br />
<form name="form1" method="post" action="MultiUpdateDemo.asp">
<table width="500" border="0" cellpadding="1" cellspacing="6" bgcolor="#F0F0F0">
<tr>
<td width="37"><strong>RecID</strong></td>
<td width="217"><strong>RecText</strong></td>
<td width="226"><strong>RecNum</strong></td>
</tr>
<%
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
%>
<% intRecID =(Recordset1.Fields.Item("RecID").Value) ' Store the current RecordID in a variable %>
<tr>
<td nowrap><%= intRecID %><input name="hidRecID<%= intRecID %>" type="hidden" value="<%= intRecID %>" size="5"></td>
<td nowrap><input name="txtText<%= intRecID %>" type="text" onChange="RecUpdate('<%= intRecID %>')" value="<%=(Recordset1.Fields.Item("RecText").Value)%>" size="20"></td>
<td nowrap><input name="txtNum<%= intRecID %>" type="text" onChange="RecUpdate('<%= intRecID %>')" value="<%=(Recordset1.Fields.Item("RecNum").Value)%>" size="20"></td>
</tr>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
Recordset1.MoveNext()
Wend
%>
</table>
<br />
<input name="hidRecIDs" type="text" size="40"> <=== This would be hidden
<br />
<br />
<br />
<input type="submit" name="Submit" value="Update">
</form>
</body>
</html>
<%
Recordset1.Close()
Set Recordset1 = Nothing
%> |