troubleshooting Question

Replace Exact word string procedure failing

Avatar of Fordraiders
FordraidersFlag for United States of America asked on
Visual Basic.NET
19 Comments1 Solution313 ViewsLast Modified:
vb.net 2003

Problem:
'   THIS FUNCTION WILL NOT CHANGE ANYTHING ?
when I call it


2 Tables:
Table 1
tblData
4 Fields
fldDId - Text   20
fldMfgname - Text 150
fldMfrnum - text  150
fldDescription - text 255
example:
0001  |LAWSON|12324R5|LAMP INCAND.  60WATTS
0002  |LAWSON|32DE45|1.A 2.B D.5 SPEEDBIT DRILLBIT
0003  |UNION BUTTEFIELD|435656| SOCKET HEAD CAP SCREW  1/2" X 3/4" LENGTH
0004  |LAWSON|12324R5|BULB , 100AMP
0005  |LAWSON|32DE45|DRILLBITS  3/4" X 6" LGTH

Table 2
tblCriteria
3 fields
fldCid
fldFindMe - text  150
fldReplaceWith -text 150
example data:
fldFindMe       fldReplaceWith
INCAND.        INCANDESCENT
BULB              LAMP

SO DATA BECOMES:
0001  |LAWSON|12324R5|LAMP INCANDESCENT  60WATTS '<---------- CHANGE HERE
0002  |LAWSON|32DE45|1.A 2.B D.5 SPEEDBIT DRILLBIT
0003  |UNION BUTTEFIELD|435656| SOCKET HEAD CAP SCREW  1/2" X 3/4" LENGTH
0004  |LAWSON|12324R5|LAMP , 100AMP  '<---------- CHANGE HERE
0005  |LAWSON|32DE45|DRILLBITS  3/4" X 6" LGTH


for Each drc As DataRow In tblCriteria.Rows
    For Each drd As DataRow In tblData.Rows
    drd("fldDescription") = FindAndReplace(drd("fldDescription"), drc("fldFindMe"), drc("fldReplaceWith"))
            Next
        Next

Private Function FindAndReplace(ByVal SearchText As String, ByVal Token As String, ByVal ReplaceText As String) As String
        Dim String1 As String
        Dim String2 As String
        Dim x As Integer

        For x = 0 To Len(SearchText)
            If x + Token.Length > SearchText.Length Then
                x = SearchText.Length - Token.Length
            End If
            If SearchText.Substring(x, Token.Length) = Token Then
                If x - 1 >= 0 Then
                    If IsNumeric(SearchText.Substring(x - 1, 1)) Then
                        String1 = SearchText.Substring(0, x)
                        String2 = SearchText.Substring(x + Len(Token))
                        Return String1 & ReplaceText & String2
                    End If
                End If
            End If

            If x + Token.Length = SearchText.Length Then Exit For
        Next
        Return SearchText
    End Function

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 19 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 19 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros