Solved

Parse tags within string

Posted on 2006-07-20
5
228 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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

685 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