Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for specific text within a text box

Status
Not open for further replies.

grgimpy

Programmer
Nov 1, 2006
124
US
I have a text box on my form that is automatically filled in with comments based on calculations done in the form. I have several controls that calculate values and place comments into the same text box one after another.

If the user inputs the values incorrectly this may result in the wrong comments being placed in the text box. Once they re-enter the values correctly, I would like to write an IF statement that looks at the comments text box and clears it based on what is in the box.

Is there a way to delete only certain text out of a text box? i.e. write an IF statement that checks if a text box contains a specific sentence within several sentences then deletes that specific sentence.

Any help would be greatly appreciated.
 
You can use Instr() and Mid() functions to find and rebuild your text string, but why would you store 'calculated comments' in the first place?

If you are storing a series of 'historical comments', you might be better off putting them in a separate time-stamped table of comments.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
A specific example: An operator enters a pH value for a specific process. I have established control limits on the pH for this process and if the pH is outside of these limits, then a comment such as "The pH is above the Upper Control Limit" is inserted into the Comments text box. There may be a concentration value for the same process. If this concentration value is outside of the control limits, then another comment such as "The concentration is above the Upper control limit" is added to this text box.

After the user has entered both these values, they realize that pH value they entered was incorrect and the value is actually within the control limits. After the new value is entered, I want to remove the sentence "The pH is above the Upper Control Limit."

I'll try the Instr() and Mid() functions and see if I can get anywhere. Thanks for the suggestion.
 
The easiest way is probably
Code:
Text1.Text = Replace(Text1.Text, "The pH is above the Upper Control Limit." & vbCrLf, "")
The thing that I would wonder about is

- The entered pH is below the limit so you insert
[blue]"The pH is below the Lower Control Limit."[/blue]

- The user modifies the pH but it's too high so you insert
[blue]"The pH is above the Upper Control Limit."[/blue]

- The user changes it again and finally gets it right!!

Do you need some mechanism that removes all messages relating to pH when the user modifies the pH value (for example) and then appends new messages if appropriate?
 
Yes, I would remove all messages relating to pH.

This is how I envisioned the whole code process. Code is attached to the pH value text box for example. I would probably use the AfterUpdate routine. At the beginning of the sub routine, I would run a code to see if the text box contained any sentences about the pH and delete all of them. Then all the calculations would be done again to determine if the pH was in control and place new messages if appropriate, which is pretty much what you asked Golom, i think.

It's ok for values to be out of control, I just need to know about it. I don't want a bunch of wrong messages in the comments box b/c then the whole thing becomes useless. I don't expect the users to make tons of errors, but it does happen occasionally.
 
I take back what I said about the Replace function. I think it will work just fine. I only have 8 different sentences programmed into the code, so it should be pretty easy. Thanks for the input.
 
I need to trap the error "invalid use of null" when the comments box does not contain the specific sentence I am trying to replace. Any suggestions on the best way to do this?
 
Just concatenate a null string:
Replace(Me![comments box] [!] & ""[/!], ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top