Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6723689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:35:55+00:00 2026-05-26T09:35:55+00:00

I have a table like this: cod_amb | field_a | field_b | field_c |

  • 0

I have a table like this:

    cod_amb | field_a | field_b | field_c | field_d | field_e

    21      | N.A.    | Fixed   | N.A.    | N.A.    | Natural   
    21      | Conif   | Temp    | N.A.    | >5 years| Artificial
    21      | N.A.    | Fixed   | N.A.    | N.A.    | Natural   
    24      | N.A.    | Fixed   | N.A.    | N.A.    | Natural   
    24      | NR      | Fixed   | N.A.    | <5 years| Natural   
    24      | N.A.    | Temp    | N.A.    | N.A.    | Natural   
    21      | N.A.    | Fixed   | N.A.    | N.A.    | Natural   

I have to get a resulting table like this:

    cod_amb | param   | value               |

    21      | field_a | N.A., Conif         |
    21      | field_b | Fixed, Temp         |
    21      | field_c | N.A.                |
    21      | field_d | N.A., >5 years      |
    21      | field_e | Natural, Artificial |  
    24      | field_a | N.A., NR            |
    24      | field_b | Fixed, Temp         |
    24      | field_c | N.A.                |
    24      | field_d | N.A., <5 years      |
    24      | field_e | Natural             |

Please, can anybody help me? Thanks in advance.
SQL for Access 2007

@@@@@@@@@@@@@@@@@@@@@@
UPDATE

I wrote this routine in Access:

Sub printSQL()
Dim db As Database
Set db = CurrentDb()
Dim tab As String
tab = "tab_011"
Dim rs1 As dao.Recordset
Set rs1 = db.OpenRecordset(tab)
Dim sqlAll As String
Dim str() As String
Dim i As Integer
i = 0
ReDim str(i)
Dim fld As dao.Field
For Each fld In rs1.Fields
    If fld.Name = "Area" Then
    ElseIf fld.Name = "lungh" Then
    ElseIf fld.Name = "id_elem" Then
    ElseIf fld.Name = "id_tass" Then
    ElseIf fld.Name = "x_coord" Then
    ElseIf fld.Name = "y_coord" Then
    Else
        str(i) = "SELECT cod_amb, '" & fld.Name & "' AS Param, " & fld.Name & " AS Val "
        i = i + 1
        ReDim Preserve str(i)
    End If
Next
Set fld = Nothing
i = 0
For i = 0 To UBound(str) - 1
    If i = 0 Then
        sqlAll = str(i) & "FROM " & tab & " UNION "
    ElseIf i > 0 And i < UBound(str) - 1 Then
        sqlAll = sqlAll & str(i) & "FROM " & tab & " UNION "
    ElseIf i = UBound(str) - 1 Then
        sqlAll = sqlAll & str(i) & "FROM " & tab & ";"
    End If
Next
Debug.Print sqlAll
End Sub

The output of the routine is a string like this:

SELECT tab_011.cod_amb, "type_veg" AS Param, type_veg AS Val
FROM tab_011
UNION
SELECT tab_011.cod_amb, "type_acq" AS Param, type_acq AS Val
FROM tab_011
UNION
SELECT tab_011.cod_amb, "status" AS Param, status AS Val
FROM tab_011
UNION
SELECT tab_011.cod_amb, "num_pnt" AS Param, num_pnt AS Val
FROM tab_011
UNION
SELECT tab_011.cod_amb, "terr" AS Param, terr AS Val
FROM tab_011;

Finally I run this string in Access as a Union Query and then I export the resulting table in Excel.
The resulting table is slightly different compared with the output shown above: the difference is that column Val contains single occurrence and not the concatenated string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:35:56+00:00Added an answer on May 26, 2026 at 9:35 am

    If you can use code, something like this will work for you:

    Dim rs As DAO.Recordset
    Dim db As Database
    Dim sql As String
    Set db = CurrentDb()
    
    Set rs = db.OpenRecordset("tblTable")
    
    sql = ""
    Dim fld As DAO.Field
    For Each fld In rs.Fields
        If fld.Name <> "cod_amb" Then sql = sql + "SELECT cod_amb, '" + fld.Name + "' AS fldName, " + fld.Name + " AS fldValue FROM tblTable UNION "
    Next
    Set fld = Nothing
    
    sql = Left(sql, Len(sql) - 6)
    
    MsgBox (sql)
    
    Form_tblTable.RecordSource = sql
    
    rs.Close
    Set rs = Nothing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table like this: <table> <tfoot> <tr><td>footer</td></tr> </tfoot> <tbody> <tr><td>Body 1</td></tr> <tr><td>Body
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |
I have a table like this: name code group john 12 smith 15 how
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
i have a table like this one: -------------------------------- id | name -------------------------------- 1 |
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have a table like this (using wordpress) +---------+----------+------------+ | meta_id | meta_key |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.