473,226 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Loop to create an array from a dynamic form.

Loop to create an array from a dynamic form.

I'm having trouble with an application, and I'll try to explain it as
clearly as possible:

1. I have a form with two fields, say Apples and Oranges. The user
selects from a drop down menu, between 1-10 of each and clicks submit.
The resulting page will display a NEW form, with rows and a list of
fields for the amount of each items selected.

Example. If I selected 3 apples and 5 oranges, the resulting page
would show 3 rows of text fields for apples and 5 rows of text fields
for oranges.

The form fields would be (after each semicolon, the first word is the
form element name (Apple1, Apple2)):

Apple1: net price | gross price | date | time
Apple2: net price | gross price | date | time
Apple3: net price | gross price | date | time
Orange1: net price | gross price | date | time
Orange2: net price | gross price | date | time
Orange3: net price | gross price | date | time
Orange4: net price | gross price | date | time
Orange5: net price | gross price | date | time
I want to insert each one of these records into the database, each
record being a net, gross price, date and time for apple 1-X and
orange 1-X. The apples will go into the apple table and the oranges
will go into the oranges table.
I'm used to looping like this:

for each item in request.form
if left(item, 6) = "orange" then
orange = orange & request.form(item) & ","
end if
next
After that, I'll create a loop that inserts each record into the
database separately, something of this nature:
for each item in orange
set rsOrange= server.createobject("adodb.recordset")
strSql = "select * from oranges"
rsOrange.open strSql, Connstr, 1, 3

rsOrange.Addnew
rsOrange("net") = request.form("net")
rsOrange("gross") = request.form("gross")
rsOrange("date") = request.form("date")
rsOrange("time") = request.form("time")
rsOrange.Update
rsOrange.close
next
But since this is a multi dimensional array (gross, net, date, time
will need to be inserted correspondingly), you can see my dilemma. I
understand how they work when hard coded, but not to dynamically
create and populate them, then use in an insert statement. Sorry if
this is elementary, but any help would really be appreciated.

Thanks!
Jul 19 '05 #1
2 4049
You have to number your net, gross, date, and time form fields as well.
(Why do you have separate date and time columns in your table? Why not just
one field for this data?)

Also, when you're doing INSERTs, don't create a recordset object.

Try this code. (Three files)
FORM.HTM:
<html><head></head>
<body>
<form method="post" action="form.asp">
Apples: <input name="apples" type="text" />
Oranges: <input name="oranges" type="text" />
<input type="submit" />
</form>
</body>
</html>

--------------------------------------
FORM.ASP:
<html><head></head>
<body>
<%
Dim iOranges, iApples
iApples = Request.Form("apples")
iOranges = Request.Form("oranges")
%>
<form method="post" action="insert.asp">
<input type="hidden" name="apples" value="<%=iApples%>" />
<input type="hidden" name="oranges" value="<%=iOranges%>" />
<table>
<tr>
<th colspan="5">Apples</th>
</tr>
<tr>
<td>&nbsp;</td>
<td>net price</td>
<td>gross price</td>
<td>date</td>
<td>time</td>
</tr>
<% For i = 1 To iApples %>
<tr>
<td>Apples <%=i%></td>
<td><input name="Applesnet<%=i%>" type="text" /></td>
<td><input name="Applesgross<%=i%>" type="text" /></td>
<td><input name="Applesdate<%=i%>" type="text" /></td>
<td><input name="Applestime<%=i%>" type="text" /></td>
</tr>
<% Next %>

<tr>
<th colspan="5">Orange</th>
</tr>
<tr>
<td>&nbsp;</td>
<td>net price</td>
<td>gross price</td>
<td>date</td>
<td>time</td>
</tr>
<% For i = 1 To iOranges %>
<tr>
<td>Apples <%=i%></td>
<td><input name="Orangesnet<%=i%>" type="text" /></td>
<td><input name="Orangesgross<%=i%>" type="text" /></td>
<td><input name="Orangesdate<%=i%>" type="text" /></td>
<td><input name="Orangestime<%=i%>" type="text" /></td>
</tr>
<% Next %>
</table>
<input type="submit" />
</form>
</body>
</html>


--------------------------------------
INSERT.ASP:
<%
Dim iOranges, iApples
iApples = Request.Form("apples")
iOranges = Request.Form("oranges")

Set oADO = CreateObject("ADODB.Connection")
oADO.Open YourConnectionStringHere
For i = 1 To iApples
sSQL = "INSERT INTO [apples] ([net],[gross],[date],[time]) VALUES (" & _
Request.Form("Applesnet" & i) & "," & _
Request.Form("Applesgross" & i) & "," & _
Request.Form("Applesdate" & i) & "," & _
Request.Form("Applestime" & i) & ")"
' oADO.Execute sSQL,,129 UNCOMMENT IF ALL IS FINE
RESPONSE.WRITE SSQL & "<BR>"
Next
For i = 1 To iOranges
sSQL = "INSERT INTO [Oranges] ([net],[gross],[date],[time]) VALUES (" & _
Request.Form("Orangesnet" & i) & "," & _
Request.Form("Orangesgross" & i) & "," & _
Request.Form("Orangesdate" & i) & "," & _
Request.Form("Orangestime" & i) & ")"
' oADO.Execute sSQL,,129 UNCOMMENT IF ALL IS FINE
RESPONSE.WRITE SSQL & "<BR>"
Next
oADO.Close : Set oADO = Nothing
%>

Ray at home
"Nick" <an*****@gmail.com> wrote in message
news:c3**************************@posting.google.c om...
Loop to create an array from a dynamic form.

I'm having trouble with an application, and I'll try to explain it as
clearly as possible:

1. I have a form with two fields, say Apples and Oranges. The user
selects from a drop down menu, between 1-10 of each and clicks submit.
The resulting page will display a NEW form, with rows and a list of
fields for the amount of each items selected.

Example. If I selected 3 apples and 5 oranges, the resulting page
would show 3 rows of text fields for apples and 5 rows of text fields
for oranges.

The form fields would be (after each semicolon, the first word is the
form element name (Apple1, Apple2)):

Apple1: net price | gross price | date | time
Apple2: net price | gross price | date | time
Apple3: net price | gross price | date | time
Orange1: net price | gross price | date | time
Orange2: net price | gross price | date | time
Orange3: net price | gross price | date | time
Orange4: net price | gross price | date | time
Orange5: net price | gross price | date | time
I want to insert each one of these records into the database, each
record being a net, gross price, date and time for apple 1-X and
orange 1-X. The apples will go into the apple table and the oranges
will go into the oranges table.
I'm used to looping like this:

for each item in request.form
if left(item, 6) = "orange" then
orange = orange & request.form(item) & ","
end if
next
After that, I'll create a loop that inserts each record into the
database separately, something of this nature:
for each item in orange
set rsOrange= server.createobject("adodb.recordset")
strSql = "select * from oranges"
rsOrange.open strSql, Connstr, 1, 3

rsOrange.Addnew
rsOrange("net") = request.form("net")
rsOrange("gross") = request.form("gross")
rsOrange("date") = request.form("date")
rsOrange("time") = request.form("time")
rsOrange.Update
rsOrange.close
next
But since this is a multi dimensional array (gross, net, date, time
will need to be inserted correspondingly), you can see my dilemma. I
understand how they work when hard coded, but not to dynamically
create and populate them, then use in an insert statement. Sorry if
this is elementary, but any help would really be appreciated.

Thanks!

Jul 19 '05 #2
N I
Ray
Thanks so much. I had created the form elements as you described in your
reply, but your insert loop is perfect and much more simple than what I
had started working with.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
1
by: Řyvind Isaksen | last post by:
Hello! I need to dynamic generate a SQL statement based on how many images a user select to upload. Here you see an example with 2 images. It can be up to 50 images and I dont want to write...
4
by: Kevin H | last post by:
Apologies in advance if this sounds slow-witted, but I didn't find it here. Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it...
8
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++)...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
6
by: Rich | last post by:
Hello, I want to simulate the dynamic thumbnail display of Windows Explorer (winxp) on a form or pannel container. If I place a picture box on my container form/pannel and dimension it to the...
7
by: murrayatuptowngallery | last post by:
After a few searches & inquiries here, I have a method of generating an html table populated with results of equations that works and wasn't too far up a learning curve for me, thanks to a helpful...
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.