Put a Record Set into an Array. Assign keys to the records in the array.

Sometimes, you may want to pull a record out of a recordset by its order number to use elsewhere because you don't know the primary key of the record or anything else about it. This is especially useful to know when your primary keys have gaps in the sequences where records have been deleted, or if your primary keys aren't numerical. (Keywords: Assign Array Keys to Record Sets recordsets ASP active server pages Access database)

I tried to figure out how to do this for a long time. I searched all over the web trying to find it, but it must have seemed too obvious to everyone for anyone to bother posting it phrased in just this way. One day, while driving in the truck, the solution came to me. If you like it, please rate.

Rated:
by Aspin.com users
What do you think?

 

 

 

Tutorial Menu - (scroll down for site menu)
Main Code (key to record benchmark) Assign rs variables to static keys Scripting Dictionary Object with rs keys rsKey used as array key numbers rs into simple array

Actual demonstration from database (note that the Primary Key numbers have a gap in their sequence):

rsKey=1 for file id 19
rsKey=2 for file id 20
Your record id for rsKey number 3 is 22
rsKey=4 for file id 23
rsKey=5 for file id 24
rsKey=6 for file id 25
rsKey=7 for file id 26
rsKey=8 for file id 27
rsKey=9 for file id 28
rsKey=10 for file id 29
rsKey=11 for file id 30
rsKey=12 for file id 31
rsKey=13 for file id 34
rsKey=14 for file id 35
rsKey=15 for file id 44
rsKey=16 for file id 47
rsKey=17 for file id 48
rsKey=18 for file id 49
rsKey=19 for file id 50
rsKey=20 for file id 51
rsKey=21 for file id 52


My selected rsKey is 3 and its record id is 22. That file belongs to Donna and the filename is georgia.JPG.
That line of text comes from this: response.write " That file belongs to " & rsNew("USER_ID") & " and the filename is " & rsNew("FILENAME")&"."

Here is the main code for assigning array keys to recordset items:
<%

'<--- First we get the database connection and sql statement taken care of and make the recordset
Dim conn ' ADO connection
Dim rs ' ADO recordset
Dim strDBPath ' path to our Access database (*.mdb) file
Dim rsKey
Dim MyRec
'dim CursorType,adOpenStatic
strDBPath = Server.MapPath("database_interface/gallery_demo/IMAGE_GALLERY.mdb")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"


strSQL = "SELECT * FROM IMAGEFILES ORDER BY FILE_ID " & ";"

'To pre-filter records comment out above and uncomment below with correct fields for keyID and OrderField
'strSQL = "SELECT * FROM IMAGEFILES WHERE FILE_ID ="& keyID &" ORDER BY " & OrderField & ";"

Set rsConn = Server.CreateObject("ADODB.Connection")

' Create recordset and set the page size
Set rs = Server.CreateObject("ADODB.Recordset")


' Open rs
rs.Open strSQL, conn,3,3, adCmdText

'<--- BEGIN where key numbers are assigned to items in the record set:
rs.movefirst  'going to the first record as a place to begin the array
x = 0
do until rs.eof
strID = rs("FILE_ID")
'response.write " " & strID & " "
'uncomment to have text here
x = x + 1 
' Setting up our array's key numbers

do until rs("FILE_ID") <> strID

if x = 3 then ' Which key do you want to work with? I chose 3.
response.write " Your record id for rsKey number 3 is " & rs("FILE_ID") & "<BR> "
rsKey = x  ' Here is where we BENCHMARK the key-to-record association
MyRec = rs("FILE_ID")
end if
if NOT x = 3 then
response.write " rsKey=" & x & " for file id " & rs("FILE_ID") & "<br> "
end if
rs.movenext
If rs.eof Then Exit Do
loop

'<--- END where key numbers are assigned to items in the record set.
response.write " " 'text here if you want

loop
response.write "</br><br>"
response.write "<b>My selected rsKey is " & rsKey &" and its record id is " & MyRec & "."

newSQL = "SELECT * FROM IMAGEFILES WHERE FILE_ID ="& MyRec & ";" 'The rsKey from above is used here to find one record by its ordinal number and then to filter the table for only that record
Set rsNew = Server.CreateObject("ADODB.Recordset")
rsNew.Open newSQL, conn,3,3, adCmdText
response.write " That file belongs to " & rsNew("USER_ID") & " and the filename is " & rsNew("FILENAME")&".</b>"

%>
<%
' Close DB objects and free variables
rsNew.Close
set rsNew = Nothing
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
%>
 


Assign variables from recordset to static keys


myArray(1) is 22 aka variable MybRec.
<%
Dim myArray(3)

myArray(0)=MyaRec
myArray(1)=MybRec
myArray(2)=MycRec
response.write "<br>myArray(1) is " & myArray(1) &" aka variable MybRec.<br>"


From W3 schools I adapted this code to put selected record keys into a new array using keys 2,3, and 4:

The Primary Keys of the items are:

20
22
23

The code using Scripting Dictionary Object, with the key variables that were set above.
<%
dim d,a,i,s
set d=Server.CreateObject("Scripting.Dictionary")
d.Add rsaKey, MyaRec
d.Add rsbKey, MybRec
d.Add rscKey, MycRec

Response.Write("<p>The values of the items are:</p>")
a=d.Items
for i = 0 To d.Count -1
s = s & a(i) & "<br>"
next
Response.Write(s)

set d=nothing
%>
Another array that shows the rsKey variables being used as key numbers:
<%
Dim disArray(4)
disArray(0) = "null" 'I chose to work with ordinals 1, 2, and 3, but array keys must begin with 0. I simply made the key(0) equal null. If I were using higher ordinals such as 11, 12, and 13, then I would be required to begin disArray( ) with 0 and to fill in all the keys between 0 and 11 with null as placeholders. If this were to be a completely dynamic array for which I wouldn't know how many keys, I might use select count or recordcount to get the total number of potential keys.
disArray(rsaKey) = MyaRec
disArray(rsbKey) = MybRec
disArray(rscKey) = MycRec
response.write "<BR>Here is key disArray(rsaKey): " &disArray(rsaKey)&"<BR>"
response.write"Here is a readout of the full array:<BR>"
for each i in disArray
if NOT i = disArray(0) then
response.write i& " - "
end if
Next
%>

Here is key disArray(rsaKey): 20
Here is a readout of the full array:
- 20 - 22 - 23 -
Here are our key variables placed into a simple array:
Here are the Primary Keys from our newRs recordset: 20, 22, 23,
Here is the code:
<%
Dim simpleArray
response.write "Here are the Primary Keys from our rsNew recordset: "
simpleArray=Array(MyaRec,MybRec,MycRec)
for each i in simpleArray
response.write i &", "
Next

%>

ASP Examples Menu of Active Server Pages scripts code Classic ASP

Classic ASP Active Server Pages Examples, code, tutorials, scriptsASP Examples Menu (below*)



Active Server Pages | ASP | Scripts | Tutorials | Code | Web Programming | Examples
Active Server Pages ASP code examples, tutorials, and snippets for use in Programming interactive websites.

NOTE: Recently changed hosting and url so some demos aren't working now because I haven't updated pathing; if you find omissions or errors let me know.
 

*

Multiple records per row with paging

Display multiple records per row

Get ID of newly inserted record, Classic ASP, Access Database

Javascript timer with ASP to keep track of time expired until login expires / timeout session is up

Multiple Javascript event functions triggered from single form onsubmit

Get Querystring key names along with querystring variables

ASP Youtube Downloader  Version FOUR
All ASP code. Allows user client side downloads of Youtube videos. You have the option to allow files to be saved directly to your site also.

ASP/PHP video downloader (Works better than ASP for large files)

Use ASP to dynamically parse a Youtube XML playlist to render to html.

Zip up an archive file of your site for backup if you need to restore website with ASP and free Zip Component.

Zip all files in a folder into an archive or backup file dynamically by specific file extension.

Sanitize or clean price cost field for database insertion or display.

Javascript image dimensions and image file size preview.

Put recordset into array, assign keys to the records then use record associated with specific array key as a variable elsewhere.

Access Database Interface Generator Wizard to read your table and field names and to create a table display.

Combining dropdown selection box menu with dynamic ASP database page.

How to redirect user to original page they tried to view before logging, after they are logged in.

Previous/Current/Next Records, or Buddy Ring Script

Custom ASP 404 page not found error script sends visitors to virtual folders

ASP Example: Combine ASP with Javascript for popup windows
dynamically sized to image dimensions.

ASP Example: How to use ASPJPEG to create thumbnails
Click for free Highly recommended thumbnailer for which you don't need Server access

ASPjpeg: How to re-scale aspect ratio of thumbnails by height instead of width.

ASP Example: What if your host has an older version of ASPJPEG that doesn't support gifs?

Using inline frames as "dynamic includes" (offline for updating)

Select Count Distinct Records for Access Database

Upload Image to folder and insert filename into database

Get date in such a way that it can be made part of a file name

Isolate a file name away from its path or extension or strip a path from a file name or strip characters from database field

User-customized on-the-fly stylesheet css files

Enhanced Page Hits Counter (also counts downloads and menu link hits)

Loop through array and compare to string with select case

Aphabetical Paging for your ASP scripts

Web Wiz Guide's Site Searcher Script modified as a file content indexer

Let Users Select How Many Records to View

Access Database for your PayPal IPN scripts

Miscellaneous collected code

You don't have to be a genius, just persistent, to write original Active Server Pages scripts

Software Recommendation: bare_bones_no_bells_and_whistles_asp_code_generator_database_interface_maker

--Lil at gmail.com