0

I am creating a table directly from the ADO recordset using field.name and field.value. As seen from another prior post Retrieve ADO Recordset Field names (Classic ASP).
Taking it a step further, I am trying to rename the table headers based on values from another database. Essentially if th = Column_1 then write "First Name" etc.

I can do this individually with a "if then" function with specific code. However, I have another data base that has all the proper heading names and would rather use while loop via a recordset. Instead of writing each line - columnheading1=x and columnheading2=y etc I would rather create a while loop.

Again the single "if then" statements work fine as noted below in the code, but the recordset does not.
I have tried to fix the recordset and the loop, but no good. Any ideas why the while loop (or repeat region) would not work in the function?

Below is the example:

    <table>
      <thead>
          <tr>

    <%
    Function Header_Name_Sub(Header_NameX)
    Header_Name_Write = Header_NameX
    If Header_Name_Write = "Project_File_ID" Then
    Header_Name_Write = "File ID" ' this works great.
    End If
    If Header_Name_Write = "Project_Assignment_Name" Then
    Header_Name_Write = "Project Name" ' this works great.
    End If

    ' Have a data base of all header names based on column name ... this repeat is not working in the function.  If I pull it out this section works fine elsewhere in the page...
Do While Not RS_Build_Numeric_Names.EOF
    If CStr(Header_Name_Write) = CStr((RS_Header.Fields.Item("True_Column_Name").Value)) Then ' thought maybe string issue is why CStr
    Header_Name_Write = (RS_Build_Numeric_Names.Fields.Item("Fixed_Column_Name").Value
    End If
Loop

    If Header_Name_Write = "Project_City" Then
    Header_Name_Write = "City" ' this works great.
    End If
    End Function
    %>      

             <%For Each fld in RS.Fields%>
            <% 
             Header_Name_Sub(fld.Name)
             %>
    <th><span><%=Header_Name_Write%></span></th>
             <%Next %>
          </tr>
      </thead>
       <tbody>
     <%
       Do Until RS.EOF
          OutputRow RS.Fields
          RS.MoveNext
       Loop
     %>
       </tbody>
    </table>
    <%
     Sub OutputRow(fields)
    %>
          <tr>
             <%For Each fld in fields%>
               <td><span><%=(fld.Value)%></span></td>
             <%Next %>
          </tr>
     <%
     End Sub
     %>

So here is the edit in simple form I just realized. Within the Function...

If Header_Name_Write = "Project_File_ID" Then
Header_Name_Write = "File ID" ' this works great.
End If

Works great above. I was trying to use the while loop to write the 50+ additional 'If Then's' When instead it was just looping through. So how can I write in the function the extra's?

If x = 1 Then Header_Name_Write = "File ID" End If
If x = 2 Then Header_Name_Write = "Next Header" End If
If x = 3 Then Header_Name_Write = "Another Header" End If

Is it a for loop instead?

A little further along...

For Each fld in RS_Build_Numeric_Names.Fields
If Header_Confirm = (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Column_Name").Value) Then 
Header_Name_Write = (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Name").Value)
End If
Next

If I use a for statment is does work for the first record only in the recordset... not the whole loop.

Community
  • 1
  • 1
Brewy
  • 53
  • 5

3 Answers3

0

since Header_Name_Sub() is a function, you should actually get it returning the result properly instead of using the Header_Name_Write variable. In your code, Header_Name_Write is expected to be a global variable, but since you do not declare that explicitly using a DIM statement, is may produce unexpected result.

You should try the below instead:

<%
  Function Header_Name_Sub(Header_NameX)
    Dim Header_Name_Write '' make it local here
    Header_Name_Write = Header_NameX
    .
    .
    .
    .
    Header_Name_Sub = Header_Name_Write '' assign the local value and return it
  End Function

  For Each fld in RS.Fields
    %><th><span><%=Header_Name_Sub(fld.Name)%></span></th><%
  Next
.
.
.
some1
  • 857
  • 5
  • 11
  • Thanks for the reply back, and I agree helps clean up the code some too. Still can't get the while to return the headers. Still trying! – Brewy Aug 20 '16 at 02:39
  • Just dawned on me why the loop won't work. Because it keep redefining the value within the loop. Doooop! I want the loop to write into the function all of the needed 'if then' statements... not replace the value. How can I accomplish that? – Brewy Aug 20 '16 at 02:54
0

Figured it 99% out. Was right in using a scripting dictionary to define the header names.

<table border="1px">
  <thead>
      <tr>

<%
Set Header_Options_All = Server.CreateObject("Scripting.Dictionary")
Do While Not RS_Header_Options_2.eof 'one RS Loop
    Header_Options_All.Add (RS_Header_Options_2.Fields.Item("Build_Project_Drop_Column_Name").Value), (RS_Header_Options_2.Fields.Item("Build_Project_Drop_Option_Name").Value)
    RS_Header_Options_2.movenext
Loop
Do While Not RS_Build_Numeric_Names.eof 'Another RS Loop
    Header_Options_All.Add (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Column_Name").Value), (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Name").Value)
    RS_Build_Numeric_Names.movenext
Loop
Header_Options_All.Add "Project_File_ID", "File ID" 'singles I added
Header_Options_All.Add "Project_ID", "ID"
Header_Options_All.Add "Project_Assignment_Name", "Project Name"
Header_Options_All.Add "Project_City", "City"
%>

<%
Function Header_Name_Sub(Header_NameX)
Dim Header_Name_Write
Dim Header_Confirm
Header_Name_Write = Header_NameX
Header_Confirm = Header_NameX 
For Each elem In Header_Options_All
Header_Name_Write = Header_Options_All.Item(Header_Confirm)
Next
Header_Name_Sub = Header_Name_Write
End Function
%>      

         <%For Each fld in RS_Full_List_Building_Inner_Join.Fields%>
<th><span><%=Header_Name_Sub(fld.Name)%></span></th>
         <%Next %>
      </tr>
  </thead>

Now I just can't get the others to fill in. Meaning if it doesn't match a name I want the default name. Close enough!

Brewy
  • 53
  • 5
0

Just dawned on me why the loop won't work. Because it keep redefining the value within the loop. Doooop! I want the loop to write into the function all of the needed 'if then' statements... not replace the value. How can I accomplish that? –

variable = variable & "new data"

Craig
  • 21
  • 2