MS ACCESS VBA Module remove portions of a string based on phrases in another table (1 Viewer)

hyperdreamz

New member
Local time
Today, 17:48
Joined
Sep 29, 2004
Messages
1
Request help in creating a MS ACCESS VBA Module to remove portions of a string based on phrases in another table.

I am not a programmer but has a basic understanding of MS ACCESS & VBA.

Any help would be appreciated.

Table 1 with over a million records contains FIELD1 with large amounts running text(chats transcripts)


Table 2 contains a large number of restricted phrases in FIELD2 I wish to remove from the running text without leaving extra spaces


In order to generate OUTPUT table below


Code:
TABLE1
---------
FIELD1
This is normal text.
This is not normal text.
This maybe normal text. This normal is text.
This can be normal text.
This normal is text. This normal is text.
.

Code:
TABLE2
---------
FIELD2
is normal
is text
.
Code:
OUTPUT
---------
FIELD1
This text.
This is not normal text.
This maybe normal text. This normal.
This can be normal text.
This normal. This normal.
.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:18
Joined
Oct 29, 2018
Messages
21,447
Hi. Have you tried using the Replace() function?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:18
Joined
May 7, 2009
Messages
19,227
supposing the Output field is also in Table1.
create a Query that will update Output field:
Code:
UPDATE TABLE1 SET TABLE1.[OUTPUT] = apReplace([FIELD1],"TABLE2","FIELD2");
copy and paste the function in a Module.
Code:
Public Function apReplace(ByVal varTarget As Variant, strSourceTable As String, strSourceField As String) As Variant
    Dim strSought As String
    If IsNull(varTarget) Then
        Exit Function
    End If
    varTarget = varTarget & vbNullString
    With CurrentDb.OpenRecordset( _
            "select [" & strSourceField & "] from [" & strSourceTable & "];", _
            dbOpenDynaset)
            
        If Not (.BOF And .EOF) Then
            .MoveFirst
        End If
        While Not .EOF
            strSought = .Fields(0)
            With CreateObject("VBScript.RegExp")
                .Global = True
                .IgnoreCase = True
                .pattern = "\b" & strSought & "\b"
                varTarget = .Replace(varTarget, "")
            End With
            .MoveNext
        Wend
    End With
    apReplace = varTarget
End Function
 

Users who are viewing this thread

Top Bottom