Convert the query contents

naobao

Registered User.
Local time
Today, 11:07
Joined
Feb 13, 2014
Messages
99
m_box_id SHID
20240719144942 X807-TW-X-XXX-AB563(CS39#HC)
20240719144942 X807-X-X-XXX-EC499-TW
20240719144942 X807-X-X-XXX-AC383-TW
20240719144942 X675-X-X-XXX-HB065-TW
20240719144950 2N6U-XXXT-0900-123
20240719144950 2N6U-XXXT-0810-123
20240719144823 X909-TW-X-XXX-AB563(CS39-EC499-RTT84329


Can convert the query contents of MS Access into the following format?


m_box_id SHID
20240719144942 X807-TW-X-XXX-AB563(CS39#HC), X807-X-X-XXX-EC499-TW, X807-X-X-XXX-AC383-TW, X675-X-X-XXX-HB065-TW
20240719144950 2N6U-XXXT-0900-123, 2N6U-XXXT-0810-123
20240719144823 X909-TW-X-XXX-AB563(CS39-EC499-RTT84329
 
Search for concatenate. You will find code that lets you mush records together.
 
Last edited:
Or use a cross tab query. This allows you to convert rows into columns.
 
Cross tab query cannot achieve this effect.
If I use it, then yes.
SQL:
TRANSFORM 
   MIN(X.SHID) AS xy 
SELECT 
   X.m_box_id 
FROM 
   (
      SELECT 
         T.m_box_id, 
         T.SHID, 
         "F" & DCOUNT("*", "tblNaobao", "m_box_id = '" & T.m_box_id & "' AND SHID < '" & T.SHID & "'") + 1 AS PivotColumn 
      FROM 
         tblNaobao AS T 
   ) AS X 
GROUP BY 
   X.m_box_id 
PIVOT 
   X.PivotColumn
 
If I use it, then yes.
SQL:
TRANSFORM
   MIN(X.SHID) AS xy
SELECT
   X.m_box_id
FROM
   (
      SELECT
         T.m_box_id,
         T.SHID,
         "F" & DCOUNT("*", "tblNaobao", "m_box_id = '" & T.m_box_id & "' AND SHID < '" & T.SHID & "'") + 1 AS PivotColumn
      FROM
         tblNaobao AS T
   ) AS X
GROUP BY
   X.m_box_id
PIVOT
   X.PivotColumn
Thanks!
 
Here is a generic function that you can use:
Code:
Function Concatenate(pstrSQL As String, _
        Optional pstrDelim As String = ", ", _
        Optional pstrLastDelim As String = "") _
        As Variant
'   Created by Duane Hookom, 2003
'   Modified 6/30/2014 to correct some issues
'   this code may be included in any application/mdb providing
'   this statement is left intact
'   example
'   tblFamily with FamID as numeric primary key
'   tblFamMem with FamID, FirstName, DOB,...
'   return a comma separated list of FirstNames
'   for a FamID
'    John, Mary, Susan

'   ======= in a Query =========================
'   SELECT FamID,
'   Concatenate("SELECT FirstName FROM tblFamMem
'    WHERE FamID =" & [FamID]) as FirstNames
'   FROM tblFamily
'   ============================================

'   to get a return like Duane, Laura, Jake, and Chelsey

'   ======= in a Query =========================
'   SELECT FamID,
'   Concatenate("SELECT FirstName FROM tblFamMem
'    WHERE FamID =" & [FamID], ",",", and ") as FirstNames
'   FROM tblFamily
'   ============================================

'   If FamID is a string rather than numeric,
'    it will need to be delimited with quotes

'   ======= in a Query =========================
'   SELECT FamID,
'   Concatenate("SELECT FirstName FROM tblFamMem
'    WHERE FamID =""" & [FamID] & """", ",",", and ") as FirstNames
'   FROM tblFamily
'   ============================================


'======For DAO uncomment next 4 lines=======
'====== comment out ADO below =======
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim intCount As Integer
    Dim strLastValue As String
    Dim intLenB4Last As Integer     'length before last concatenation
    Set db = CurrentDb
    Set rs = db.OpenRecordset(pstrSQL)

'======For ADO uncomment next two lines=====
'====== comment out DAO above ======
    'Dim rs As New ADODB.Recordset
    'rs.Open pstrSQL, CurrentProject.Connection, _
        adOpenKeyset, adLockOptimistic
    Dim strConcat As String 'build return string 
    With rs
        If Not .EOF Then
            .MoveFirst
            Do While Not .EOF
                intCount = intCount + 1
                intLenB4Last = Len(strConcat)
                strConcat = strConcat & _
                .Fields(0) & pstrDelim
                strLastValue = .Fields(0)
                .MoveNext
            Loop
        End If
        .Close
    End With
    Set rs = Nothing
'====== uncomment next line for DAO ========
    Set db = Nothing
    If Len(strConcat) > 0 Then
        strConcat = Left(strConcat, _
            Len(strConcat) - Len(pstrDelim))
        If Len(pstrLastDelim) > 0 And intCount > 1 Then
            strConcat = Left(strConcat, intLenB4Last - Len(pstrDelim)) & pstrLastDelim & strLastValue
        End If
    End If
    If Len(strConcat) > 0 Then
        Concatenate = strConcat
     Else
        Concatenate = Null
    End If
End Function
 

Users who are viewing this thread

Back
Top Bottom