View Full Version : Server Objects
xtremcoder
04-23-2003, 10:51 PM
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:
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. :mad:
Anyone know how to do this? Thanks in advance.
01WhiteCobra
04-23-2003, 11:50 PM
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.
xtremcoder
04-23-2003, 11:55 PM
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...:confused:
01WhiteCobra
04-23-2003, 11:57 PM
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...:confused:
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.
xtremcoder
04-23-2003, 11:59 PM
ASP and VBScript, I thought I said but I guess not. Im gonna try that, see if it works. Lets see... :)
xtremcoder
04-24-2003, 12:10 AM
hey man, it worked... sorta
i got this output:
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:
for each fieldEl in recordSet.fields
Response.Write "Field Name: " & fieldEl.Name & "<br>" &" Field Type: " & fieldEl.Type & "<br>" & " Field Value: " & fieldEl.Value & "<br>"
next
if i wanted to display the contents of the field, id use the fieldel.value ???
AbecX
04-24-2003, 06:23 AM
Use php, it makes more sense ;)
01WhiteCobra
04-24-2003, 07:14 AM
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
DarkWolf
04-24-2003, 05:18 PM
Originally posted by AbecX
Use php, it makes more sense ;)
http://www.dfwstangs.net/ubb/icons/icon14.gif
:D
01WhiteCobra
04-24-2003, 06:41 PM
Haters.
But then again if I could as much programming something in PHP as I could in ASP, I'd probably convert.
vBulletin v3.0.6, Copyright ©2000-2008, Jelsoft Enterprises Ltd.