Solved

Replace string replacing on itself

Posted on 2011-09-04
9
324 Views
Last Modified: 2012-05-12
I have a text field on a form... if a user enters Poly or Polycarb anywhere in the field I would like to replace it with Polycarbonate.

If I use this code the repalce doesn't provide the correct results.

Me.RefracAdd = Replace(Me.RefracAdd, "polycarb", "Polycarbonate")
Me.RefracAdd = Replace(Me.RefracAdd, "poly", "Polycarbonate")

Is this possible?
0
Comment
Question by:thandel
9 Comments
 
LVL 75
ID: 36480947

This should work - RefracAdd assumed to be your text box name.

Private Sub RefracAdd_AfterUpdate ()

Me.RefracAdd = Replace(Me.RefracAdd, "polycarb", "Polycarbonate")
Me.RefracAdd = Replace(Me.RefracAdd, "poly", "Polycarbonate")

End Sub

What results are you getting and/or where do you have the code?

mx
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 36480948
It should work if you use it in the AfterUpdate event.

/gustav
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36480952
This is something where I'd be tempted to use Regular Expressions.

1) Add this code to a regular VBA module:

Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
    Optional MultiLine As Boolean = False)
    
    ' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
    ' as long as you properly credit and attribute authorship and the URL of where you
    ' found the code
    
    ' For more info, please see:
    ' http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
    
    ' This function relies on the VBScript version of Regular Expressions, and thus some of
    ' the functionality available in Perl and/or .Net may not be available.  The full extent
    ' of what functionality will be available on any given computer is based on which version
    ' of the VBScript runtime is installed on that computer
    
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll
    ' controls whether all instances of the matched string are replaced (True) or just the first
    ' instance (False)
    
    ' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
    ' instead
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    ' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
    ' where a large number of calls to this function are made, making RegX a static variable that
    ' preserves its state in between calls significantly improves performance
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
        .MultiLine = MultiLine
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
End Function

Open in new window


2) In your form code, replace those two lines with:

Me.Refrac.Add = RegExpReplace(Nz(Me.RefracAdd, ""), "\bpoly(carb)?\b", "Polycarbonate", True, False)

For more about Regular Expressions, please see:

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36480954
I think you need a conditional statement so that the text comes out right:

If instr(1,me.refracadd, "polycarb") > 0 then
    Me.RefracAdd = Replace(Me.RefracAdd, "polycarb", "Polycarbonate")
else
    Me.RefracAdd = Replace(Me.RefracAdd, "poly", "Polycarbonate")
end if

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36480962
>>Me.RefracAdd = Replace(Me.RefracAdd, "polycarb", "Polycarbonate")
>>Me.RefracAdd = Replace(Me.RefracAdd, "poly", "Polycarbonate")

The problem with those lines, as I see it:

1) An original entry of "polycarb" becomes "Polycarbonatecarbonate"

2) An original entry of "Polycarbonate" becomes "Polycarbonatecarbonateonate"

I went with RegExp because it allows me to specify word boundaries, i.e., \b  :)
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36480985
Same thing happens if we do a test like:

>>If instr(1,me.refracadd, "polycarb") > 0 then

If the text already contains "Polycarbonate", we're going to get a bad replacement.

This might work:

If instr(1,me.refracadd, "polycarbonate") > 0 then
    ' do nothing!!!
elseIf instr(1,me.refracadd, "polycarb") > 0 then
    Me.RefracAdd = Replace(Me.RefracAdd, "polycarb", "Polycarbonate")
else
    Me.RefracAdd = Replace(Me.RefracAdd, "poly", "Polycarbonate")
end if

Open in new window


Of course, that will do us no good if this control contains long text which might have a mixture of polycarbonate, polycarb, and/or poly in the same entry...
0
 
LVL 44

Accepted Solution

by:
GRayL earned 500 total points
ID: 36481004
Private Sub AfterUpdate()
  Me!RefreactAdd = Switch(Me!RefractAdd="Poly","Polycarbonate",Me!RefractAdd="Polycarb","PolyCarbonate")
End Sub
0
 

Author Comment

by:thandel
ID: 36481528
Gary your above code works great.... As I did others forgot that the in string looks for any part so the others did not work.  The only issues with your solution is if the user enters polycabonatevthen it leaves the field empty.  So I added a check to see if polycarbnate was entered correctly initially.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36481888
thandel,

I am very, very confused.  You indicated in yur question:

>>if a user enters Poly or Polycarb anywhere in the field [emphasis added]

Ray's suggestion applied to the entire field, not anywhere in the field.

Did you even try my suggestion in http:#a36480952 ?

Patrick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now