473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need to search each line in RTF field / return lines that contain specific string

Hello, esteemed experts!

I have a RTF field called "TxtMedications" that contains multiple lines of text, each of these obviously preceded by a <div> tag. For example:

Lacosamide 200 mg bid
Divalproex ER 1500 mg qhs
Pregabalin 50 mg q8h
Ibuprofen 400 mg tid PRN
Cetirizine 10 mg daily PRN

Or in string form...

<div>Lacosamide 200 mg bid</div>
<div>Divalproex ER 1500 mg qhs</div>
<div>Pregabalin 50 mg q8h</div>
<div>Ibuprofen 400 mg tid PRN</div>
<div>Cetirizine 10 mg daily PRN</div>

What I'm trying to do (and where I'm currently stuck) is separating out all lines which contain the string "PRN" into another RTF text box, called "TxtPRNMeds" and having the remainder put into a RTF text box called "TxtScheduledMeds".

I'm able to use the InStr() and Mid() functions to return the first string of text between the tags and search that string for "PRN".

Expand|Select|Wrap|Line Numbers
  1. Dim strAllMeds As String
  2. Dim strSearch As String
  3.   strAllMeds = Me.TxtMedications
  4.  
  5. strSearch = Mid(strAllMeds, InStr(1, strAllMeds, "<div>", vbTextCompare) + 5, InStr(1, strAllMeds, "</div>", vbTextCompare) - 6)
  6. If InStr(1, strSearch, "PRN", vbTextCompare) > 0 Then
  7.   'Add to TxtPRNMeds
  8. Else
  9.   'Add to TxtScheduledMeds
  10. End If
So in the above, I'm able to isolate the string "Lacosamide 200 mg bid" and determine that it does not contain the matching string "PRN" but that's about as far as I've gotten to this point. Any advice on how to search the subsequent rich text lines?

Thanks for any help.

Signed,
Perplexed
Mar 23 '19 #1

✓ answered by JenniferM

I played around with the loop code a little bit, and it looks like this solved the problem:

Expand|Select|Wrap|Line Numbers
  1. If Len(Nz(Me.TxtMedications, "")) > 0 Then
  2.   GoTo SplitValues
  3.   Else
  4.   Exit Sub
  5.   End If
  6.  
  7. SplitValues:
  8. Dim strArray() As String
  9. Dim PRNBuild As String
  10. Dim ScheduledBuild As String
  11. Dim var As Variant
  12.  
  13.  strArray() = Split(Me.TxtMedications, "</div>")
  14.  
  15.  For var = LBound(strArray()) To UBound(strArray())
  16.     If InStr(1, strArray(var), "PRN", vbTextCompare) > 0 Then
  17.      PRNBuild = PRNBuild & strArray(var) & "<div> "
  18.      Else
  19.      ScheduledBuild = ScheduledBuild & strArray(var) & "<div>"
  20.      End If
  21.  Next var
  22.  
  23.  
  24. If Len(ScheduledBuild) > 6 Then
  25.   TxtScheduledMeds = Left(ScheduledBuild, Len(ScheduledBuild) - 6)
  26.   Else
  27.   TxtScheduledMeds = ScheduledBuild
  28.   End If
  29.  
  30. If Len(PRNBuild) > 6 Then
  31.   TxtPRNMeds = Left(PRNBuild, Len(PRNBuild) - 6)
  32.   Else
  33.   TxtPRNMeds = PRNBuild
  34.   End If

Thanks for the help, Nauticalgent!!

10 1206
Nauticalgent
100 64KB
Dear Perplexed,

Just so I understand correctly, you have a Memo field (Long Text) that is formatted as RTF on the bound form? Also, where are the <div> tags coming from, are they imported from an external source?
Mar 23 '19 #2
Nauticalgent
100 64KB
Hi Perplexed...

A couple of ways to tackle this. Having never used RTF on a table or field level, I had to educate myself on the HTML tags.

I did a quick mock-up of your form and table and in my opinion the best (easiest way) to go about this would be to use the Split() function to populate and array to hold the data:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.     Dim strArray() As String
  3.  
  4.     strArray() = Split(Me.TextField, "</div>")
  5.  
  6.     'Debug.Print strArray(0), strArray(1)
  7.     ' This would result in:
  8.     ' <div>Lacosamide 200 mg bid 
  9.     ' <div>Divalproex ER 1500 mg qhs
  10. End Sub
Then you could use a For Each loop to examine each Array element for the "PRN" string and act accordingly.

My first inclination would have been to use Regular expressions, but in this case, that would be using a bazooka to kill a mosquito.
Mar 23 '19 #3
That's a clever way of doing it, and sounds like it'd be the most efficient way too. For clarification on your earlier post, I have a memo field formatted in rich text that needs to be split into two textboxes formatted with rich text (TxtScheduledMeds and TxtPRNMeds). For testing and whatnot, I'm using a rich text textbox, that I'm calling TxtMedications, which holds the string needing to be split.

I've implemented your technique and tried to loop through the array elements to find matching string.

Expand|Select|Wrap|Line Numbers
  1. If Len(Nz(Me.TxtMedications, "")) > 0 Then
  2.   GoTo SplitValues
  3.   Else
  4.   Exit Sub
  5.   End If
  6.  
  7. SplitValues:
  8. Dim strArray() As String
  9.  strArray() = Split(Me.TxtMedications, "</div>")
  10.  
  11.  Dim PRNBuild As String
  12.  Dim ScheduledBuild As String
  13.  
  14.  Dim element As Variant
  15.  For Each element In strArray
  16.    If InStr(1, element, "PRN", vbTextCompare) > 0 Then
  17.      PRNBuild = PRNBuild & element & "</div>"
  18.      End If
  19.    If InStr(1, element, "PRN", vbTextCompare) = 0 Then
  20.      ScheduledBuild = ScheduledBuild & element & "</div>"
  21.     End If
  22.  Next element
  23.  
  24. If Len(ScheduledBuild) > 6 Then
  25.   TxtScheduledMeds = Left(ScheduledBuild, Len(ScheduledBuild) - 6)  'Trim off extra "</div> string
  26.   Else
  27.   TxtScheduledMeds = ScheduledBuild
  28.   End If
  29.  
  30. If Len(PRNBuild) > 6 Then
  31.   TxtPRNMeds = Left(PRNBuild, Len(PRNBuild) - 6)  'Trim off extra "</div> string
  32.   Else
  33.   TxtPRNMeds = PRNBuild
  34.   End If
The code is recognizing all the array variants, but now the problem I'm running into is that it's adding each element to the PRNbuild string (even when there is no match). I can't seem to pinpoint the reason for this.

So after I run the above code, I get:
TxtScheduledMeds: (Null)

TxtPRNMeds:
Lacosamide 200 mg bid
Divalproex ER 1500 mg qhs
Pregabalin 50 mg q8h
Ibuprofen 400 mg tid PRN
Cetirizine 10 mg daily PRN
When I'm hoping to get:
TxtScheduledMeds:
Lacosamide 200 mg bid
Divalproex ER 1500 mg qhs
Pregabalin 50 mg q8h

TxtPRNMeds:
Ibuprofen 400 mg tid PRN
Cetirizine 10 mg daily PRN
Can anybody see where I'm messing this up?
Mar 24 '19 #4
I played around with the loop code a little bit, and it looks like this solved the problem:

Expand|Select|Wrap|Line Numbers
  1. If Len(Nz(Me.TxtMedications, "")) > 0 Then
  2.   GoTo SplitValues
  3.   Else
  4.   Exit Sub
  5.   End If
  6.  
  7. SplitValues:
  8. Dim strArray() As String
  9. Dim PRNBuild As String
  10. Dim ScheduledBuild As String
  11. Dim var As Variant
  12.  
  13.  strArray() = Split(Me.TxtMedications, "</div>")
  14.  
  15.  For var = LBound(strArray()) To UBound(strArray())
  16.     If InStr(1, strArray(var), "PRN", vbTextCompare) > 0 Then
  17.      PRNBuild = PRNBuild & strArray(var) & "<div> "
  18.      Else
  19.      ScheduledBuild = ScheduledBuild & strArray(var) & "<div>"
  20.      End If
  21.  Next var
  22.  
  23.  
  24. If Len(ScheduledBuild) > 6 Then
  25.   TxtScheduledMeds = Left(ScheduledBuild, Len(ScheduledBuild) - 6)
  26.   Else
  27.   TxtScheduledMeds = ScheduledBuild
  28.   End If
  29.  
  30. If Len(PRNBuild) > 6 Then
  31.   TxtPRNMeds = Left(PRNBuild, Len(PRNBuild) - 6)
  32.   Else
  33.   TxtPRNMeds = PRNBuild
  34.   End If

Thanks for the help, Nauticalgent!!
Mar 24 '19 #5
Nauticalgent
100 64KB
Well done JenniferM/Perplexed, glad I could help get you pointed in the right direction...

That being said, I have to ask: I am assuming the txtMedications field holds a list of medications for a patient.

If I have that correctly then your tables are not structured correctly. You should have table with a Primary Key (PK) for the patients and another table that hold the medications for each patient. The medication table would hava a Foreign Key (FK) that would correspond to that patient's PK - each record would hold one and only one prescription which would eliminate the need for this code altogether and you could do all of this with a simple query.

What you have now is the equivalent to a multi-value field which should be avoided...

Again, this all based on the accuracy of my assumption. I am shocked some of the other members have not descended upon you like a pack of wolves.
Mar 24 '19 #6
twinnyfo
3,653 Expert Mod 2GB
NauticalGent:
I am shocked some of the other members have not descended upon you like a pack of wolves.
I was away for the weekend. Good catch (and advice), NauticalGent!
Mar 25 '19 #7
Nauticalgent
100 64KB
Speaking of Wolves...thanks TwinnyFo.

P.S. How do you quote somebody on this forum? I have not been able to figure it out. HTML tags?
Mar 25 '19 #8
twinnyfo
3,653 Expert Mod 2GB
Left Square Bracket Quote Right Square Bracket Left Square Bracket Highlight Right Square Bracket NauticalGent: Left Square Bracket /Highlight Right Square Bracket Carriage Return Add all y'all's text Left Square Bracket /Quote Right Square Bracket
Mar 25 '19 #9
Nauticalgent
100 64KB
[TwinnyFo:]
Left Square Bracket Quote Right Square Bracket Left Square Bracket Highlight Right Square Bracket NauticalGent: Left Square Bracket /Highlight Right Square Bracket Carriage Return Add all y'all's text Left Square Bracket /Quote Right Square Bracket
Wow...that is a LOT of work!
Mar 25 '19 #10
twinnyfo
3,653 Expert Mod 2GB
NauticalGent:
Wow...that is a LOT of work!
Not really.....

;-)
Mar 25 '19 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: robboll | last post by:
Using MS Access 2003 I am looking for a function that will search the entire Tables Collection for a specific string in text or memo fields. For example if I enter "widget" it interrogates the...
1
by: Jackson M via AccessMonster.com | last post by:
I have buttons name A thur Z - When I select say the letter "H" button I need my ProductName field to jump to the record that starts with the letter "H" and soforth. Can anyone help -- This...
2
by: John Dalberg | last post by:
Hi What's the regex to remove the carriage return/line field from a string? These can occur multiple times in the string as in xxx\r\n\r\n. -- John Dalberg
1
by: jason | last post by:
The following vb code displays every line that has a particular string in it. How do I list every line that does not have that string. "!" does not seem to work for me and not sure where to place...
2
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field...
6
by: wayniac | last post by:
Hello, my name is Wayne and I would like to type in a textbox so that it may search the a table in that specific field for results. But I wish for it to display all of the fields in that query. ...
1
AutumnsDecay
by: AutumnsDecay | last post by:
Hey there everyone. I've been searching through the pages here and out on the internet, and I can honestly say I cannot find what I'm looking for. What I'm in need of, for a client of mine, is...
4
bugboy
by: bugboy | last post by:
Hi, I need to remove the return characters from a string but i can't figure out how to reference it in a replace() method. this.value=this.value.replace(/char(13)/, ""); Also when i test the...
1
by: David Michaels | last post by:
I have a field called Photos in a database of information scraped from public records. This field contains the Photo information if one is available, however, if no photo is available, it inserts a...
0
by: istimac | last post by:
General idea is to add ASCII data from text file to existing data in DataSet, first string of txt file is the same as DataSet column, but not in the same order. I try to search one colum for...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.