Solved

Custom headers

Posted on 2016-09-28
9
39 Views
Last Modified: 2016-09-29
Hi,

I was wondering if it is possible to display custom headers. I need to create an Excel report that display "user-friendly" headers.
What I use is something like this

Select username as "User Name", firstname as "First Name".....

It works fine to display all fields.  What if user can select the headers they want?  Is there a way to accomplish this goal?  thanks
0
Comment
Question by:mcrmg
  • 5
  • 4
9 Comments
 
LVL 57
ID: 41820690
<<It works fine to display all fields.  What if user can select the headers they want?  Is there a way to accomplish this goal?  thanks>>

 Not sure I understand the question, but there are two methods to do this:

1. Build the SQL on the fly, aliasing the field names as you showed.

2. Use a querydef, and set the Caption property on each of the fields.

  You can see this in the query designer by right clicking on a field to get it's properties.

<<What if user can select the headers they want? >>

 You'd use a form to collect that input and use for #1 or #2 above.

Jim.
0
 

Author Comment

by:mcrmg
ID: 41820837
At this point, users can pick their fields that they want. I combine all the fields into a comma delimited string and store them in a table. They can actually get the Excel report. The problem, again, is the headers.

I think I will need more directions.   In this case, which approach do you think is better?

If I have a string like this  userid, username, firstname, lastname, address

How can I alias the fields with different names?  thanks
0
 
LVL 57
ID: 41821505
<<   In this case, which approach do you think is better? >>

  Their both more or less equivenlant.  Depends on what your doing in code already as to which would work better.

<< I combine all the fields into a comma delimited string and store them in a table. >>

  You would probably change that part.   When you combine the strings to form the SELECT, that's the point where you would add the alias.

 strSQL = "SELECT " & <users first field choice> & " As " & <what user wants as a header> & ", " & <users 2nd field choice> & " As " & <what user wants as a header> & " FROM "

 and you'd end up with an SQL statement that looks just like what you posted in your question.

 If you post the code that is doing this and creates the spreadsheet, I can be more specific.

Jim.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:mcrmg
ID: 41821554
I am not sure if this is enough, user can select the fields they want from a listbox in asp.net page. Then I loop through the fields by adding "," in between, then save that string to a field in sql table. So, when generating Excel report, it will be like this

strSQL =  "SELECT " & db(field) & " FROM table"

    // Save Report
    protected void SaveReportbtn_Click(object sender, EventArgs e)
    {
        string TimeslotItems = "";
        ReportNameMsg.Text = "";

        if (ReportName.Text == "")
        {
            ReportNameMsg.ForeColor = System.Drawing.Color.Red;
            ReportNameMsg.Text = "Please enter a report name";
        }
        else
        {
            foreach (ListItem item in mainlist.Items)
            {
                TimeslotItems += item.ToString() + ","; // /n to print each item on new line or you omit /n to print text on same line
            }
            //testBox.Text = TimeslotItems;

            CreateReport(TimeslotItems);
        }
    }

Open in new window

0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 41821574
Not sure why I thought this was a Access/VBA question....no wonder you didn't understand what I was saying!  My apologies.

But you'd need to modify line 16 in the above adding in the aliases and then possibly modify CreateReport(), although I'm assuming your just passing the select list and it's building an SQL statement with that, so it will most likely work with no changes other than to line 16 of what you posted.

Jim.
0
 

Author Comment

by:mcrmg
ID: 41821590
No worries....     :)

Do you think it would work?  In the dropdown list, there will be a value and a text (where user will see on the page).  In order to get both fields of table and the "user friendly" fields, using a "*" in between them.  For example,
                    <asp:listbox ID="FollowUpReason" runat="server" Width="150px">
                    <asp:ListItem Value="">Select</asp:ListItem>
                    <asp:ListItem Value="username*User Name">User Name</asp:ListItem>
                    </asp:listbox>

I will have to parse this once pass to the function.  Is there a better way? thanks
0
 
LVL 57
ID: 41821615
<<Is there a better way? thanks>>

 Don't believe so.

<<username*User Name>>

  Your only job then would be to replace the * with " as "

Jim.
0
 

Author Closing Comment

by:mcrmg
ID: 41821617
thank you very much
0
 

Author Comment

by:mcrmg
ID: 41821631
oops...I left out another issue, Please let me know if I need to open a new question.

What if I need to do some data type conversion? such as

convert(varchar(50),cast(a.abc as money),1) as 'ABC',

--OR--

case a.abc                        
        when '1' then 'Yes'
        when '2' then 'No'  
        else ''
        end as 'ABC',


thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now