Parsing Comma Delimited Lists

  • Thread starter Thread starter PucSpifo
  • Start date Start date
P

PucSpifo

Guest
Hey all,

I'm new to the forums and Access both...please be gentle :)

I have a field that contains a comma delimited list of 1 and 0 (ex. 0,1,0,0,0,1,0,1,0) and I need to parse the list and associate each position of the list to another string (ex. 1.1 - 0, 1.2 - 1, 1.3 - 0 etc.) and then have that data printed on a form. Any help is greatly appreciated.

Josh Johnston
 
Here's a quick function to get the nth element out of a delimited input string when each element is only 1 digit long:
Code:
Function ListElement(intElementNo As Integer, strInputString) As Integer
    'You might want to have it return the answer as string if you
    '   want to concatenate it later to a string

    ListElement = Mid(strInputString, intElementNo * 2 - 1, 1)
End Function
Usage:
Code:
ListElement(3,"0,1,0,0,0,1,0,1,0")
will return the number 0, the 3rd element in the comma-delimited string.

associate each position of the list to another string
Where is the other string coming from?
 
Last edited:
The following will extract each of the 1s and 0s from one string and associate it into the other string as you wanted. To test it, create a form with 2 text boxes, txtInput and txtOutput. Put this code in the afterupdate
event for txtInput. The results will appear in txtOutput.

Private Sub txtInput_AfterUpdate()
Dim lenVar As Integer, X As Double, strX As String, i As Integer
X = 1
lenVar = Len(Me!txtInput)
strX = Format(X, "0.0") & " - " & Left(Me!txtInput, 1)
For i = 3 To lenVar Step 2
X = X + 0.1
strX = strX & "," & Format(X, "0.0") & " - " & Mid(Me!txtInput, i, 1)
Next i
Me!txtOutput = strX
End Sub
 
maybe I missed something but the above seems rather over complicated.
The following takes two strings of the same array length and splits them out then concatenates them.

String1="1,2,3"
String2="1.1,2.2,3.3"

ArrString1=split(String1,",")
ArrString2=split(string2,",")

newstring=""

for i=0 to ubound(arrString1)
NewString= Newstring & arrString2(i) & "-" &arrString1(i) &","
next
Newstring=left(newstring,len(newstring)-1)
 

Users who are viewing this thread

Back
Top Bottom