Solved

Parse tags within string

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

770 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