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

ASKER CERTIFIED SOLUTION
Sancler

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 19 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 19 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004