G
Guest
Hi - and thanks for reading this.
I have 'purloined' the following macro, posted years ago by Evelyn (thanks
to her). It is to replicate the Excel Substitute function in Access. The
problem I have is that if the strInPhrase in blank, Access gives me #Error.
If anyone could tell me how to overcome this error, I'd really appreciate
it.
Thanks.
Public Function Subst(strInPhrase As String, strSubstThis As String,
strWithThis As String)
'given a phrase (strInPhrase), this function will search for
'strSubstThis in the phrase and replace it with strWithThis
'it returns the change phrase
'Ev woolston
Dim FindInStr As Integer
Dim PartBefore As String
Dim PartAfter As String
Dim NewString As String
Dim LenSubstThis As Integer
Dim LenParts As Integer
LenSubstThis = Len(strSubstThis)
CheckIfThere:
FindInStr = InStr(1, strInPhrase, strSubstThis)
If FindInStr = 0 Then
'find strSubstThis in strInPhrase
'if it doesn't exist there then
'return the entire phrase unchanged
GoTo Unchanged
End If
'get part of message before strSubstThis
PartBefore = Left$(strInPhrase, FindInStr - 1)
LenParts = Len(PartBefore) + LenSubstThis + 1
PartAfter = Mid(strInPhrase, LenParts)
NewString = PartBefore & strWithThis & PartAfter
'check new string for a further
'occurance of strSubstThis
strInPhrase = NewString
GoTo CheckIfThere
Unchanged:
Subst = strInPhrase
End Function
I have 'purloined' the following macro, posted years ago by Evelyn (thanks
to her). It is to replicate the Excel Substitute function in Access. The
problem I have is that if the strInPhrase in blank, Access gives me #Error.
If anyone could tell me how to overcome this error, I'd really appreciate
it.
Thanks.
Public Function Subst(strInPhrase As String, strSubstThis As String,
strWithThis As String)
'given a phrase (strInPhrase), this function will search for
'strSubstThis in the phrase and replace it with strWithThis
'it returns the change phrase
'Ev woolston
Dim FindInStr As Integer
Dim PartBefore As String
Dim PartAfter As String
Dim NewString As String
Dim LenSubstThis As Integer
Dim LenParts As Integer
LenSubstThis = Len(strSubstThis)
CheckIfThere:
FindInStr = InStr(1, strInPhrase, strSubstThis)
If FindInStr = 0 Then
'find strSubstThis in strInPhrase
'if it doesn't exist there then
'return the entire phrase unchanged
GoTo Unchanged
End If
'get part of message before strSubstThis
PartBefore = Left$(strInPhrase, FindInStr - 1)
LenParts = Len(PartBefore) + LenSubstThis + 1
PartAfter = Mid(strInPhrase, LenParts)
NewString = PartBefore & strWithThis & PartAfter
'check new string for a further
'occurance of strSubstThis
strInPhrase = NewString
GoTo CheckIfThere
Unchanged:
Subst = strInPhrase
End Function