Question : Update Query to reverse items in a string

I'm looking for a update query  (or possible Access VBA) to permanently reverse items in a string.  The string is in text field names "PNUser10" in table "PN".  The items are separated by commas.  Below is an example of how PNUser10 currently displays data:
12G3, 12G4, FDA00034 KIT, FDA00108 VER 2.4.0, FDA00173 KIT, FDA00184, FDA00191_2, fda00194, FDA0021

I would like the string to look like this:
FDA0021, fda00194, FDA00191_2, FDA00184, FDA00173 KIT, FDA00108 VER 2.4.0, FDA00034 KIT, 12G4, 12G3

Reason behind this it that the user wants to see the latest data first.  Currently, it is on the end.  The string can get up to 255 characters long.

Below is just a simple query to display the data:
Code Snippet:
1:
2:
3:
SELECT PN.PNID, PN.PNPartNumber, PN.PNTitle, PN.PNDetail, PN.PNUser10
FROM PN
WHERE (((PN.PNUser10) Is Not Null));

Answer : Update Query to reverse items in a string

Add this UDF to a VBA module:


Function ReverseIt(InputStr As String)

    Dim arr As Variant
    Dim Counter As Long, Counter2 As Long
    Dim Result() As String

    arr = Split(Nz(InputStr, "") & " ", ",")
    ReDim Result(0 To UBound(arr))

    For Counter = UBound(arr) To LBound(arr) Step -1
        Result(Counter2) = Trim(arr(Counter))
        Counter2 = Counter2 + 1
    Next

    ReverseIt = Join(Result, ", ")

End Function.




Now, use that function in your query:

SELECT PNID, PNPartNumber, PNTitle, PNDetail, ReverseIt([PNUser10]) AS ReversedUser10
FROM PN
WHERE PNUser10 Is Not Null
Random Solutions  
 
programming4us programming4us