dbase: how to get fieldNAME ofa table

B

bart plessers

Hello,

below you will find a simple script that reads out info of a dbase
It reads out all the records and every field of the record.
For my table header, I want to display the field names of the table

can ASP extract this information? How? What is the correct syntax?

tia

bartp


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open CONN_STRING
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
iRecords = RS.RecordCount
iFields = RS.Fields.Count
%>

<table border="1" align="center">
<TR bgcolor="#CCCCCC">
<%For j = 0 To iFields-1 %>
<TD><%response.Write("&nbsp;")%></TD> --------> this should be
modified to display the table header
<%Next 'j%>
</TR>
<%For i = 0 To iRecords-1 %>
<TR>
<%For j = 0 To iFields-1 %>
<TD><%response.Write(RS.Fields(j))%></TD>
<%Next 'j%>
</TR>
<%RS.MoveNext%>
<%Next 'i%>
</table>

<%
RS.Close
Set RS = Nothing
Conn.Close
Set Conn = Nothing
%>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

--

==========================================
Hyper A.R.T.
bart plessers
Paul Van Ostaijenlaan 4
3001 Heverlee
BELGIUM
tel: +32 (16) 23.40.85
fax: +32 (16) 23.41.06
==========================================
 
R

Ray at

I would say that typically you would want to manually control what is
displayed as the column headers, so instead of displaying something like
"fname," you can have "First Name." But, I see what you're trying to do.
This is how you can do it.



<%
Dim oADOX
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open CONN_STRING
Set oADOX = Server.CreateObject("ADOX.Catalog")
oADOX.ActiveConnection = Conn
For Each col in oADOX.Tables("YourTableName").Columns
Response.Write "<td>" & col.Name & "</td>"
Next
Set oADOX = Nothing
'''your other code




%>


Ray at home
 
J

jbongran

bart said:
Hello,

below you will find a simple script that reads out info of a dbase
It reads out all the records and every field of the record.
For my table header, I want to display the field names of the table

can ASP extract this information? How? What is the correct syntax?

tia

bartp


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open CONN_STRING
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
iRecords = RS.RecordCount
iFields = RS.Fields.Count
%>

<table border="1" align="center">
<TR bgcolor="#CCCCCC">
<%For j = 0 To iFields-1 %>
<TD><%response.Write("&nbsp;")%></TD> --------> this should
be modified to display the table header
<%Next 'j%>
</TR>
<%For i = 0 To iRecords-1 %>
<TR>
<%For j = 0 To iFields-1 %>
<TD><%response.Write(RS.Fields(j))%></TD>
<%Next 'j%>
</TR>
<%RS.MoveNext%>
<%Next 'i%>
</table>

<%
RS.Close
Set RS = Nothing
Conn.Close
Set Conn = Nothing
%>

So you want the columns names ?
<%For j = 0 To iFields-1
Response.Write "<TD>" & RS.Fields(j).Name & "</TD>"
Next%>
 
B

Bart Plessers \(artabel\)

Ray,

In fact, I was looking for the solution of jbongran (other post on here)

But the solution you gave me is very interesting also.

However, it seems that the order of columns in oADOX.Tables("YourTableName")
does NOT correspond to the order in the table. My columns are shifted 1 to
the right

Here is my table:
IdGallery
Name
sortorder
descend

with oADOX.Tables("YourTableName") I get
descend - idGallery - Name - Sortorder

with Response.Write "<TD>" & RS.Fields(j).Name & "</TD>", I get
idGallery - Name - Sortorder - descend


Any idea why this happens


thanx again
regards
 
B

Bob Barrows

Bart said:
Ray,

In fact, I was looking for the solution of jbongran (other post on
here)

But the solution you gave me is very interesting also.

However, it seems that the order of columns in
oADOX.Tables("YourTableName") does NOT correspond to the order in the
table. My columns are shifted 1 to the right

Not exactly.
Here is my table:
IdGallery
Name
sortorder
descend

with oADOX.Tables("YourTableName") I get
descend - idGallery - Name - Sortorder

with Response.Write "<TD>" & RS.Fields(j).Name & "</TD>", I get
idGallery - Name - Sortorder - descend


Any idea why this happens
The column names are sorted alphabetically when retrieved from the schema.
There is another column that contains the ordinal position of the column
names being retrieved. Look it up in online help. If you don't have online
help on your pc, go to msdn/microsoft.com/library and use the tree menu on
the left to navigate to the appropriate documents in the Data node.

Bob Barrows
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top