Solved

Parse tags within string

Posted on 2006-07-20
5
233 Views
Last Modified: 2010-04-30
I have the following string I need to parse and assign the values to textbox's. It looks like the tags are formatted &dataname=. for example I need txtFname.text to = test from the below string.

dim str as string
str = "customer_id=255349&authorized=no&first_name=test&last_name=order&phone=5618662933&email=test%40test.com&product=50&username=&password=&shipping_address=1124+apollo+rd&shipping_unit=1124+apollo+rd&shipping_city=Melbourne&shipping_state=FL&shipping_zip=32935&shipping_country=US&billing_address=1124+apollo+rd&billing_unit=1124+apollo+rd&billing_city=Melbourne&billing_state=FL&billing_zip=32935&billing_country=US&cc_number=5454545454545454&cc_expires=2011-05-30+23%3A59%3A59&cc_name_on_account=test+order&cc_ccid=123"
0
Comment
Question by:Dalexan
[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
  • Learn & ask questions
5 Comments
 
LVL 13

Accepted Solution

by:
jmundsack earned 400 total points
ID: 17148740
(Warning: air code)

Dim arrVars() As String
Dim arrParts() As String
Dim intVar As Integer

arrVars = Split(str, "&")
For intVar = LBound(arrVars) To UBound(arrVars)
    arrParts = Split(arrVars(intVar), "=")
    Select Case arrParts(0)
        Case "customer_id"
            txtCustomerID.Text = arrParts(1)
        Case "authorized"
            txtAuthorized.Text = arrParts(1)
        'etc.
    End Select
Next intVar

0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 100 total points
ID: 17148759
Sub Parse()
    Dim str As String
    Dim i As Integer
    Dim settings() As String
    Dim setting() As String
   
    str = "customer_id=255349&authorized=no&first_name=test&last_name=order&phone=5618662933&email=test%40test.com&product=50&username=&password=&shipping_address=1124+apollo+rd&shipping_unit=1124+apollo+rd&shipping_city=Melbourne&shipping_state=FL&shipping_zip=32935&shipping_country=US&billing_address=1124+apollo+rd&billing_unit=1124+apollo+rd&billing_city=Melbourne&billing_state=FL&billing_zip=32935&billing_country=US&cc_number=5454545454545454&cc_expires=2011-05-30+23%3A59%3A59&cc_name_on_account=test+order&cc_ccid=123"
    settings = Split(str, "&")
    For i = 0 To UBound(settings)
        setting = Split(settings(i), "=")
        Select Case setting(0)
            Case customer_id
                'txtwhatever = setting(1)
            Case "first_name"
                txtFname.Text = setting(1)
            '...
        End Select
    Next i
End Sub

0
 

Author Comment

by:Dalexan
ID: 17148895
Any idea how I can split out the cc_name_on_account tag if there is more than two words

&cc_name_on_account=test+the+order

txtFname.text = test
txtMname.text = the
txtLname.text = order

0
 
LVL 13

Assisted Solution

by:jmundsack
jmundsack earned 400 total points
ID: 17148940
Just split it one more time, like so:

Dim arrVars() As String
Dim arrParts() As String
Dim intVar As Integer

arrVars = Split(str, "&")
For intVar = LBound(arrVars) To UBound(arrVars)
    arrParts = Split(arrVars(intVar), "=")
    Select Case arrParts(0)
        Case "cc_name_on_account"
            arrParts = Split(arrParts(1), "+")
            txtFname.Text = arrParts(0)
            txtMname.Text = arrParts(1)
            txtLname.Text = arrParts(2)
    End Select
Next intVar

0
 
LVL 9

Expert Comment

by:dancebert
ID: 17149205
Using this requires an array of your textbox names in the same order as they fields (e.g., customer_id) appear in the variable str.  You'd then assign the value to the textbox where the Debug.Print statements are.  This assumes that the order and number of fields in the input string doesn't change.


Private Sub Form_Load()
    Dim str As String
    str = "customer_id=255349&authorized=no&first_name=test&last_name=order&phone=5618662933&email=test%40test.com&product=50&username=&password=&shipping_address=1124+apollo+rd&shipping_unit=1124+apollo+rd&shipping_city=Melbourne&shipping_state=FL&shipping_zip=32935&shipping_country=US&billing_address=1124+apollo+rd&billing_unit=1124+apollo+rd&billing_city=Melbourne&billing_state=FL&billing_zip=32935&billing_country=US&cc_number=5454545454545454&cc_expires=2011-05-30+23%3A59%3A59&cc_name_on_account=test+order&cc_ccid=123"
    Dim parseEquals
    parseEquals = Split(str, "&")
    Dim ii As Long, jj As Long
    Dim KeyValue, parsePluses
    For ii = 0 To UBound(parseEquals)
        KeyValue = Split(parseEquals(ii), "=")
        If InStr(parseEquals(ii), "+") = 0 Then
            Debug.Print "Field: " & KeyValue(0) & ",  Value: " & KeyValue(1)
        Else
            parsePluses = Split(KeyValue(1), "+")
            For jj = 0 To UBound(parsePluses)
                Debug.Print "Field_" & CStr(jj) & ": " & KeyValue(0) & ",  Value: " & parsePluses(jj)
            Next
        End If
    Next

End Sub

'''''''''' output from above

Field: customer_id,  Value: 255349
Field: authorized,  Value: no
Field: first_name,  Value: test
Field: last_name,  Value: order
Field: phone,  Value: 5618662933
Field: email,  Value: test%40test.com
Field: product,  Value: 50
Field: username,  Value:
Field: password,  Value:
Field_0: shipping_address,  Value: 1124
Field_1: shipping_address,  Value: apollo
Field_2: shipping_address,  Value: rd
Field_0: shipping_unit,  Value: 1124
Field_1: shipping_unit,  Value: apollo
Field_2: shipping_unit,  Value: rd
Field: shipping_city,  Value: Melbourne
Field: shipping_state,  Value: FL
Field: shipping_zip,  Value: 32935
Field: shipping_country,  Value: US
Field_0: billing_address,  Value: 1124
Field_1: billing_address,  Value: apollo
Field_2: billing_address,  Value: rd
Field_0: billing_unit,  Value: 1124
Field_1: billing_unit,  Value: apollo
Field_2: billing_unit,  Value: rd
Field: billing_city,  Value: Melbourne
Field: billing_state,  Value: FL
Field: billing_zip,  Value: 32935
Field: billing_country,  Value: US
Field: cc_number,  Value: 5454545454545454
Field_0: cc_expires,  Value: 2011-05-30
Field_1: cc_expires,  Value: 23%3A59%3A59
Field_0: cc_name_on_account,  Value: test
Field_1: cc_name_on_account,  Value: order
Field: cc_ccid,  Value: 123

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month6 days, 6 hours left to enroll

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question