Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2003, 08:59 AM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 166
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using fields

<%
If Not objRec.EOF Then

    Response.Write("<table>")
    Response.Write("<tr>")
    For Each objField In objRec.Fields
        Response.Write("<td>" & objField.name & "</td>")
    Next
    Response.Write("</tr>")
    While Not objRec.EOF
        Response.Write("<tr>")
        For Each objField In objRec.Fields
            Response.Write("<td>" & objRec(objField.Name)& "</td>")
        Next
        Response.Write("</tr>")
        objRec.MoveNext
    Wend
    Response.Write("</table>")

End If
%>

I have the above code which works fine, but I only want to show 5 fields in the table instead of all of them, there is too many. How can I modify the code to only show the field names and fields I want?
Thanks
__________________
-----------------------------------------------------------
\"Don\'t follow someone who\'s not going anywhere\" John Mason
 
Old November 20th, 2003, 10:40 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Very simply, you could change the query that generates the recordset so select only the field you want.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 20th, 2003, 10:54 AM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 166
Thanks: 0
Thanked 0 Times in 0 Posts
Default

objRec.Open "Books",strConnect,0,1,2

I am just using this to grab all the fields in the talbe.
How do I define certain field names here?

 
Old November 20th, 2003, 11:22 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, in that case, simply refer to the columns in the recordset by name:

Response.Write("<tr>")
  Response.Write("<td>" & objRec("FirstColumn")& "</td>")
  Response.Write("<td>" & objRec("ThirdColumn")& "</td>")
  Response.Write("<td>" & objRec("SecondColumn")& "</td>")
Response.Write("</tr>")
objRec.MoveNext

This will write out the values of the three columns. Note that you can refer to the column (as in this example) or by an index:

Response.Write("<tr>")
  Response.Write("<td>" & objRec(0)& "</td>")
  Response.Write("<td>" & objRec(2)& "</td>")
  Response.Write("<td>" & objRec(1)& "</td>")
Response.Write("</tr>")
objRec.MoveNext

In both examples I used a mixed column order, just to show you that it is possible.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old November 20th, 2003, 11:33 AM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 166
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar that works great, but how do I get the field names I want?

Response.Write("<td>" & objField.name & "</td>")

this line here is messing me up, I can fill the table with certain records but all the field names are still showing up. Or should I just make a table row in html for the headings and fill in in with only the columns I want?

Thanks

 
Old November 20th, 2003, 11:42 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Yes, that's what he's suggesting. I recall giving you the code that you first posted here. That's useful for showing everything in a table, but if you want a specific output, you need to construct like Imar shows in his post. In short:

<table>
    <tr>
        <td>header</td>
        <td>header</td>
        <td>header</td>
        <td>header</td>
    </tr>
    <% 'do your "for each row" loop here %>
        <tr>
            <td><%=recordset("column1")%></td>
            <td><%=recordset("column2")%></td>
            <td><%=recordset("column3")%></td>
            <td><%=recordset("column4")%></td>
        </tr>
    <% 'next row %>
</table>

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 20th, 2003, 11:49 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, I think it's an either / or situation. Either you use the For Each Field in Fields construct, or you should create the header manually.

The For Each is useful if you don't know the column names in advance. You should use the header solution if you want to display just a few columns, or if you want control over the way the labels are displayed (StartDate looks good in a database, but on your Web page you'd want something like Start Date).

Regards,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old November 20th, 2003, 12:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar...

I have been following this thread too, and I was wondering... I have never been able to extract the column names from an Access database, is that actually possible...!? I once did something like this...
Code:
Set recAtr = Server.CreateObject ("ADODB.Recordset")
recAtr.Open "SHOW COLUMNS FROM someTable", someDB
...using a MySQL databse which enabled be to make a table view; A view of the entire table where the headers where shown as in the database! Is it infact possible with Access aswell...!?

Really curious, best regards

Jacob.

 
Old November 20th, 2003, 12:13 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, it is. When you open a recordset, you not only get the data, but also the metadata; data about data.

This way you can retrieve column names, types, etc. Not every piece of information is supported by all drivers / databases, but AFAIK, you should be able to get the data from access like this:

' Create Connection
Set objRec = myConnection.Execute("SELECT * FROM MyTableInAccessDatabase")
If Not objRec.EOF Then
    Response.Write("<table>")
    Response.Write("<tr>")
    For Each objField In objRec.Fields
        Response.Write("<td>" & objField.name & "</td>")
    Next
    Response.Write("</tr>")
    While Not objRec.EOF
        Response.Write("<tr>")
        For Each objField In objRec.Fields
            Response.Write("<td>" & objRec(objField.Name)& "</td>")
        Next
        Response.Write("</tr>")
        objRec.MoveNext
    Wend
    Response.Write("</table>")
End If

I just copied and pasted the posted example so I can't guarantee it's bug free, but it should work like this.

Is this what you mean??

Imar



---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old November 20th, 2003, 12:43 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 166
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guys you are a big help!!

In regards to Jacob it does work in returning all the Field names and thier data in a table, I only want a couple columns and that's what they were helping me with, but the code above returns the whole table, well at least it worked for me, lol

Thanks again






Similar Threads
Thread Thread Starter Forum Replies Last Post
automate the fields anukagni Access 3 February 12th, 2007 08:45 AM
Looking Up Values In Other Fields meista Crystal Reports 0 December 11th, 2006 07:50 AM
(a)sociative fields icopec C++ Programming 0 March 12th, 2005 08:09 AM
Using Forum fields select fields on the fly hellosureshkumar Crystal Reports 0 December 17th, 2004 08:20 AM
Memo fields takabyte Classic ASP Basics 2 October 31st, 2003 06:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.