SQL Syntax - Insert multiple records with one submission - Classic ASP

Hi Experts,
My query returns multiple records that I would like to submit to a different table with a single submission.
I'm using checkboxes for each record that I would like to insert into my new table.
My "Process.asp" can insert only one record at once. How can I modify it to submit all records at once. Please take a look at my code.
I would really appreciate your help.
<html>
<head>
<title></title>
<link rel="stylesheet" href="/RouteMapper/css/Styles.css" type="text/css" />

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function checkLikeMe($) {
    var x = document.getElementsByName('DeviceName');
    for (var y = 0; y < x.length; y++)
        x[y].checked = $;
}

// End -->
</script>

</head>
<body>

<form name="contact_form" method="post" action="Process.asp">
<table border="1">
<tr>
<th><input type="checkbox" onClick="checkLikeMe(this.checked)"></th>
<th>Device Name</th>
<th>OU</th>
<th>SubOU</th>
</tr>

<%
rs.AbsolutePage = intPage
For intRecord = 1 To rs.PageSize
%>
<tr>
<td><input type="checkbox" name="DeviceName" value="<%=rs("DeviceName") %>"></td>
<td><%=rs("DeviceName")%></td>
<td><%=rs("OU")%></td>
<td><%=rs("SubOU")%></td>
</tr>



<%
	rs.MoveNext
If rs.EOF Then Exit For

Next

rs.Close
set rs = Nothing
AdoCon.Close
set AdoCon = nothing
%>
</table>

<center>
<br />
<select name="Environment">
<option value="5">Dev</option>
<option value="4">Stage</option>
</select>

<input type="Submit" name="Submit" value="Submit" />
</center>
</form>
</center>

</body>
</html>	

Open in new window




And this is my "Process.asp" that needs to be modified in order to insert multiple records:

<!-- #include virtual="/RouteMapper/Connections/APSConn.asp" -->
<%
'declare variables
Dim AdoCon, strSQL, strDeviceName, strOU, strSubOU, Environment

'Set values into variables
strDeviceName = request.Form("DeviceName")
strOU = request.Form("OU")
strSubOU = request.Form("SubOU")
strEnvironment = request.Form("Environment")

'create SQL string for inserting data into database
strSQL = "INSERT INTO TestTable (DeviceName, OU, SubOU, Environment) VALUES ('"& strDeviceName &"','"& strOU &"','"& strSubOU &"','"& strEnvironment &"')"

'now add data with Execute() method
AdoCon.Execute(strSql)

'close AdoConection object
AdoCon.Close
Set AdoCon = Nothing


'Redirect to the completed.asp page
Response.Redirect "Default.asp"

%>

Open in new window

romsomIT DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan LouwerensSoftware EngineerCommented:
You probably want something like:

for index = 1 to Request.Form("DeviceName").Count
   strDeviceName = request.Form("DeviceName")[index]

   'create SQL string for inserting data into database
   strSQL = "INSERT INTO TestTable (DeviceName, OU, SubOU, Environment) VALUES ('"& strDeviceName &"','"& strOU &"','"& strSubOU &"','"& strEnvironment &"')"

   'now add data with Execute() method
   AdoCon.Execute(strSql)
next

Open in new window


Also note that the "DeviceName" is the only parameter you're putting into the form to be harvested. The "OU" and "SubOU" fields are not set into form inputs, so they would not be harvested after the form is submitted.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
romsomIT DeveloperAuthor Commented:
Thank you for your quick response. I'm trying to make it work now.
What would be the best way to also harvest OU and SubOU?
0
Jan LouwerensSoftware EngineerCommented:
I don't know if there's a single best way, but options include:
  1. Concatenating them together, using a unique delimiter that you know doesn't show up in the data (such as a comma, colon, or pipe symbol)
  2. Making a  JSON object

Whichever method you choose, you have to combine them all together in the form side, and set that as the value of the checkbox input. Then, as you harvest it, you'll have to do the reverse, and split the values apart again.
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

romsomIT DeveloperAuthor Commented:
I get an error after I submit. Would you know the reason?

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/ewr/lab/Process.asp, line 5

strDeviceName = request.Form("DeviceName")[index]
------------------------------------------^
0
Jan LouwerensSoftware EngineerCommented:
Those square brackets should be parenthesis. That was my typo.

strDeviceName = request.Form("DeviceName")(index)

Open in new window

0
romsomIT DeveloperAuthor Commented:
Thank you, now it inserts the checked records. Unfortunately it doesn't insert "Environment". It just sends the value 0, not the one selected in the dropdown. What can be the reason?
0
romsomIT DeveloperAuthor Commented:
The problem is fixed, thank you so much for your help!
0
romsomIT DeveloperAuthor Commented:
Thank you very much, it's working the way I wanted it. I'm really grateful for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.