Hello there. I am witting a program in vba/access and I have a string coming in. I need to go through the string and look for specific tags and return the rest of the line. So lets say:
"I went"
"to the"
"park"

I need to tell the program to look for "to" and return the string "to the" The string is from a letter and I need to pick out specific lines and fill them into an forum.
Thank you so much for your help!

Recommended Answers

All 7 Replies

Hello there. I am witting a program in vba/access and I have a string coming in. I need to go through the string and look for specific tags and return the rest of the line. So lets say:
"I went"
"to the"
"park"

I need to tell the program to look for "to" and return the string "to the" The string is from a letter and I need to pick out specific lines and fill them into an forum.
Thank you so much for your help!

I would say the first step is to put those lines of strings into a string array using kind of a loop. What type of loop and how to do this is kind of hard to help you with without more details.

Dim strInput() as String, intStringCount as integer
Dim strReceived as String
Dim strSearch as String, intCounter as integer
'  intStringCount = 0

While  '   ..............
' Read a line into your [I]strReceived[/I] variable

' Code to do the above

' dimension your string array
Redim strInput(intStringCount)
' Assign that line into the array
strInput(intStringCount) = strReceived

' increment your counter
intStringCount = intStringCount + 1
WEnd ' '''''''''''''''''''''''''''''''''

' Identify your tag

strSearch = txtSearch.text

' Loop through the string Array till you find the tag

for intCounter = 0 intStringCount
   ' Use one of string functions like Instr, Mid$, etc to find the tag
   if strSearch = txtSearch.text then
      ' Code to do what you need to do
      exit for
   Endif
next intCounter

Why loop on input when you can read whole file in in one step?

Public Function ReadFileReturnStringArray(PathFileName As String) As String()

Dim FileNumb As Integer,  FileContents As String

'get a free file handle
FileNumb = FreeFile

'open file
Open PathFileName For Binary As #FileNumb

'retrieve its contents
FileContents = Input(FileLen(PathFileName), #FileNumb)

'close file to reclaim resources asap
Close #FileNumb

ReadFileReturnStringArray = Split(FileContents, vbNewLine)

End Function

Then from calling proc

Public Sub CallingSub()

Dim MyArray() As String, UpperLimitOfArray As Integer, ForLoopCounter As Integer
Dim MySearchString As String, FoundSearchStringPosition As Integer

MyArray = ReadFileReturnStringArray("C:\YourPath\YourFile.Extension")

UpperLimitOfArray = Ubound(MyArray)

MySearchString = "Test"

For ForLoopCounter = 0 To UpperLimitOfArray
  
  FoundSearchStringPosition = InStr(1, MyArray(ForLoopCounter), MySearchString)
  If FoundSearchStringPosition > 0 Then
    
    Call SomeSubToProcessString(Mid(MyArray(ForLoopCounter), FoundSearchStringPosition))
    
  End If
  
Next ForLoopCounter

End Sub

Good Luck

Is there any way to do it without reading a file itself? I am reading in an email and I am trying to parse out certain xml tags within it and get all of the information behind it till the end of the line. Then I need to take the information for different tags and put them into an forum that is already created.

Right now I can post the string into a message and it is showing up in the same format as the email. I guess I was hoping that I could just parse out the string that shows in the message.

Sorry for the need for help. I am learning vba as I program in it.

The only way I have been able to read in the email is to put it into a string.

Is there any way to do it without creating a new file? ...
The only way I have been able to read in the email is to put it into a string.

Lindsey, you're right. You shouldn't make your task any harder than it should be. That's one of the first rules in programming: Keep it simple stupid (KISS). The intent of a software program should not be to impress the world with the programmer's brilliance, rather its intent shoule be to achieve a specific purpose.

Nevertheless, I would say that your first step should be to put the e-mail into a string array so that you could deal with each line separately. Your e-mail text probably has several lines and you must determine how to break that up into lines so that you can analyze each line separately.

You might post the value of that string variable that you read in your program. The answer to your problem lies within the structure of that value; but it's hard to advise you without that information.

Okay, then all you need is the last line of the readfile...

ReadFileReturnStringArray = Split(StringContents, vbNewLine)

Okay, then all you need is the last line of the readfile...

That's a great solution, vb5. Didn't know that there was a Split function. That function make sure makes life simple.

Returns a zero-based, one-dimensionalarray containing a specified number of substrings. MSDN documenation

Lindsey, that should easily solve the first requirement of splitting up the string into an array. Now you should be able to proceed to solve your remaining two dilemnas: (1) parse out the xml tags leaving your desired info, and (2) separating specific tagged lines to post in your forum.

After using the Split function and verifying that it returns the data for which you're looking, I would solve the second problem first. Then you can concentrate on removing the xml tags.
But just take it one step at a time. You can only solve one problem at a time. It's easy to become overwhelmed, if you try to solve the whole problem at once.

It's usually good practice to first write out what they call Psuedo Logic in your comments:

' Step 1: Convert String into Array
' Step 2: Separate Specific tagged lines
'   If ..... yada, yada, yada
' etc., .....

By the yard it's hard. By the inch it's a cinch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.