[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Column alias

Posted on 2004-08-27
7
Medium Priority
?
1,162 Views
Last Modified: 2008-02-01
Is it is possible to use data from one table as a column alias. Lets say I have a table Products with columns Id, Name Dim and DimText. I want to do something like this..

---
SELECT Name, Dim as [Products.DimText]
FROM Products
WHERE Id=2
---

This is not exactly what I want to do, but it illustrates the problem I have. I want to use the value stored in DimText as the column alias for the column Dim. Is this possible to do with SQL or do I have to do it using VBA?

/ aurion
0
Comment
Question by:aurion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:NicholasSmith
ID: 11912952
what you could do is create a temporary table using vba code which takes the values froma field and inserts it as the column name. let me know if you need help with the coding to do this.

Nic
0
 
LVL 5

Expert Comment

by:TheMek
ID: 11912982
Actually, this wouldn't make sense...you give an alias for a column. This alias is given only once for the query. So what would you expect to see there if you have more than one record in your table? A random pick from a Dimtext out of the records selected?
So the answer is no you can't do this, because it doesn't make sense. Maybe you should look into a so-called 'crosstable query' where the contents of a field are used as column headers. But it depends on your needs if this is suitable for you.

Cheers,
   Erwin
0
 
LVL 16

Expert Comment

by:Nestorio
ID: 11912990
Are you trying to update the Dim Text column from Dim column?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:Nestorio
ID: 11913041
If so you can do:

Update Products Set Dim = [Products.DimText]
WHERE Id=2
0
 
LVL 85
ID: 11913053
Erwin is correct, of course ... think about it ... you're trying to reference an object (your "naming" column) before it's actually created ...
0
 
LVL 16

Accepted Solution

by:
Chuck Wood earned 600 total points
ID: 11913354
Hi aurion,

It seems to me, if I understand what you want to do, that you are trying to change the column name for each row of data, based on the data in another field. If you only want to do that for one row, it is feasible. But, if you want to do it for multiple rows, you can't do that in a SQL statement, vba code, a query, or a table.

If you want to do it for just one row, you must use two SQL SELECT statements.

Example:

Public Sub TestCode()
Dim rst As New ADODB.Recordset, strSQL As String, strValue As String, fFound As Boolean
    strSQL = "SELECT [Dim Test] FROM Products WHERE Id=2"
    With rst
        .Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
        If Not .EOF Then
            strValue = .Fields(0)
            fFound = True
        End If
        .Close
    End With
    Set rst = Nothing
    If fFound Then
        strSQL = "SELECT Name, Dim AS " & strValue & " FROM Products WHERE Id=2"
        With rst
            .Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
            If Not .EOF Then
                Debug.Print .Fields(0).Name & "-" & .Fields(0) & ", " & .Fields(1).Name & "-" & .Fields(1)
                '   OR
                MsgBox .Fields(0).Name & "-" & .Fields(0) & ", " & .Fields(1).Name & "-" & .Fields(1)
            End If
            .Close
        End With
        Set rst = Nothing
    End If
End Sub
0
 

Author Comment

by:aurion
ID: 11929110
Thanks cwood-wm-com!

You have confirmed my suspicions that I have to do it using two queries. Also, when I wrote the question I should have explained what I was trying to do a little better. The reason I didn't do this was to focus on the problem I had. Obviously this cauesd some confusion, so I'll explain now instead.

Actually I have two tables (Products and ProductTypes). They are joined together using column 'Type' in the Products table. The 'DimText' column is actually placed in the ProductType table. Modifying the sql in the original question I would have written

---
SELECT a.Name, a.Dim as [b.DimText]
FROM Products AS a JOIN ProductTypes AS b ON a.Type=b.Id
where a.Type=2
---

As you can see, this is a complete different question. Let's say you have a product type 'Pipes'. The DimText property for that type would be 'Diameter', whereas another type could have a different name for the Dim column. Now I want to list these products by type, each of the types having its own alias for the Dim column.

Doing like this, I can list all products of one type on a form, using type specific naming of the Dim column.

Hope this clears it out for you, Thanks again!

/ aurion
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

656 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