为包含分隔字符串的列中的每个项创建重复行

时间:2016-12-21 19:21:27

标签: ms-access ms-access-2010

我有一个带有列的表,用于存储由分号(;)分隔的时间块。我想为每个时间块创建一行,例如,给定

enter image description here

我想为每个时间段创建一行

enter image description here

如果可能,请告诉我。

修改

我尝试使用此查询

SELECT * INTO ImportedData
FROM (
  SELECT [SourceData].[Time block], [SourceData].[Work History Id],[SourceData].[Operation Code]
  FROM SourceData
  WHERE InStr([SourceData].[Time block], ';') = 0
  UNION ALL
  SELECT Left([SourceData].[Time block], InStr([SourceData].[Time block], ';') - 1),[SourceData].[Work History Id], [SourceData].[Operation Code]
  FROM SourceData
  WHERE InStr([SourceData].[Time block], ';') > 0
  UNION ALL
  SELECT Mid([SourceData].[Time block], InStr([SourceData].[Time block], ';') + 1), [SourceData].[Work History Id], [SourceData].[Operation Code]
  FROM SourceData
  WHERE InStr([SourceData].[Time block], ';') > 0) AS CleanedUp;

我也试过这个VBA代码而没有运气。

Public Sub addToTable()
    Dim rstObj As DAO.Recordset, dbObj As DAO.Database
    Dim InsertSQL As String
    Set dbObj = CurrentDb()
    Set rstObj = dbObj.OpenRecordset("Query1")
    Do While Not rstObj.EOF
        Dim memArr() As String
        memArr = Split(rstObj.Fields("Time block"), ",")
        For i = 0 To UBound(memArr)
            InsertSQL = "SELECT*INTO ImportedData(Time block, Work History ID) VALUES(""" & rstObj.Fields("Time block") & """, """ & memArr(i) & """)"
            DoCmd.RunSQL (InsertSQL)
        Next
        rstObj.MoveNext
    Loop
End Sub

1 个答案:

答案 0 :(得分:0)

在这里找到答案[在此输入链接描述] [1]

[1]:http://www.access-programmers.co.uk/forums/showthread.php?t=239727对于任何可能具有相同要求的人,我希望人们能够提供实际帮助,而不仅仅是标记答案或纠正问题的提问方式。