Finding Data From String (1 Viewer)

DMT Dave

New member
Local time
Yesterday, 22:40
Joined
May 2, 2018
Messages
2
Hi Guy's, can someone help with this one please, i can usually find data using InStr function, in this case, the data i am trying to retrieve is 2 lines after the First Name:

So with being unsure of how many characters after First Name: is the word Phil, so i am using x 2 vbCrlf after First Name, i can't get the name Phil, can somone help and put me right on this please ?

Code:
Dim fn() As String, ifn As Integer
            fn = Split(strMailMessage, vbCrLf)
                For ifn = 0 To UBound(fn)
                    If InStr(1, fn(ifn), "First Name:") > 0 Then
                    Me.txtName = Mid(fn(ifn), vbCrLf & vbCrLf)
            
                Debug.Print Mid(fn(ifn), 1) & vbCrLf & vbCrLf
                
                    End If
                Next ifn

This is the text box strMailMessage

Page submitted on: Contact Us - website name removed/web-agency-gb-contact-us/>



First name:

Phil


Last name:
 

June7

AWF VIP
Local time
Yesterday, 21:40
Joined
Mar 9, 2014
Messages
5,471
Have you set breakpoint and step debugged?

Try:

Mid(fn(ifn + 2), 1) & vbCrLf & vbCrLf

Also, add an Exit For in the If Then block to terminate loop when name is retrieved.
 
Last edited:

jdraw

Super Moderator
Staff member
Local time
Today, 01:40
Joined
Jan 23, 2006
Messages
15,379
Dave,

This what I see when I look at your example in Notepad++


Dave2023-09-14 16_19_58-_new 11 - Notepad++.png

A little test:

Code:
Sub daveTSep23()
    Dim s As String, i As Integer, x
    s = "First Name:" & vbCrLf & vbCrLf & "Phil" & vbCrLf & vbCrLf & vbCrLf & "Last Name:"
    x = Split(s, vbCrLf)
    For i = LBound(x) To UBound(x)
     If x(i) <> "" Then Debug.Print "x(" & i & ")  "; x(i)
    Next
End Sub

'additional info
'you could modify the if statement, as below, and just get the names you want--providing format is consistent.

' If x(i) <> "" And Not x(i) Like "*Name:" Then Debug.Print "x(" & i & ")  "; x(i)

Result:

x(0) First Name:
x(2) Phil
x(5) Last Name:
 
Last edited:

Josef P.

Well-known member
Local time
Today, 07:40
Joined
Feb 2, 2023
Messages
826
Or like this:
Code:
Debug.Print Replace(GetStringBetweenStrings(strMailMessage, "First name:", "Last name:"), vbCrLf, vbNullString)
Code:
Private Function GetStringBetweenStrings(ByVal StringToBeSeached As String, ByVal StartString As String, ByVal EndString As String)
  
    Dim StartPos As Long
    Dim EndPos As Long
  
    StartPos = InStr(1, StringToBeSeached, StartString) 
    if StartPos = 0 then
        exit function
    end if
    StartPos = StartPos + Len(StartString)

    EndPos = InStr(StartPos, StringToBeSeached, EndString)
    if EndPos = 0 then
        exit function
    end if

    GetStringBetweenStrings = Mid(StringToBeSeached, StartPos, EndPos - StartPos)
  
End Function
 
Last edited:

Eugene-LS

Registered User.
Local time
Today, 08:40
Joined
Dec 7, 2018
Messages
481
Debug.Print Replace ...
Or in this way:
Code:
Dim strMailMessage As String
Dim strRes As String
    
    strMailMessage = "Bla - Bla - Bla " & vbCrLf & vbCrLf & vbCrLf & "First name:" & _
        vbCrLf & vbCrLf & "Phil" & vbCrLf & "GOGO" & vbCrLf & vbCrLf & "Last name:"
    'Debug.Print strMailMessage
    strRes = Replace(GetStringBetweenStrings(strMailMessage, "First name:", "Last name:"), vbCrLf, " ")
    strRes = Replace(Trim(strRes), "  ", " ")
    Debug.Print strRes
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:40
Joined
Feb 28, 2001
Messages
27,186
I don't know how much effort you are willing to do here, but you can parse it using the class module I supplied for the purpose of string analysis.


If that is one continuous string with embedded CR/LF pairs and other string elements, the parser will help you pick everything apart. It IS an advanced technique that could quickly degrade from a finite automaton (i.e. well-behaved) to a non-finite automaton (i.e. get lost in loops) if you are not careful to assure that you always recognize when you have reached the actual end of the string.

The download is a zip file with the parser module code, but it also has a word document to explain how it works. As the document would tell you, it is oriented to ASCII text, not extended character sets.
 

ebs17

Well-known member
Local time
Today, 07:40
Joined
Feb 7, 2020
Messages
1,946
A web contact form is standardized and should always provide the data in the same structure. Therefore, you should always find contact entries in the same places, i.e. in the same lines and, as @jdraw shows, derived from the array created by split.

Therefore, one could immediately write as further use: Use x(2), x(7), x(12) ...

For more sophisticated parsing you can use regular expressions. For a search pattern, for example, the range "First Name:" would be recorded up to immediately before "Last Name:", the expressions "First Name:" etc. would be recorded as subpatterns.
This means you can then record both the field name and the associated content for each match and use both as a combination, e.g. to update a table with them.
So you should immediately deal with the entire content of the form and its return (if you want to automate).
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:40
Joined
May 7, 2009
Messages
19,243
another variation:
Code:
Public Function fnGetName(ByVal sText As String, ByVal sWhichName As String) As String
' sText         the string to search
' sWhichBame    the search string (in your sample it should be either: "First name:" or "Last name:")
'
'
' example:
'
' Me.txtName = fnGetName(strMailMessage & "", "First name:")
'
    Dim ipos As Long
    Dim var As Variant, v As Variant
    'check the length of the string
    sText = Trim$(sText & "")
    If Len(sText) < 1 Then Exit Function
    'check if sWhichname is in the string
    ipos = InStr(1, sText, sWhichName)
    If ipos = 0 Then Exit Function
    sText = Mid$(sText, ipos)
    sText = Trim$(Replace$(sText, sWhichName, ""))
    v = Split(sText, vbCrLf)
    For Each var In v
        var = Trim$(var & "")
        If Len(var) <> 0 Then
            fnGetName = var
            Exit For
        End If
    Next
End Function
 

bastanu

AWF VIP
Local time
Yesterday, 22:40
Joined
Apr 13, 2010
Messages
1,402
Have a look at my free utility that uses keywords or placeholders (i.e. First Name:) to parse webforms:
Cheers,
 
Local time
Today, 01:40
Joined
Sep 16, 2023
Messages
35
a$=Mid(fn(ifn), vbCrLf & vbCrLf)
This is not valid
Is:
a$=Mid(Strng, position from, number of characters)
 

DMT Dave

New member
Local time
Yesterday, 22:40
Joined
May 2, 2018
Messages
2
Thank you guy's, will need to come back to this in due course, i have unfortunately been with paramedics with my father for most of the week since posting, thanks you though and will look through suggestions soon
 

Users who are viewing this thread

Top Bottom