We help IT Professionals succeed at work.

Find txt after a string

mtrussell
mtrussell asked
on
I have an EDI message that I need to return the value in the 20C: line.  The lines will be stacked as below and the number of characters is variable before and after the line so the Left, Right, Mid, Instrg don't really help me out in this.  However the 20C: line is always on the message and the number of characters after 20C will always equal 15.

How would I be able to return the value 45029, for example, in the below message either in a query or vba?

{1:F01MIDLGB2992297029}{2:I543ESSENOKXXN}{3:{108:113137}}{4:
:16R:GENL
:20C::SEME//45029
:23G:NEWM
:16S:GENL
:16R:TRADDET
:98A::TRAD//20111110
:98A::SETT//20111115
:35B:ISIN NO0010985
STAL ASA
:16S:TRDET
:16R:FAC
:36B::SETT//UNIT/3493,
:97A::SAFE//097002601
:16S:FIAC
:16R:SETDET
:22F::SETR//TRAD
:16R:SETPRTY
:95P::REAG//SENOKXXXX
:16S:SETPRTY
:16R:SETPRTY
:95P::RECU//EMNL2A
:97A::SAFE//970736
:16S:SETPRTY
:16R:SETPRTY
:95P::BUYR//RBFRPP
:16S:SETPRTY
:16R:SETPRTY
:95P::PSET//VPOKK
:16S:SETPRTY
:16R:AMT
:19A::SETT//NOK5869,15
:16S:AMT
:16S:SETDET
-}{5:{CHK:6EC4F18}}
Comment
Watch Question

Solutions Architect
Commented:
In your example there are 13 characters after "20C:" (:SEME//45029).

SELECT Mid([MyField],InStr(1,[MyField],"20C")+11,15) AS TextStr
FROM MyTable;

Returns 45029 but assumes the string you want returned is always in the same position and same number of characters as your example...
Commented:
Try this
OM Gang


Public Function FindStringInFile()

On Error GoTo Err_FindStringInFile

    Dim objFSO As New FileSystemObject
    Dim objFile As Object
    Dim intSearchLen As Integer
    Dim lngPos As Long
    Dim strLine As String, strSearch As String
   
    strSearch = "20C"
    intSearchLen = Len(strSearch)
   
    Set objFile = objFSO.OpenTextFile("c:\temp\EDIMsg.txt", ForReading)
   
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        lngPos = InStr(strLine, strSearch)
        If lngPos <> 0 Then
            Debug.Print Right(strLine, (Len(strLine) - (lngPos + intSearchLen - 1)))
        End If
    Loop

Exit_FindStringInFile:
    objFile.Close
    Set objFile = Nothing
    Set objFSO = Nothing
    Exit Function

Err_FindStringInFile:
    MsgBox Err.Number & " (" & Err.Description & ") in procedure FindStringInFile of Module Module9"
    Resume Exit_FindStringInFile

End Function

Commented:
In my example, I'm returning everything after '20C'.  You can adjust the length of the piece of string you want to return by changing
(lngPos + intSearchLen - 1)

e.g.
(lngPos + intSearchLen + 3) will return 'ME//45029'
(lngPos + intSearchLen + 7) will return '45029'

As mvasilevsky pointed out, your example has 13 characters after the 20C but your Q stated the EDI file always has 15 after the 20C.  Adjust the expression to suit your needs.
OM Gang

Author

Commented:
I have never split points but this time you both gave me what I need in sql and vba.  both will be used in the program i am building.  

sorry for the split but in this case it is the only way i see to do this.

thanks.