Solved

Column alias

Posted on 2004-08-27
7
1,138 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
Comment Utility
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
Comment Utility
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
Comment Utility
Are you trying to update the Dim Text column from Dim column?
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 16

Expert Comment

by:Nestorio
Comment Utility
If so you can do:

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

762 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

11 Experts available now in Live!

Get 1:1 Help Now