Click to See Complete Forum and Search --> : ASP and arrays?
vbuser1976
Mar 12th, 2001, 08:31 AM
Hi everyone!
I was wondering if someone can help me. I am thinking about using a 2 dimensional array (I think) to save some data. I am terrible with arrays. I need to get a review from someone how arrays work and some vbscript examples, if possible.
Thanks in advance.
André
Mar 12th, 2001, 11:48 AM
No problem:
dim MyArr(2,2)
MyArr(0,0)=1
MyArr(0,1)=2
MyArr(0,2)=3
MyArr(1,0)=4
MyArr(2,0)=5
etc etc
then:
for i=0 to ubound(Myarr,1)
for j=0 to ubound(MyArr,2)
response.write Myarr(i,j)
next
next
good luck
André
vbuser1976
Mar 12th, 2001, 12:12 PM
Okay, I understand what you have written. But, maybe I should specify what I am trying to do.I have an asp page with a data entry table with 5 rows and 7 columns. Each row constitutes a record with 7 fields. Basically, what I want to do is create a user-defined data type (that would be the 7 columns)and create an array to save the user entered data. I hope you can understand what I want to do. Is there a specific way to name the data entry fields of the table for this to work. I want to write a little code as possible.
Thanks again for your help.
André
Mar 12th, 2001, 01:52 PM
Ah, ok
you want to represent the data entry as a recordset?
why don't you create a custom recordset?
this way you can name your columns and iterate trough the records as you do with an ADODB.Recordset?
else
dim i 'row
dim j 'column
take the values of the first row and put them in their respective columns:
Array(0,0)=ValueOfRow1Column1
...
Array(0,6)=ValueOfRow1Column7
and so forth for each row
Does this help?
André
vbuser1976
Mar 12th, 2001, 02:13 PM
Yes, this is what I am looking for, but I have a stupid question: What is a custom recordset and how do you create one?
André
Mar 13th, 2001, 02:12 AM
I have done custom recordsets in VB, but not tested in VBScript, I always encapsulate my business logic into COM components, anyway this is the VB howto create your custom recordset:
Set rsCustomers = New ADODB.Recordset
With rsCustomers
' Set CustomerID as the primary key.
.Fields.Append "CustomerID", adChar, 5, adFldRowID
.Fields.Append "CompanyName", adChar, 40, adFldUpdatable
.Fields.Append "Address", adChar, 60, adFldUpdatable
.Fields.Append "City", adChar, 15, adFldUpdatable
.Fields.Append "PostalCode", adChar, 10, adFldMayBeNull
.Fields.Append "Country", adChar, 15, adFldUpdatable
.Fields.Append "Phone", adChar, 24, adFldUpdatable
' Use Keyset cursor type to allow updating records.
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open
End With
'Now you have a recordset, let's put values in it:
With rs
.AddNew
.Fields("CustomerID").Value="AMV"
.Fields("CompanyName").Value="PA"
.....
.Fields("Phone").Value="12345678"
.Update
End With
You can put that in a loop or whatever you like, at the end you will have an ADO Recordset yo can work with, as if you had executed a SELECT query!
Good Luck
André
Mar 13th, 2001, 02:18 AM
Actually, just the AddNew part should be in a Loop, you only need to define the recordset once,
Regards
André
vbuser1976
Mar 13th, 2001, 08:12 AM
Thanks on the short tutorial on custom recordsets. One other question though: How do you go through the table and assign the values to the right location, in other words, how do I do this(what is in bold):
Array(0,0)=ValueOfRow1Column1
I know how to do it with datagrids in vb (textmatrix etc). Any ideas on how to accomplish this. I am sorry I am so annoying. Thanks again for all your help
Oops, just noticed that you answered this. What I am really asking is how does vbscript knows that the table is where I want the custom recordset to work at?
André
Mar 14th, 2001, 02:21 AM
I guess you have a form right?
This form has 5 rows and 7 columns right?
Then you know exactly the names of the fields!
If you name the fields the same, then when you submit the form you will have an array of values!
for example an inout type text with the name CustomerID. by having 5 rows you will have an array CustomerID(4). You get this by request.form("CustomerID")(i)
So by doing it this way, you now have 7 column arrays, with their index referring to a row!
I would say that you have to massage the arrays and shape them into the recordset :)
André
vbuser1976
Mar 14th, 2001, 05:15 AM
Okay, so what I am getting from this is that if I have a table with rows and columns, in order for vbscript to understand I need for it to create an array, all fields in a column need to have the same name and then when I try to save it using "Arr(i)" it will understand it.
Hmm, sounds simple enough. Thanks for all your help and I will let you know if I got it working.
André
Mar 14th, 2001, 06:25 AM
Great,
cheers
André
vbuser1976
Mar 15th, 2001, 12:20 PM
Okay, now I need to bring the data back into the table. How do I do that? Of course, the database is not an array but it is bringing in the records I need. I just need to know how I put them back in the table in the right places. Any ideas? Remember, all the fields in a column have the same name.
Thanks in advance.
André
Mar 16th, 2001, 12:42 AM
Ok, you need to build a loop like this, after retrieving the recordset from the database:
response.write "<table>
Do until rs.EOF
response.write "<tr>"
response.write "<td><input type=text name=CustomerID value=" & rs.Fields("CustomerID").value & "></td>"
response.write "<td><input type=text name=CustomerName value=" & rs.Fields("CustomerName ").value & "></td>"
'etc etc.
response.write "</tr>"
rs.moveNext
Loop
response.write "</table>"
This should build your table with data from the recordset and with fields named the same.
Cheers,
André
André
Mar 16th, 2001, 12:44 AM
I saw a typo, the first response.write "<table> is missing the end quotation!! it should be "<table>"
André
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.