Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-24-2007, 03:32 AM   PM User | #1
lse123
Regular Coder

 
Join Date: Dec 2005
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
lse123 is infamous around these parts
How to access an ADO field in a certain row of the recordset ?

How to access an ADO field in a certain row of the recordset ? If I have only one row may use oRS(1) for access field one , oRS(2) or oRS(field2) for access field two,....etc ? If a field of a recordset has value trip.[total expenses] where "trip" is the tablename and other part is fieldname, how to access this field, below is valid ?

totalexpensesTRIP1 = oRS("trip.[total expenses]")
lse123 is offline   Reply With Quote
Old 07-24-2007, 06:12 AM   PM User | #2
undertaker78
New Coder

 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
undertaker78 is on a distinguished road
<%=RecordSetName.Fields("FieldName").Value%>
undertaker78 is offline   Reply With Quote
Old 07-24-2007, 08:22 AM   PM User | #3
lse123
Regular Coder

 
Join Date: Dec 2005
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
lse123 is infamous around these parts
What if field name of record set [join 2 tables] has tablename and field name is two words like trip.[total expenses] ?
Also what if record set has 3rows how access value in 2nd row 2nd field ?
Also what if record set has 1row how access value in 2nd field, may use
oRS(2) or this is error ?
I mean without a do while loop....
lse123 is offline   Reply With Quote
Old 07-24-2007, 12:48 PM   PM User | #4
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Try not to use recordsets directly. Query the data, then get it into an array with getRows(). Arrays are much less work for the server than a big, complex Recordset object whose extended functionality you're never going to use.

But, to answer your questions:

1. I don't think that ADO will mind a space in the field name if you're referencing it the way undertaker78 suggests. But if it does, there are a couple of ways around it. One is to use the field index rather than the name, if you know it: RecordSetName.Fields(3).Value. The other is to use an alias in your SQL: SELECT [mytable].[myField] AS myAlias.

2. I don't know your application scope but if you're looking to jump around in data like that then I'd consider rethinking it a bit. You probably shouldn't have to go jumping to the nth record. But you can do so with the Move() recordset method. See http://www.devguru.com/technologies/ado/8642.asp

Of course, if you dump the recordset into an array, all of this is academic. You just reference array elements as you would with any other.
Spudhead is offline   Reply With Quote
Old 07-24-2007, 02:23 PM   PM User | #5
lse123
Regular Coder

 
Join Date: Dec 2005
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
lse123 is infamous around these parts
I mean without a do while loop... if record set has 1-row how access value in 2nd field, may use simple oRS(2) or this is error [without referencing row without loop] and needs to use recordsetobject.MoveFirst ? if I must use this statement tell me code 3-5 lines in asp/vbscript ?
lse123 is offline   Reply With Quote
Old 07-24-2007, 04:24 PM   PM User | #6
Daemonspyre
Regular Coder

 
Join Date: Mar 2007
Posts: 505
Thanks: 1
Thanked 19 Times in 19 Posts
Daemonspyre is on a distinguished road
If you are only pulling 1 row -- then yes, you can use oRS(2). However, remember that Recordsets are 0-based, so technically the second field would be oRS(1).

You can also use oRS("field_Name") where field_Name is the name of your column in your database (less code than oRS.Fields("field_Name").value)
__________________
Quote:
To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
Please, if you found my post helpful, pay it forward. Go and help someone else today.
Daemonspyre is offline   Reply With Quote
Old 07-24-2007, 06:48 PM   PM User | #7
lse123
Regular Coder

 
Join Date: Dec 2005
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
lse123 is infamous around these parts
To refer field: trip.[total expenses]
this is correct
totalexpensesTRIP1 = oRS("trip.[total expenses]")
lse123 is offline   Reply With Quote
Old 07-24-2007, 06:52 PM   PM User | #8
Daemonspyre
Regular Coder

 
Join Date: Mar 2007
Posts: 505
Thanks: 1
Thanked 19 Times in 19 Posts
Daemonspyre is on a distinguished road
If that is how your Access or SQL Server is returning the field name, then yes, that would be correct.

However, that being said -- save your sanity -- use aliases as Spudhead said in his post.
__________________
Quote:
To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
Please, if you found my post helpful, pay it forward. Go and help someone else today.
Daemonspyre is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:01 AM.


Advertisement
Log in to turn off these ads.