Solved

Column alias

Posted on 2004-08-27
7
1,139 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
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 16

Expert Comment

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

Update Products Set Dim = [Products.DimText]
WHERE Id=2
0
 
LVL 84
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

867 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

17 Experts available now in Live!

Get 1:1 Help Now