求めるソリューションの一般的な形式は、次のような UNION クエリを使用することです。
SELECT [country], [product], [id],
[1qtr 2010 flash] AS [Value],
2010 AS [Year],
1 AS [Qtr],
"flash" AS [Type]
FROM ImportedTable
UNION ALL
SELECT [country], [product], [id],
[2qtr 2010 flash] AS [Value],
2010 AS [Year],
2 AS [Qtr],
"flash" AS [Type]
FROM ImportedTable
UNION ALL
SELECT [country], [product], [id],
[3qtr 2010 flash] AS [Value],
2010 AS [Year],
3 AS [Qtr],
"flash" AS [Type]
FROM ImportedTable
...
このようなクエリを [ReformatDataFromExcel] のような名前で保存すると、新しい列を持つ更新された Excel フィードをインポートするときに、必要に応じて新しい UNION ALL 句をコピーして貼り付けることでクエリを更新できます。
- 編集 -
または、コードを使用して SQL クエリ文字列を作成する場合は、次のような VBA を使用します...
Sub BuildQueries()
Dim cdb As DAO.Database, fld As DAO.Field, sSQL As String
Dim sYear As String, sQtr As String, sType As String, a() As String
Set cdb = CurrentDb
For Each fld In cdb.TableDefs("ImportedTable").Fields
Select Case fld.Name
Case "country", "product", "id"
' do nothing
Case Else
a = Split(fld.Name, " ", -1, vbBinaryCompare)
sQtr = Left(a(0), 1)
sYear = a(1)
sType = a(2)
sSQL = _
"SELECT [country], [product], [id], " & _
"[" & fld.Name & "] AS [Value], " & _
sYear & " AS [Year], " & _
sQtr & " AS [Qtr], " & _
"""" & sType & """ AS [Type] " & _
"FROM ImportedTable"
Debug.Print sSQL ' or whatever you want to do with the query
End Select
Next
Set fld = Nothing
Set cdb = Nothing
End Sub
...次のような SQL 文字列を作成できます。
SELECT [country], [product], [id], [1qtr 2010 flash] AS [Value], 2010 AS [Year], 1 AS [Qtr], "flash" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [2qtr 2010 flash] AS [Value], 2010 AS [Year], 2 AS [Qtr], "flash" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [3qtr 2010 flash] AS [Value], 2010 AS [Year], 3 AS [Qtr], "flash" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [4qtr 2010 flash] AS [Value], 2010 AS [Year], 4 AS [Qtr], "flash" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [1qtr 2011 budget] AS [Value], 2011 AS [Year], 1 AS [Qtr], "budget" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [2qtr 2011 budget] AS [Value], 2011 AS [Year], 2 AS [Qtr], "budget" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [3qtr 2011 budget] AS [Value], 2011 AS [Year], 3 AS [Qtr], "budget" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [4qtr 2011 budget] AS [Value], 2011 AS [Year], 4 AS [Qtr], "budget" AS [Type] FROM ImportedTable