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
Solved

Column alias

Posted on 2004-08-27
7
1,144 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

792 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