Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

replace a whole word in vba

I have a table that has a column called lookfor it has string type.

I have another table with a string field made of words.

If i find tableA.lookfor in the table2 field I want to remove the whole word in table 2. for example

look for value "323"
table 2 string =3 SERIES SALOON 323i 4dr

323i would be replaced by nothing ""

not sure how to achieve that.

I have two recordsets so can get to the data:

[code]

Private Sub BtnRemoveModelData_Click()
Dim Db As DAO.Database
Set Db = CurrentDb()
Dim RstAutoRange As DAO.Recordset
Set RstAutoRange = Db.OpenRecordset("TblProcess")

Dim RstWordGroups As DAO.Recordset
Set RstWordGroups = Db.OpenRecordset("ClientWordGroups")
Dim ClientWords As Variant
 Dim x As Long
Dim WordToRemove As String



DoCmd.SetWarnings False

With RstAutoRange

    .MoveFirst
    countrecords = .RecordCount

 'cycle through the recordset
    Do Until RstAutoRange.EOF

    
    
        With RstWordGroups
            RstWordGroups.MoveFirst
            Do Until RstWordGroups.EOF
                If (InStr(RstWordGroups.Fields("ConcatenatedUnwantedRemoval").Value, RstAutoRange.Fields("LookFor").Value) <> 0) And (RstWordGroups.Fields("MarqueAlias").Value = RstAutoRange.Fields("Marque").Value) Then
                
                'found
                'getlength
             
                
                
                
                
                
                
                Else
                'not found
                
                
                End If
    
    
    
            RstWordGroups.MoveNext
            Loop
        End With
            




    RstAutoRange.MoveNext
    Loop
    
End With



End Sub

[/code]

Open in new window

Open in new window

Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Maybe I'm missing something - have you thought about using the vba Replace function?

replace(variable,"lookForThis","ReplaceWithThis")
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

yes I did that first, but i want to eliminate the whole word so if i say find 325 but the word in the string to search has 325st how do i replace the 325st as a whole and not just the substring.
If it's a "word" then it's surrounded by spaces.

replace " 325 " with " something "

MAYBE you next a couple replaces to cover " 325, " and " 325. "
if i did replace("my string 325st","325","") i would get

"my string st"

what I would like is:
"my string"
I see your issue now.

I would suggest looking over the solution here:
https://www.experts-exchange.com/questions/28173264/using-wildcard-in-vba-access-replace-function.html

It appears you will have to use a RegEx expression in your replacing.
ASKER CERTIFIED SOLUTION
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
went with this function as it was small and efficient and I can call it when needed.