'StringFunctions_Module ' ' Some basic functions ' ExtractData_lib(s1, StartPattern, EndPattern) As String ' Returns the string between 2 patterns ' ' TrimAll_lib(s1) As String ' Removes leading tabs and spaces ' ' ReplaceAll_lib (InThisString, ReplaceThis, WithThis) As Long ' Replaces all occurances of one string with another ' ' ReplaceFirst_lib (InThisString, ReplaceThis, WithThis) As Long ' Replaces the first occurance of one string with another ' ' 8-20-2002 R Clemenzi Option Compare Database Option Explicit ' Accepts a string and returns the characters between ' the start and end patterns Public Function ExtractData_lib(s1 As String, _ StartPattern As String, EndPattern As String) Dim i As Integer, j As Integer Dim str As String On Error GoTo Error_ExtractData_lib i = InStr(1, s1, StartPattern) If i > 0 Then str = Mid(s1, i + Len(StartPattern)) Else str = s1 End If i = InStr(1, str, EndPattern) If i > 0 Then str = Mid(str, 1, i - 1) End If Exit_ExtractData_lib: str = TrimAll_lib(str) ExtractData_lib = str Exit Function Error_ExtractData_lib: ' Error 5 - s1 & StartPattern are both null If Err.Number <> 5 Then MsgBox Err.Description & vbCrLf & "number " & Err.Number End If Resume Exit_ExtractData_lib End Function ' This is simiar to the Trim function ... ' except that it also removes tabs Public Function TrimAll_lib(s1 As String) Dim i As Integer, j As Integer Dim str As String On Error GoTo Exit_TrimAll_lib str = Trim(s1) While Mid(str, 1, 1) = vbTab str = Trim(Mid(str, 2)) Wend While Mid(str, Len(str), 1) = vbTab str = Trim(Mid(str, 1, Len(str) - 1)) Wend Exit_TrimAll_lib: TrimAll_lib = str Exit Function Error_TrimAll_lib: MsgBox Err.Description Resume Exit_TrimAll_lib End Function ' Replace all occurances of one string with another ' ' VisualBasic provides a Replace command which provides ' a similar capability ' ' This function returns the number of replacements made Public Function ReplaceAll_lib _ (InThisString As String, _ ReplaceThis As String, _ WithThis As String) As Long Dim LocationOfMatch As Long Dim StartOfSearch As Long ' This keeps from matching ' part of the new string Dim temp As String Dim temp2 As String ' Has the last part of the original Dim MatchCount As Long StartOfSearch = 1 temp = "" temp2 = InThisString MatchCount = 0 LocationOfMatch = InStr(InThisString, ReplaceThis) While LocationOfMatch > 0 MatchCount = MatchCount + 1 temp = temp & Mid(InThisString, StartOfSearch, _ LocationOfMatch - StartOfSearch) temp = temp & WithThis temp2 = Mid(InThisString, LocationOfMatch + Len(ReplaceThis)) StartOfSearch = LocationOfMatch + Len(ReplaceThis) LocationOfMatch = InStr(StartOfSearch, InThisString, ReplaceThis) Wend temp = temp & temp2 InThisString = temp ReplaceAll_lib = MatchCount End Function ' Replace the first occurance of one string with another ' ' This function returns the number of replacements made Public Function ReplaceFirst_lib _ (InThisString As String, _ ReplaceThis As String, _ WithThis As String) As Long Dim LocationOfMatch As Long Dim StartOfSearch As Long ' This keeps from matching ' part of the new string Dim temp As String Dim temp2 As String ' Has the last part of the original Dim MatchCount As Long StartOfSearch = 1 temp = "" temp2 = InThisString MatchCount = 0 LocationOfMatch = InStr(InThisString, ReplaceThis) If LocationOfMatch > 0 Then MatchCount = MatchCount + 1 temp = temp & Mid(InThisString, StartOfSearch, _ LocationOfMatch - StartOfSearch) temp = temp & WithThis temp2 = Mid(InThisString, LocationOfMatch + Len(ReplaceThis)) End If temp = temp & temp2 InThisString = temp ReplaceFirst_lib = MatchCount End Function