Transform and Pivot in Access 2016

Wintersbite

I know there are topics on this, but the examples all go backwards from what I need to accomplish.

I have data in Access 2016 that looks like this:

Source Data

And I need it turned vertically so it looks like this:

Preferred Output

Etc. The student ID column never changes, but the number of questions does so I think it would have to be some kind of loop until end of record.

Powerquery does this easily in excel, but I need it to be native in access.

Alternatively, if someone can explain how to do this TRANSFORM and PIVOT in Access 2013 SQL backwards I may be able to finish it from there.

Normal pivot and unpivot won't handle this because I need to bring the column title down as a data field AND I need it to loop this move until the end of the record and won't know exactly how many columns there will be each time. So one time the file may go to Q07, and another go to Q43.

My skill level with access is amateur. I can do enough VBA to copy and modify code but not enough to write this in Access.

Thank you for your assistance!

Wintersbite

First I tried this.

SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q01' as Question, [Q01] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q02' as Question, [Q02] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q03' as Question, [Q03] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q04' as Question, [Q04] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q05' as Question, [Q05] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q06' as Question, [Q06] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q07' as Question, [Q07] as Response FROM Random_data_generator
ORDER BY StudentID, Question;

But I couldn't figure out how to do it dynamically for a random number of answers. So I blew the dust off my VB books and ended up with this which I'm sure is poorly coded and will make folks wince. But does work.

Dim db As DAO.Database
Set db = CurrentDb
Dim qdf As DAO.QueryDef

ColumnCount = CurrentDb.TableDefs("Random_data_generator").Fields.Count
ColumnCount2 = ColumnCount - 2
Dim QueryString As String
Dim QueryEntry As String
Dim counter As Integer
Dim counterEntry As String
Dim QueryTest As Recordset
Dim QuestionnaireNum As String
counter = 1


Do While counter <= ColumnCount2

 counterEntry = Format(counter, "00")

    QueryString = "SELECT StudentID, [QuestionnaireNum] as QuestionnaireID, 'Q" & counterEntry & "' as Question, [Q" & counterEntry & "] as Response FROM Random_data_generator UNION ALL " & vbCrLf
    counter = counter + 1
    QueryEntry = QueryEntry + QueryString

 Loop

 counterEntry = Format(counter, "00")

 QueryString = "SELECT StudentID, [QuestionnaireNum] as QuestionnaireID, 'Q" & counterEntry & "' as Question, [Q" & counterEntry & "] as Response FROM Random_data_generator ORDER BY StudentID, Question;"
 QueryEntry = QueryEntry + QueryString

 On Error Resume Next
 DoCmd.DeleteObject acQuery, "tempQry"
 On Error GoTo 0

 Set qdf = db.CreateQueryDef("tempQry", QueryEntry)

 DoCmd.OpenQuery ("tempQry")

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Converter consulta TRANSFORM / PIVOT do MS Access para SQL Server

分類Dev

ACCESS TRANSFORM COUNT / PIVOTからSQLSERVER2005に変換します

分類Dev

AutoCAD: Access group pivot in C#

分類Dev

動的な行と列を使用したPIVOTの作成-SQLServer 2016

分類Dev

Power Pivot2016でのDAX計算メジャー

分類Dev

Access 2016-ユーザーと権限?

分類Dev

Microsoft Access2016-レコード数

分類Dev

Access2016クエリの高速化

分類Dev

MS Access 2016Dcount動的機能

分類Dev

Is it possible to access column names within pandas groupby transform?

分類Dev

Is there still a way to fetch instagram feed without using access token now (06/2016)?

分類Dev

Access2016のクエリ内の計算フィールド

分類Dev

Access2016の2列のデータを比較する

分類Dev

クエリの結果の反対を取得するAccess2016

分類Dev

Access2016で最大値を持つ行に結合する

分類Dev

MS Access 2016:CTRL + Aでフォームを保存しますか?

分類Dev

MS-Access-2016クエリ言語 'ORDERBY'句

分類Dev

Access2016クエリのグループ別の合計

分類Dev

Access2016の変数selectステートメント

分類Dev

Microsoft Access2016フォームに写真が表示されない

分類Dev

MS Access2016のSQLServerLEADと同等の機能

分類Dev

Access 2016Switchboardはマクロをvbaに変換します

分類Dev

SQL 쿼리에서 콤보 상자를 사용하여 Access 2016

分類Dev

「Get&Transform」機能を使用してExcel2016にJSONをインポートできない

分類Dev

SQLServerでクエリTRANSFORM / PIVOTを定義するにはどうすればよいですか?

分類Dev

MS Access 2016、複合キーで複数のテーブルを結合

分類Dev

Access2016ランタイムエラーnullの無効な使用

分類Dev

MS Access2016でフォームとクエリを表示できません

分類Dev

Microsoft Access2016の要約とSQLによるテーブルのマージ

Related 関連記事

  1. 1

    Converter consulta TRANSFORM / PIVOT do MS Access para SQL Server

  2. 2

    ACCESS TRANSFORM COUNT / PIVOTからSQLSERVER2005に変換します

  3. 3

    AutoCAD: Access group pivot in C#

  4. 4

    動的な行と列を使用したPIVOTの作成-SQLServer 2016

  5. 5

    Power Pivot2016でのDAX計算メジャー

  6. 6

    Access 2016-ユーザーと権限?

  7. 7

    Microsoft Access2016-レコード数

  8. 8

    Access2016クエリの高速化

  9. 9

    MS Access 2016Dcount動的機能

  10. 10

    Is it possible to access column names within pandas groupby transform?

  11. 11

    Is there still a way to fetch instagram feed without using access token now (06/2016)?

  12. 12

    Access2016のクエリ内の計算フィールド

  13. 13

    Access2016の2列のデータを比較する

  14. 14

    クエリの結果の反対を取得するAccess2016

  15. 15

    Access2016で最大値を持つ行に結合する

  16. 16

    MS Access 2016:CTRL + Aでフォームを保存しますか?

  17. 17

    MS-Access-2016クエリ言語 'ORDERBY'句

  18. 18

    Access2016クエリのグループ別の合計

  19. 19

    Access2016の変数selectステートメント

  20. 20

    Microsoft Access2016フォームに写真が表示されない

  21. 21

    MS Access2016のSQLServerLEADと同等の機能

  22. 22

    Access 2016Switchboardはマクロをvbaに変換します

  23. 23

    SQL 쿼리에서 콤보 상자를 사용하여 Access 2016

  24. 24

    「Get&Transform」機能を使用してExcel2016にJSONをインポートできない

  25. 25

    SQLServerでクエリTRANSFORM / PIVOTを定義するにはどうすればよいですか?

  26. 26

    MS Access 2016、複合キーで複数のテーブルを結合

  27. 27

    Access2016ランタイムエラーnullの無効な使用

  28. 28

    MS Access2016でフォームとクエリを表示できません

  29. 29

    Microsoft Access2016の要約とSQLによるテーブルのマージ

ホットタグ

アーカイブ