Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Parse string function

Posted on 1998-10-12
Medium Priority
167 Views
I have 2 strings. In these strings I have values seperated by ;

Locations = "blablubla;bobbabbib;jamjam;namnam;"
Visits = "200;300;50;80;"

I need a function that can give me the X value of both strings.
Example:
strValue1 = Gime(Locations,2)
strValue2 = Gime(Visits,2)
will return this:
strValue1 = bobbabbib
strValue2 = 300
0
Question by:jfl
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 6

Accepted Solution

clifABB earned 80 total points
ID: 1439498
Public Function ParseString(sText as String, nSection as Integer)
Dim sTemp   As String
Dim nPtr    As Integer
Dim nCnt1   As Integer

For nCnt1 = 1 To nSection
nPtr = Instr(nPtr + 1, sText, ";")
If nPtr = 0 Then
ParseString = ""
Exit Function
End If
Next nCnt1
sTemp = Mid\$(sText, nPtr + 1)
ParseString = Left\$(sTemp, Len(sTemp) - Instr(sTemp, ";")
End Function
0

LVL 6

Expert Comment

ID: 1439499
Oops...
I tried to do this off the top of my head, but screwed up.

Here is the real working code (I tested it this time):

Public Function ParseString(sText As String, nSection As Integer)
Dim sTemp   As String
Dim nPtr    As Integer
Dim nCnt1   As Integer

For nCnt1 = 1 To nSection - 1
nPtr = InStr(nPtr + 1, sText, ";")
If nPtr = 0 Or nPtr >= Len(sText) Then
ParseString = ""
Exit Function
End If
Next nCnt1
sTemp = Mid\$(sText, nPtr + 1)
nPtr = InStr(sTemp, ";") - 1
ParseString = Left\$(sTemp, nPtr)
End Function

0

LVL 4

Expert Comment

ID: 1439500
Here's a little function that does the same sort of thing:

Function Token(LookIn As String, Delim As String, tkno As Integer) As String

' this function extracts token #tkno from a
' string of tokens (LookIn) separated by a delimiter (Delim).
' last character in (LookIn) *must* be a delimiter

Dim ptr As Long, ptr0 As Long, tptr As Long
ptr = 1
ptr0 = 1
tptr = 1

Do
ptr = InStr(ptr0, LookIn, Delim)

' if we can't find any more tokens
' check to see if this is the one we want
If ptr = 0 Then
If tptr = tkno Then
Token = Mid(LookIn, ptr0)
Else
Token = Empty
Exit Do
End If
Else
' do we have correct token?
If tptr = tkno Then

Token = Mid(LookIn, ptr0, ptr - ptr0)
Exit Do
End If
End If
tptr = tptr + 1
ptr0 = ptr + 1
Loop
End Function

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
###### Suggested Courses
Course of the Month10 days, 5 hours left to enroll