473,399 Members | 3,401 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,399 software developers and data experts.

array/recordset?

Lo all,

Ok - this is what I was aiming to do, and then I thought - naahhh, that cant
be right!

query database
results to recordset
results to array using GetRows
update values in one column in array
<BOING>
realised you cant sort an array easily..."hmmm, perhaps if I put the array
contents into a recordset I could sort them"

<DUH>

The above seems pretty stupid to me, ie, recordset, to array, to recordset -
where finally I *will* actually need them in an array anyway....

So, is it possible to update a recordset once its pulled from the database?
It's probably a stupid question, but its not something I've ever done before
so wasn't sure? If so, a small example would be handy...

I'm basically calculating a field which needs to be stored back against the
row whilst iterating through the whole lot.

Any info appreciated.

Regards

Rob
Jul 19 '05 #1
23 3221
Rob Meade wrote:
Lo all,

Ok - this is what I was aiming to do, and then I thought - naahhh,
that cant be right!

query database
results to recordset
results to array using GetRows
update values in one column in array
<BOING>
realised you cant sort an array easily..."hmmm, perhaps if I put the
array contents into a recordset I could sort them"

<DUH>

The above seems pretty stupid to me, ie, recordset, to array, to
recordset - where finally I *will* actually need them in an array
anyway....

So, is it possible to update a recordset once its pulled from the
database? It's probably a stupid question, but its not something I've
ever done before so wasn't sure? If so, a small example would be
handy...

I'm basically calculating a field which needs to be stored back
against the row whilst iterating through the whole lot.

Any info appreciated.

Regards

Rob


Why can't the calculation be done in the query used to open the recordset,
then use the calculated column in the Order By clause? That would be the
most efficient means of doing this.

Alternatively:
By opening the recordset using a LockType of adLockBatchOptimistic (4), and
setting the ActiveConnection property to Nothing, you can disconnect a
recordset from the database (you can even close and destroy the Connection
at this point if you wish - it's usually a good idea unless you plan on
using the connection later on in the page). This will allow you to update
the recordset without affecting the data in the database. And once you have
updated the data, you can use the recordset's Sort method to sort the
results before using getrows to convert it to an array.

Set rs=server.createobject("adodb.recordset")
rs.cursorlocation = adUseClient
rs.Open SQL, cn, ,adLockBatchOptimistic,adCmdText
set rs.activeconnection=nothing
cn.close:set cn=nothing

or, if you don't have the constants defined:
Set rs=server.createobject("adodb.recordset")
rs.cursorlocation = 3
rs.Open SQL, cn, ,4,1
set rs.activeconnection=nothing
cn.close:set cn=nothing

You can now do your updates, using the Update method.

If you wanted to save these changes back to the database, you would set the
activeconnection property back to an open connection, and call UpdateBatch
to send the changes to the database.
HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #2
"Bob Barrows" wrote ...

Hey ho Bob :o)
Why can't the calculation be done in the query used to open the recordset,
then use the calculated column in the Order By clause? That would be the
most efficient means of doing this.
Indeed it would - but - I dont know how in a SQL statement I can count the
total number of times each word form the search criteria is found in each
field, and across the 3 cases of all words / any words / exact phrase -
hence doing out of SQL and in ASP...
Alternatively:
By opening the recordset using a LockType of adLockBatchOptimistic (4), and setting the ActiveConnection property to Nothing, you can disconnect a
recordset from the database (you can even close and destroy the Connection
at this point if you wish - it's usually a good idea unless you plan on
using the connection later on in the page). This will allow you to update
the recordset without affecting the data in the database.
The column in question is what I like to call a fairy, it doesnt really
exist (but I guess it kinda does) :o)

ie, I select 4 real fields for example, and then I add one, like this :

SELECT websiteid, websitename, websitedesc, websitemeta, 0 AS Relevance
FROM....

its the Relevance field that will be updated(hopefully) in the ASP, once
each row is updated and sorted based on Relevance, I'll then drop it all
into an array for display to the page etc..
And once you have updated the data, you can use the recordset's Sort method to sort the results before using getrows to convert it to an array.


yep - thats what I want :)

I think my current connection string (used vastly across the entire site on
different pages to do different stuff etc) is causing me my current error :

Set objCommand2 = Server.CreateObject("ADODB.Command")
Set RS2 = Server.CreateObject("ADODB.Recordset")
objCommand2.CommandText = SQL2
objCommand2.CommandType = adCmdText
Set objCommand2.ActiveConnection = objConnection2
RS2.Open objCommand2,,adOpenKeySet, adLockOptimistic

Current error :

Microsoft OLE DB Provider for ODBC Drivers error '80040e21'
Multiple-step OLE DB operation generated errors. Check each OLE DB status
value, if available. No work was done.
/parasolit/statics/mainbody-search-results.asp, line 631
Is this my commands on the bottom line of the above that are causing the
problems with the update? Is it safe to change these to something else
without affecting the rest of the app? (maybe worth having a new connection
string just for this etc)...
Any more help appreciated,
Regards
Rob
Jul 19 '05 #3
Rob Meade wrote:
"Bob Barrows" wrote ...

Hey ho Bob :o)
Why can't the calculation be done in the query used to open the
recordset, then use the calculated column in the Order By clause?
That would be the most efficient means of doing this.
Indeed it would - but - I dont know how in a SQL statement I can
count the total number of times each word form the search criteria is
found in each field, and across the 3 cases of all words / any words
/ exact phrase - hence doing out of SQL and in ASP...


If it was SQL Server, this would be do-able in a stored procedure using a
temp table or table variable, but ... I think you're using Access ...?

Alternatively:
By opening the recordset using a LockType of adLockBatchOptimistic
(4), and setting the ActiveConnection property to Nothing, you can
disconnect a recordset from the database (you can even close and
destroy the Connection at this point if you wish - it's usually a
good idea unless you plan on using the connection later on in the
page). This will allow you to update the recordset without affecting
the data in the database.


The column in question is what I like to call a fairy, it doesnt
really exist (but I guess it kinda does) :o)

ie, I select 4 real fields for example, and then I add one, like this
:

SELECT websiteid, websitename, websitedesc, websitemeta, 0 AS
Relevance FROM....


Calculated fields are not updatable. So that leaves the
original-recordset-update solution out. This leaves three options.

1. Use javascript in your server-side code. Sorting an array is a one-liner
in javascript
2. Use an ad hoc recordset
3. Write code to sort the array

Here's a link to a post I made back in July in response to Robb Meade (was
that you?):
http://groups.google.com/groups?q=do...phx.gbl&rnum=1
Here's a link to another post I made in which I compared the performance of
techniques 2 and 3:
http://groups.google.com/groups?hl=e...output%2B%253F

HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #4
"Bob Barrows" wrote ...
If it was SQL Server, this would be do-able in a stored procedure using a
temp table or table variable, but ... I think you're using Access ...?
Nope - I am using SQL Server..

I'd really like to post up what I have on this page at the moment, but its a
bit big!
Calculated fields are not updatable. So that leaves the
original-recordset-update solution out.
SHIT! I've just spent ages trying to work out why this kept failing,
buggery bollox, wish I'd know that little bit of knowledge a while
back....cheers though Bob - saves me spending the night beating the server
with a stick!
This leaves three options. 1. Use javascript in your server-side code. Sorting an array is a one-liner in javascript

I'd rather not if I can do without it etc.
2. Use an ad hoc recordset
ad hoc recordset? Sounds interesting - whats one of them then?
3. Write code to sort the array

Here's a link to a post I made back in July in response to Robb Meade
(was that you?):


indeed it was :o)

Checking the links now...

Cheers

Rob
Jul 19 '05 #5
"Bob Barrows" wrote ...

http://groups.google.com/groups?q=do...phx.gbl&rnum=1

reading/trying the adhoc recordset etc...got a few errors, fixed the first
(i think) with the inclusion of the adovbs.inc file.
fixed the next by adding an equals sign in the rs.sort "data" line...
another by adding "'s to the animals in the brackets (array etc)

Have this now - but still causing an error

<!--#Include File="_includescripts/adovbs.inc"-->
<%
dim ar, i , rs
ar=array("dog","cat","bird","monkey")
response.write "Before Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next

Set rs=server.createobject("adodb.recordset")
'again, use the datatype that makes sense for your data
rs.fields.append "data",adVarChar,20
rs.open
for i = 0 to ubound(ar)
rs.addnew "data", ar(i)
next
rs.sort = "data"
i=0
do until rs.eof
ar(i) = rs(0)
i=i+1
loop
rs.close
set rs=nothing
response.write "After Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next
%>

Any ideas?

I'm obviously missing something....

Cheers

Rob

Jul 19 '05 #6
Rob Meade wrote:
"Bob Barrows" wrote ...

http://groups.google.com/groups?q=do...phx.gbl&rnum=1
reading/trying the adhoc recordset etc...got a few errors, fixed the
first (i think) with the inclusion of the adovbs.inc file.
fixed the next by adding an equals sign in the rs.sort "data" line...
another by adding "'s to the animals in the brackets (array etc)

Have this now - but still causing an error


What error? I can't see your screen ...
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #7
Oops - disregard the last about Sort being a method - brain fart here.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #8
"Bob Barrows" wrote ...
What error? I can't see your screen ...


Soz, working in loft, getting very cold up here now :o/

Microsoft VBScript runtime error '800a0009'
Subscript out of range: '4'
/parasolit/test.asp, line nn

its the 2nd line of

do until rs.eof
ar(i) = rs(0)
i=i+1
loop
Jul 19 '05 #9
Rob Meade wrote:
"Bob Barrows" wrote ...

http://groups.google.com/groups?q=do...phx.gbl&rnum=1
reading/trying the adhoc recordset etc...got a few errors, fixed the
first (i think) with the inclusion of the adovbs.inc file.
fixed the next by adding an equals sign in the rs.sort "data" line...


Wait a minute. Sort is a method, not a property. The "=" sign should not be
there.

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #10
"Bob Barrows" wrote ...
Oops - disregard the last about Sort being a method - brain fart here.


lol - ok - it didnt work without it anyway :)

Oh, and I'm trying to use what you have there as a basis for what I'm trying
to do - I've check the adovbs.inc file and I dont see an obvious replacement
for my SQL Server TEXT data type? Is it adLongVarChar - ie, loads of text is
fine?

Regards

Rob
Jul 19 '05 #11
Rob Meade wrote:
"Bob Barrows" wrote ...
What error? I can't see your screen ...


Soz, working in loft, getting very cold up here now :o/

Microsoft VBScript runtime error '800a0009'
Subscript out of range: '4'
/parasolit/test.asp, line nn

its the 2nd line of

do until rs.eof
ar(i) = rs(0)
i=i+1
loop

hmm. The other link contains tested code. You should probably look at that.
In the meantime, I'll debug this air code to see what's wrong ...
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #12
Rob Meade wrote:
"Bob Barrows" wrote ...

http://groups.google.com/groups?q=do...phx.gbl&rnum=1
reading/trying the adhoc recordset etc...got a few errors, fixed the
first (i think) with the inclusion of the adovbs.inc file.


Instead of adovbs.inc, see this: http://www.aspfaq.com/show.asp?id=2112

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #13
"Bob Barrows" wrote ...
hmm. The other link contains tested code. You should probably look at that. In the meantime, I'll debug this air code to see what's wrong ...


I changed a bit of it myself, to remove the bottom section (from i=0) and
just added a do while not rs.eof - response.write the values etc, and out
they popped...

I'm guessing the next bit is to dump them back into an array? Can one not
use getRows() there etc?
Jul 19 '05 #14
"Bob Barrows" wrote ...
Instead of adovbs.inc, see this: http://www.aspfaq.com/show.asp?id=2112


Will check in a bit, think its having a bad day :o) I got "There was an
error" at the top..

Rob
Jul 19 '05 #15
Rob Meade wrote:
"Bob Barrows" wrote ...
Oops - disregard the last about Sort being a method - brain fart
here.


lol - ok - it didnt work without it anyway :)

Oh, and I'm trying to use what you have there as a basis for what I'm
trying to do - I've check the adovbs.inc file and I dont see an
obvious replacement for my SQL Server TEXT data type? Is it
adLongVarChar - ie, loads of text is fine?


See http://www.able-consulting.com/ADODataTypeEnum.htm for a handy datatype
mapping chart.

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #16
Rob Meade wrote:
"Bob Barrows" wrote ...

http://groups.google.com/groups?q=do...phx.gbl&rnum=1
reading/trying the adhoc recordset etc...got a few errors, fixed the
first (i think) with the inclusion of the adovbs.inc file.
fixed the next by adding an equals sign in the rs.sort "data" line...
another by adding "'s to the animals in the brackets (array etc)

Have this now - but still causing an error


OK - this code is tested and works:
<%
dim ar, i , rs
ar=array("dog","cat","bird","monkey")
response.write "Before Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next

Set rs=server.createobject("adodb.recordset")
'again, use the datatype that makes sense for your data
rs.fields.append "data",adVarChar,20
rs.open
for i = 0 to ubound(ar)
rs.addnew "data", ar(i)
next
rs.sort = "data"
i=0
rs.MoveFirst
for i = 0 to ubound(ar)
ar(i) = rs(0)
if not rs.eof then rs.MoveNext
next
rs.close
set rs=nothing
response.write "After Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next
%>

HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #17
Rob Meade wrote:
"Bob Barrows" wrote ...
hmm. The other link contains tested code. You should probably look
at that. In the meantime, I'll debug this air code to see what's
wrong ...


I changed a bit of it myself, to remove the bottom section (from i=0)
and just added a do while not rs.eof - response.write the values etc,
and out they popped...

I'm guessing the next bit is to dump them back into an array? Can one
not use getRows() there etc?


No reason why not.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #18
"Bob Barrows" wrote ...
See http://www.able-consulting.com/ADODataTypeEnum.htm for a handy datatype mapping chart.


Fantastic link Bob - thanks :o)

Rob
Jul 19 '05 #19
"Bob Barrows" wrote ...
OK - this code is tested and works:


Plopped that on and yep running a treat, many thanks...ASPFAQ wouldnt fire
up from that link, although when I browsed to it and entered 2112 as search
criteria the article popped up, using that now - thanks again - oh, and the
link you posted and that of the article were the same - odd...

So, using that as an example I know have the following :

Set testRS = Server.CreateObject("ADODB.RecordSet")
testRS.Fields.Append "WebsiteID", adInteger
testRS.Fields.Append "WebsiteName", adVarchar, 20
testRS.Fields.Append "WebsiteDesc", adVarchar, 255
testRS.Fields.Append "WebsiteMetaKeywords", adText
testRS.Fields.Append "WebsiteMetaDescription", adVarchar255
testRS.Fields.Append "Relevance", adInteger

So I guess now I just iterate through my original database recordset,
populate this one, do what I need to do with the relevance field, sort the
new recordset, and then fire it out to an array.

Sound about right?

Rob
Jul 19 '05 #20
"Bob Barrows" wrote ...
No reason why not.


cool :)
Jul 19 '05 #21
"Rob Meade" wrote ...
testRS.Fields.Append "WebsiteMetaKeywords", adText


that should have read :

testRS.Fields.Append "WebsiteMetaKeywords", adLongVarChar, nnn

with regards to the nnn - does it matter what size i use here? For example,
I know the sizes of my varchar fields from my sql server database, but I do
not know exactly how many characters will be here, so would me putting the
maximum be a stupid thing to do or acceptable under the circumstances?

Cheers

Rob
Jul 19 '05 #22
Rob Meade wrote:
"Rob Meade" wrote ...
testRS.Fields.Append "WebsiteMetaKeywords", adText


that should have read :

testRS.Fields.Append "WebsiteMetaKeywords", adLongVarChar, nnn

with regards to the nnn - does it matter what size i use here?


No. The DefinedSize argument should be ignored for this datatype, since it's
a variable length datatype. I don't think you even have to supply it.

Do you have access to the ADO online help? The answers to many of your
questions can be found there:
http://msdn.microsoft.com/library/en...ireference.asp

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #23
"Bob Barrows" wrote ...
No. The DefinedSize argument should be ignored for this datatype, since it's a variable length datatype. I don't think you even have to supply it.
Thats what I was hoping, but it errors out when I dont.. :o/
Do you have access to the ADO online help? The answers to many of your
questions can be found there:

http://msdn.microsoft.com/library/en...ireference.asp

Thanks once again Bob for the link and info :o)

I've just managed to get my initial recordset into the new recordset, do
what I had to do with the relevance, and then dump it to the page - bloody
hell! Needs a bit of work still, and only working for 'any words' at the
moment - as my brain appears to have turned to slush - but getting there...

Can't guarantee I'm done with this thread yet (bet you can! :D) - but I just
wanted to say a big thank you for all of the help you've given me this
afternoon/evening - its very much appreciated.

With regards to the library file I'll be trying to use that in future, I
assume it contains all of the info needed that I was using before, so can
just dump my adovbs.inc's and use that instead...

Thanks again,

Rob
Jul 19 '05 #24

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

Similar topics

11
by: Laphan | last post by:
Hi All I'm using .getRows() with a local var array instead of doing a recursive loop so that I'm being a good ASP newvbie and closing my object i/o's (the recordset in this case) as quick as...
19
by: MMMMM | last post by:
Hmmm, my array seems to be emptying itself for no reason. I just want to grab every form element posted to the page into an appropriately sized array... What is going on here? dim arrShort()...
5
by: deko | last post by:
I use a For Each... Next loop like this: For Each varFnm In Array("This", "That", "OtherThing", "Foo", "Bar") RunSql ("UPDATE.... bla bla bla) Next But the exact same elements of the Array...
5
by: eliffman | last post by:
I'm trying to populate an array with the records from a recordset. But I'm getting an error. Here's the code: Dim db As Database, qd As QueryDef, rs As Recordset Dim MyArray() As Double, i As...
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
6
by: Les Juby | last post by:
I need to extract records from a database subject to conditions and only thereafter give the users the choice of which fields to sort the results on. In this situation I can't write back to a...
2
by: Marc | last post by:
Hi the below code works fine. however as i wont always know that the objloc array has 4 parameters...how can i modify this code so that the array is dynamic?? Sub MapSelectedProperties() Dim...
10
by: akselo | last post by:
Hi folks, I am working on a routine that will select a sample of parcels from a table. Each parcel belongs to a census tract, and depending on which tract, a certain calculation is applied. The...
13
by: ChrisD76 | last post by:
Hi, I am new to using VBA, and have been working with DAO Recordsets. I'm working on a little problem, and think that DAO Recordsets are the solution. I've been playing around with them to try and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.