kfschaefer
Registered User.
- Local time
- Today, 02:43
- Joined
- Oct 10, 2008
- Messages
- 58
I am looking to modify the following code to replace a portion of a memo field w/o affecting the data within the memo field. I have tried numerous methods and this is the closes I got, unfortunately it only works partly.
Say My comment field contains the following:
'******************************************
John Smith is going to the country
Work Code: Work In Lab
This is a test of the replace code.
'******************************************
So when the user wishes to change the Work Code from "Work Code: Work in Lab" to "Work Code: Work in Field"
I want to only change that portion of the comments.
My current code changes that portion, but unfortunately, removes the remaining comments "This is a Test of the replace Code".
What am I missing?
Thanks,
Karen
'************************************************************************************
Public Sub Work_Code_AfterUpdate()
strSearch = Me.RequestorComments.Value
gJG = Nz(Me.Job_Group)
Select Case Work_Code
Case 1
gWC = Me.Work_Code.Column(1)
Case 3
gWC = Me.Work_Code.Column(1)
End Select
sFieldName = "Work Code: " & gWC
strReplaceWith = sFieldName
If strSearchFor = vbNullString Then
If Len(Nz(Me.RequestorComments)) > 0 Then
Me.RequestorComments = Me.RequestorComments & vbCrLf & sFieldName
Else
Me.RequestorComments = sFieldName
End If
ElseIf strSearchFor = sFieldName Then
GoTo ResumeNext:
ElseIf strSearchFor <> sFieldName Then
recValue = Substitute(strSearchFor, strReplaceWith, strSearch)
Me.RequestorComments = recValue
End If
ResumeNext:
Me.RequestorComments.Requery
End Sub
'************************************************************************************
Private Sub Work_Code_Dirty(Cancel As Integer)
strSearch = "Work Code: "
gWC = Me.Work_Code.Column(1)
strSearchFor = strSearch & gWC
End Sub
'************************************************************************************
Function Substitute(strSearchFor As String, strReplaceWith As String, strSearch As String) As String
On Error GoTo Oops
Dim I As Integer, LastTest As Integer, Temp As String
If Len(strSearchFor) > Len(strSearch) Then
Substitute = strSearch
GoTo Out
End If
LastTest = Len(strSearch) - Len(strSearchFor) + 1
For I = 1 To LastTest
If Mid(strSearch, I, Len(strSearchFor)) = strSearchFor Then
Temp = Temp & strReplaceWith
I = I + Len(strSearchFor) - 1
Else
If I < LastTest Then
Temp = Temp & Mid(strSearch, I, 1)
Else
Temp = Temp & Right(strSearch, Len(strSearchFor))
End If
End If
Next I
Substitute = Temp
Out:
Exit Function
Oops:
Substitute = strSearch
Resume Out
End Function
'************************************************************************************
--------------------
Thanks,
K
Say My comment field contains the following:
'******************************************
John Smith is going to the country
Work Code: Work In Lab
This is a test of the replace code.
'******************************************
So when the user wishes to change the Work Code from "Work Code: Work in Lab" to "Work Code: Work in Field"
I want to only change that portion of the comments.
My current code changes that portion, but unfortunately, removes the remaining comments "This is a Test of the replace Code".
What am I missing?
Thanks,
Karen
'************************************************************************************
Public Sub Work_Code_AfterUpdate()
strSearch = Me.RequestorComments.Value
gJG = Nz(Me.Job_Group)
Select Case Work_Code
Case 1
gWC = Me.Work_Code.Column(1)
Case 3
gWC = Me.Work_Code.Column(1)
End Select
sFieldName = "Work Code: " & gWC
strReplaceWith = sFieldName
If strSearchFor = vbNullString Then
If Len(Nz(Me.RequestorComments)) > 0 Then
Me.RequestorComments = Me.RequestorComments & vbCrLf & sFieldName
Else
Me.RequestorComments = sFieldName
End If
ElseIf strSearchFor = sFieldName Then
GoTo ResumeNext:
ElseIf strSearchFor <> sFieldName Then
recValue = Substitute(strSearchFor, strReplaceWith, strSearch)
Me.RequestorComments = recValue
End If
ResumeNext:
Me.RequestorComments.Requery
End Sub
'************************************************************************************
Private Sub Work_Code_Dirty(Cancel As Integer)
strSearch = "Work Code: "
gWC = Me.Work_Code.Column(1)
strSearchFor = strSearch & gWC
End Sub
'************************************************************************************
Function Substitute(strSearchFor As String, strReplaceWith As String, strSearch As String) As String
On Error GoTo Oops
Dim I As Integer, LastTest As Integer, Temp As String
If Len(strSearchFor) > Len(strSearch) Then
Substitute = strSearch
GoTo Out
End If
LastTest = Len(strSearch) - Len(strSearchFor) + 1
For I = 1 To LastTest
If Mid(strSearch, I, Len(strSearchFor)) = strSearchFor Then
Temp = Temp & strReplaceWith
I = I + Len(strSearchFor) - 1
Else
If I < LastTest Then
Temp = Temp & Mid(strSearch, I, 1)
Else
Temp = Temp & Right(strSearch, Len(strSearchFor))
End If
End If
Next I
Substitute = Temp
Out:
Exit Function
Oops:
Substitute = strSearch
Resume Out
End Function
'************************************************************************************
--------------------
Thanks,
K