Solved

Column alias

Posted on 2004-08-27
7
1,156 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 200 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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…

705 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