Server Objects - DFWstangs Forums
 
LinkBack Thread Tools Display Modes
post #1 of 10 (permalink) Old 04-23-2003, 09:51 PM Thread Starter
 
Join Date: Mar 2003
Posts: 1,394
Server Objects

Ive only been messing with ASP and VBScript for about a month, so Im still new. I need to write a loop that will go through each field in a record and display the contents of that field. I figured I could use a line of code that looked like:
Code:
For Each myField in recordSet.Fields
   response.write "Name: " & myField.Type
Next
However, I keep getting an error when I run the page. I narrowed it down to the fact that I dont have the Fields object installed on the server. How can I install this? I have looked for a couple of days and come up with nothing. Its getting kind of frustrating.

Anyone know how to do this? Thanks in advance.
xtremcoder is offline  
Sponsored Links
Advertisement
 
post #2 of 10 (permalink) Old 04-23-2003, 10:50 PM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
not sure from your post what you want, but here is a stab at it:

' fieldEl.Name -> Field Name
' fieldEl.Value -> Field Content

strDSN = "Some DSN String"
strSQL = "Some SQL Query"

set con = Server.CreateObject("ADODB.Connection")
con.Open strDSN

'''''' skipping the error checking on connection open

set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, con

'''''' skipping the error checking on recordset retrieval

for each fieldEl in rs.fields
Response.Write "Field Name: " & fieldEl.Name & " - Field Value: " & fieldEl.Value
next


There isn't anything called a "Fields" object. Fields is a dictionary object that resides inside a recordset.
01WhiteCobra is offline  
post #3 of 10 (permalink) Old 04-23-2003, 10:55 PM Thread Starter
 
Join Date: Mar 2003
Posts: 1,394
Quote:
Originally posted by MSDN
Field Object
You use the Fields collection and Field object to access each data column of the current record. The Fields collection can be accessed through the Recordset object. You can use the Field object to retrieve the name, type, and values of each data field of the current record.

Simple Field Object Example
Sub FieldExample()
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field

Set rs = New ADODB.Recordset
' Open the recordset, specifying an SQL statement
' and a connection string.
rs.Open "Select * from authors", "DSN=pubs;UID=sa"

Debug.Print "Fields in Authors Table:" & vbCr

' Loop through each Field object in the
' Fields collection of the table and display properties.
For Each fld In rs.Fields
Debug.Print "Name: " & fld.Name & vbCr & _
"Type: " & fld.Type & vbCr & _
"Value: " & fld.Value
Next fld

' Close the recordset.
rs.Close

End Sub

I got your code until the For Each loop... I dont see where you declared fieldEl so I got lost there...
xtremcoder is offline  
 
post #4 of 10 (permalink) Old 04-23-2003, 10:57 PM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
Quote:
Originally posted by xtremcoder
I got your code until the For Each loop... I dont see where you declared fieldEl so I got lost there...
Are we doing VBScript or ASP+VBScript?


Dim strSQL, strDSN, rs, con, fieldEl
....
rest of code


As far as declaration, yes, I would declare each at the top of a source file, but for posting purposes, I'm lazy.
01WhiteCobra is offline  
post #5 of 10 (permalink) Old 04-23-2003, 10:59 PM Thread Starter
 
Join Date: Mar 2003
Posts: 1,394
ASP and VBScript, I thought I said but I guess not. Im gonna try that, see if it works. Lets see...
xtremcoder is offline  
post #6 of 10 (permalink) Old 04-23-2003, 11:10 PM Thread Starter
 
Join Date: Mar 2003
Posts: 1,394
hey man, it worked... sorta

i got this output:
Code:
Field Name: Name
Field Type: 202
Field Value: Juan
Field Name: Type
Field Type: 202
Field Value: Don’t Know
Field Name: Value
Field Type: 202
Field Value: 1
when i used this code:
Code:
for each fieldEl in recordSet.fields
    Response.Write "Field Name: " & fieldEl.Name & "" &" Field Type: " & fieldEl.Type & "" & " Field Value: " & fieldEl.Value & ""
next
if i wanted to display the contents of the field, id use the fieldel.value ???
xtremcoder is offline  
post #7 of 10 (permalink) Old 04-24-2003, 05:23 AM
¯\(º_o)/¯
 
AbecX's Avatar
 
Join Date: Nov 2001
Location: Las Colinas
Posts: 25,373
Use php, it makes more sense

AbecX is offline  
post #8 of 10 (permalink) Old 04-24-2003, 06:14 AM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
what are you trying to do?

for my example, using fieldEl.Name will give you the name of the field.

fieldEl.Value will give you the value of the field.

fieldEl.Type will give you the type of variant the field is. For example, your example return 202. 202 is a constant for a null terminated unicode character string.

Here is a function to return the field type (with common fields found in record sets):

add this to your file and change your line and you will get a descriptive name:

"Type: " & fld.Type & vbCr & _

to

"Type: " & TypeStr(fld.Type) & vbCr & _

function TypeStr(fdType)
select case fdType
case 0
TypeStr = "adEmpty"
case 16
TypeStr = "adTinyInt"
case 2
TypeStr = "adSmallInt"
case 3
TypeStr = "adInteger"
case 20
TypeStr = "adBigInt"
case 17
TypeStr = "adUnsignedTinyInt"
case 18
TypeStr = "adUnsignedSmallInt"
case 19
TypeStr = "adUnsignedInt"
case 21
TypeStr = "adUnsignedBigInt"
case 4
TypeStr = "adSingle"
case 5
TypeStr = "adDouble"
case 6
TypeStr = "adCurrency"
case 14
TypeStr = "adDecimal"
case 131
TypeStr = "adNumeric"
case 11
TypeStr = "adBoolean"
case 10
TypeStr = "adError"
case 132
TypeStr = "adUserDefined"
case 12
TypeStr = "adVariant"
case 9
TypeStr = "adIDispatch"
case 13
TypeStr = "adIUnknown"
case 72
TypeStr = "adGUID"
case 7
TypeStr = "adDate"
case 133
TypeStr = "adDBDate"
case 134
TypeStr = "adDBTime"
case 135
TypeStr = "adDBTimeStamp"
case 8
TypeStr = "adBSTR"
case 129
TypeStr = "adChar"
case 200
TypeStr = "adVarChar"
case 201
TypeStr = "adLongVarChar"
case 130
TypeStr = "adWChar"
case 202
TypeStr = "adVarWChar"
case 203
TypeStr = "adLongVarWCha"
case 128
TypeStr = "adBinary"
case 204
TypeStr = "adVarBinary"
case 205
TypeStr = "adLongVarBinary"
end select
end function
01WhiteCobra is offline  
post #9 of 10 (permalink) Old 04-24-2003, 04:18 PM
You lookin' at mah EYE?!
 
DarkWolf's Avatar
 
Join Date: May 2000
Location: Portland, OR
Posts: 8,316
Quote:
Originally posted by AbecX
Use php, it makes more sense



DarkWolf
Graphic Design / Photography / Web Design
DarkWolf is offline  
post #10 of 10 (permalink) Old 04-24-2003, 05:41 PM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
Haters.

But then again if I could as much programming something in PHP as I could in ASP, I'd probably convert.
01WhiteCobra is offline  
Sponsored Links
Advertisement
 
Reply

Bookmarks

Quick Reply
Message:
Options

Register Now



In order to be able to post messages on the DFWstangs Forums forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.

User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.

Password:


Confirm Password:
Email Address
Please enter a valid email address for yourself.

Email Address:
OR

Log-in










Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page
Display Modes
Linear Mode Linear Mode



Posting Rules  
You may post new threads
You may 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 On
Trackbacks are On
Pingbacks are On
Refbacks are On

 
For the best viewing experience please update your browser to Google Chrome