home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Special / chip-cd_2001_spec_05.zip / spec_05 / apps / crystal / disk18 / Xvb237._ / Xvb237.
Text File  |  1999-08-23  |  14KB  |  479 lines

  1. Attribute VB_Name = "modStringFunctions"
  2. Option Explicit
  3.  
  4.  
  5. Public Sub Replace(strTarget As String, ByVal strReplace As String, ByVal strWith As String, Optional ByVal vCaseSensitive As Variant)
  6. Attribute Replace.VB_Description = "Converts strReplace to strWith in strTarget. Not case sensitive by default. Pass vCaseSensitive:=""True"" if desired.\r\n"
  7. '
  8. ' converts strReplace to strWith in strTarget
  9. ' Not case sensitive by default. Pass vCaseSensitive:="True" if desired.
  10. ' Example: to change " OR " to ","
  11. '             strTarget = "This or That or TheOther"
  12. '             Replace strTarget, " OR ", ", "
  13. '---------------------------
  14. ' Created: RickM, 1996
  15. ' Last Modified: RickM, 7:32 PM 25/11/96
  16. '                   added Case Sensitive flag
  17. '---------------------------
  18.  
  19.   On Error GoTo Replace_Err:
  20.   Screen.MousePointer = vbHourglass
  21.  
  22.   Dim intStart As String
  23.   Dim strBuffer As String
  24.   Dim strTemp As String
  25.   Dim strOld As String
  26.   
  27.   ' -------- provide defaults for optional parameters -----------
  28.   If IsMissing(vCaseSensitive) Then
  29.     vCaseSensitive = False
  30.   End If
  31.   
  32.   '-- work with temp strings, so we don't mess up the passed string's Case --
  33.   If vCaseSensitive Then
  34.     strTemp = strTarget
  35.     strOld = strReplace
  36.   Else
  37.     strTemp = UCase$(strTarget)
  38.     strOld = UCase$(strReplace)
  39.   End If
  40.   
  41.   '--- find first position of string to be replaced --
  42.   intStart = InStr(strTemp, strOld)
  43.   
  44.   While intStart > 0
  45.     'extract 'before' part of string
  46.     strBuffer = Left$(strTarget, intStart - 1)
  47.     'find beginning of the end...
  48.     intStart = Len(strBuffer) + Len(strReplace) + 1
  49.     'rebuild original string
  50.     strTarget = strBuffer & strWith & Mid$(strTarget, intStart)
  51.     
  52.     're-set the Temp string, accounting for "Case Sensitive" flag
  53.     If Not vCaseSensitive Then
  54.       strTemp = UCase$(strTarget)
  55.     Else
  56.       strTemp = strTarget
  57.     End If
  58.     
  59.     'find next position of string to be replaced
  60.     intStart = InStr(strTemp, strOld)
  61.   Wend
  62.  
  63. Replace_Exit:
  64.   Screen.MousePointer = vbDefault
  65.   Exit Sub
  66.     
  67. Replace_Err:
  68.     Select Case Err
  69.         Case Else
  70.           Select Case MsgBox(Error$, vbInformation + vbAbortRetryIgnore, "Replace() error: " & Err)
  71.             Case vbRetry
  72.               Resume 0 'return to offending line
  73.             Case vbIgnore
  74.               Resume Next 'ignore offending line, continue from next line
  75.             Case vbAbort
  76.               'exit
  77.           End Select
  78.     End Select
  79.     
  80.     GoTo Replace_Exit:
  81.  
  82. End Sub
  83.  
  84. Public Function RemoveFileName(ByVal strPath As String) As String
  85. Attribute RemoveFileName.VB_Description = "Returns the path from a passed string (everything BEFORE the last backslash). Strips the filename. RELATED FUNCTIONS: GetFilename - returns the filename."
  86. '
  87. ' strip filename from passed path
  88. '
  89. '---------------------------
  90. ' Created: RickM, 1996
  91. ' Last Modified:
  92. '---------------------------
  93.  
  94.   On Error GoTo RemoveFileName_Err:
  95.   Screen.MousePointer = vbHourglass
  96.  
  97.   Dim strBuffer As String
  98.   Dim x As Integer
  99.   Dim intLen As Integer
  100.   
  101.   If strPath <> "" Then
  102.     x = InStr(strPath, "\")
  103.     While x > 0
  104.       x = InStr(x + 1, strPath, "\")
  105.       If x > 0 Then
  106.         intLen = x
  107.       End If
  108.     Wend
  109.     strPath = Left$(strPath, intLen)
  110.   End If
  111.   
  112.   RemoveFileName = strPath
  113.   
  114. RemoveFileName_Exit:
  115.   Screen.MousePointer = vbDefault
  116.   Exit Function
  117.     
  118. RemoveFileName_Err:
  119.     Select Case Err
  120.         Case Else
  121.           Select Case MsgBox(Error$, vbInformation + vbAbortRetryIgnore, "RemoveFileName() error: " & Err)
  122.             Case vbRetry
  123.               Resume 0 'return to offending line
  124.             Case vbIgnore
  125.               Resume Next 'ignore offending line, continue from next line
  126.             Case vbAbort
  127.               'exit
  128.           End Select
  129.     End Select
  130.     
  131.     GoTo RemoveFileName_Exit:
  132.   
  133. End Function
  134.  
  135. Public Function ListAddUnique(ctrlList As Control, strItem As String) As Boolean
  136. Attribute ListAddUnique.VB_Description = "Pass a Listbox or ComboBox, and a string. Searches for string in list before adding. Insures only unique items in list."
  137. '
  138. ' add string to control's list, only if not pre-existing
  139. ' handles Listbox or Combobox
  140. '
  141. ' WANTED:
  142. '   should handle ItemData as well...
  143. '   should return NewIndex property
  144. '---------------------------
  145. ' Created: RickM, 1996
  146. ' Last Modified: RickM, 7:32 PM 25/11/96
  147. '                   added Case Sensitive flag
  148. '---------------------------
  149.  
  150.   On Error Resume Next
  151.   
  152.   ' see if item is unique before calling AddItem on control
  153.   Dim x As Integer
  154.   Dim strBuffer As String
  155.   Dim bFound As Boolean
  156.   
  157.   strItem = Trim$(strItem)
  158.   If strItem <> "" Then
  159.     For x = 0 To ctrlList.ListCount
  160.       strBuffer = Trim$(ctrlList.List(x))
  161.       If LCase$(strBuffer) = LCase$(strItem) Then
  162.         bFound = True
  163.       End If
  164.     Next x
  165.     
  166.     If Not bFound Then
  167.       ctrlList.AddItem strItem
  168.       ctrlList.ItemData = ctrlList.NewIndex
  169.     End If
  170.   End If
  171.   ListAddUnique = Not bFound
  172.   
  173. End Function
  174.  
  175. Public Function ExtractFromList(ByVal strList As String, ByVal intIndex As Integer, ByVal strDelimiter As String) As String
  176. Attribute ExtractFromList.VB_Description = "Extracts item from delimited string. Pass a delimited string, the index number of the item to extract, and the delimiter used."
  177. '
  178. ' extracts a word from a delimited list
  179. ' pass a delimited string, the index of the item to extract, and the delimiter
  180. '
  181. '---------------------------
  182. ' Created: RickM, 1996
  183. ' Last Modified:
  184. '---------------------------
  185.  
  186.     On Error GoTo ExtractFromList_Err:
  187.     Screen.MousePointer = vbHourglass
  188.  
  189.     Dim intPos As Integer
  190.     Dim intStartOfPhrase As Integer
  191.     Dim strReturn As String
  192.     Dim x As Integer
  193.  
  194.     intPos = InStr(strList, strDelimiter)
  195.  
  196.     If Trim$(strList) <> "" And intIndex > 0 Then
  197.         If intIndex > 1 Then 'find the delimiter for intIndex
  198.                                 ' extract the phrase between that delimiter and the next
  199.           If intPos > 0 Then
  200.             For x = 2 To intIndex
  201.                 intStartOfPhrase = intPos
  202.                 If x = intIndex Then 'found beginning of correct phrase,
  203.                                          '   now find it's length
  204.                     intPos = InStr(intPos + 1, strList, strDelimiter)
  205.                     'extract the phrase to be returned
  206.                     If intPos = 0 Then
  207.                         strReturn = Trim$(Mid$(strList, intStartOfPhrase + 1))
  208.                     Else
  209.                         strReturn = Trim$(Mid$(strList, intStartOfPhrase + 1, intPos - intStartOfPhrase - 1))
  210.                     End If
  211.                 Else
  212.                     'haven't found beginning position yet,
  213.                     intPos = InStr(intPos + 1, strList, strDelimiter)
  214.                     If intPos = 0 Then
  215.                         Exit For  ' you've got to know when to give up!
  216.                     End If
  217.                 End If
  218.             Next x
  219.           Else
  220.             'requested item index is greater than number of items... return nothing
  221.           End If
  222.         Else
  223.             'caller asked for first phrase
  224.             'If intPos > 0 And intIndex = 0 Then 'don't care what intPos is, if caller passed 0 give them nothing! Moved this to top.
  225.  
  226.          If intPos > 0 Then
  227.             strReturn = Trim$(Left$(strList, intPos - 1))
  228.          Else
  229.           ' if intIndex is 1, return whole string
  230.             If intIndex = 1 Then
  231.               strReturn = strList
  232.             Else
  233.               'bad list or index... return nothing
  234.             End If
  235.          End If
  236.         End If
  237.     Else
  238.         'caller passed an empty string, return the favour!
  239.     End If
  240.     
  241.     ExtractFromList = strReturn
  242.     
  243. ExtractFromList_Exit:
  244.   Screen.MousePointer = vbDefault
  245.   Exit Function
  246.     
  247. ExtractFromList_Err:
  248.     Select Case Err
  249.         Case Else
  250.           Select Case MsgBox(Error$, vbInformation + vbAbortRetryIgnore, "ExtractFromList() error: " & Err)
  251.             Case vbRetry
  252.               Resume 0 'return to offending line
  253.             Case vbIgnore
  254.               Resume Next 'ignore offending line, continue from next line
  255.             Case vbAbort
  256.               'exit
  257.           End Select
  258.     End Select
  259.     
  260.     GoTo ExtractFromList_Exit:
  261.     
  262. End Function
  263.  
  264. Public Function GetFileName(ByVal strPath As String) As String
  265. Attribute GetFileName.VB_Description = "Returns the filename (everything AFTER the last backslash) from a passed string. RELATED FUNCTIONS: RemoveFileName (returns the path)."
  266. '
  267. ' strip pathname from passed path, return the filename
  268. '
  269. '---------------------------
  270. ' Created: RickM, 1997
  271. ' Last Modified:
  272. '---------------------------
  273.  
  274.   Dim x As Integer
  275.   
  276.   x = Len(strPath) - Len(RemoveFileName(strPath))
  277.   strPath = Right$(strPath, x)
  278.  
  279.   GetFileName = strPath
  280.  
  281. GetFileName_Exit:
  282.   Screen.MousePointer = vbDefault
  283.   Exit Function
  284.     
  285. GetFileName_Err:
  286.     Select Case Err
  287.         Case Else
  288.           Select Case MsgBox(Error$, vbInformation + vbAbortRetryIgnore, "GetFileName() error: " & Err)
  289.             Case vbRetry
  290.               Resume 0 'return to offending line
  291.             Case vbIgnore
  292.               Resume Next 'ignore offending line, continue from next line
  293.             Case vbAbort
  294.               'exit
  295.           End Select
  296.     End Select
  297.     
  298.     GoTo GetFileName_Exit:
  299.  
  300. End Function
  301.  
  302. Public Function GetParentFolder(ByVal strPath As String) As String
  303. '
  304. ' return the string with everything after the last backslash removed
  305. ' if the last char is a backslash, ignore it
  306. '
  307. '---------------------------
  308. ' Created: RickM, 1997
  309. ' Last Modified:
  310. '---------------------------
  311.  
  312.   On Error GoTo GetParentFolder_Err:
  313.   Dim strParent As String
  314.   
  315.   strPath = StripSlash(strPath)
  316.   strParent = StripSlash(RemoveFileName(strPath))
  317.   
  318. GetParentFolder_Exit:
  319.   GetParentFolder = strParent
  320.   Exit Function
  321. GetParentFolder_Err:
  322.   MsgBox Error$
  323.   GoTo GetParentFolder_Exit:
  324. End Function
  325. Public Function StripSlash(ByVal strTarget As String) As String
  326. '
  327. ' remove trailing backslash
  328. ' trims the string
  329. '
  330. '---------------------------
  331. ' Created: RickM, 1997
  332. ' Last Modified:
  333. '---------------------------
  334.  
  335.   On Error Resume Next
  336.   
  337.   strTarget = Trim$(strTarget)
  338.   If Right$(strTarget, 1) = "\" Then
  339.     If Err = 0 Then
  340.       strTarget = Left$(strTarget, Len(strTarget) - 1)
  341.     Else
  342.       'string was empty, that's ok, just return it
  343.     End If
  344.   End If
  345.   
  346.   StripSlash = strTarget
  347.   
  348. End Function
  349.  
  350. Public Function AddSlash(ByVal strTarget As String) As String
  351. '
  352. ' add trailing backslash only if there isn't one
  353. ' trims the string first
  354. '
  355. '---------------------------
  356. ' Created: RickM, 1997
  357. ' Last Modified:
  358. '---------------------------
  359.  
  360.   On Error Resume Next
  361.   
  362.   strTarget = Trim$(strTarget)
  363.   If Right$(strTarget, 1) <> "\" Then
  364.     If Err = 0 Then
  365.       strTarget = strTarget & "\"
  366.     Else
  367.       'string was empty, that's ok, just return it
  368.     End If
  369.   End If
  370.   
  371.   AddSlash = strTarget
  372.   
  373. End Function
  374.  
  375. Public Function SingleQ(strTarget As String) As String
  376. '
  377. ' wrap passed string in single quotes and return it
  378. '
  379.  
  380.   On Error Resume Next
  381.   
  382.   SingleQ = "'" & strTarget & "'"
  383.   
  384. End Function
  385.  
  386. Public Function DoubleQ(strTarget As String) As String
  387. '
  388. ' wrap passed string in Chr$(34) (double quotes)
  389. '   and return it
  390. '
  391.  
  392.   On Error Resume Next
  393.   
  394.   DoubleQ = Chr$(34) & strTarget & Chr$(34)
  395.   
  396. End Function
  397. Public Function ExtractAdNumber(strTarget As String) As String
  398.   Dim intShowIndex, intSlashIndex As Integer
  399.   Dim intStart As Integer
  400.   
  401.   intStart = 1
  402.   intShowIndex = 0
  403.   intSlashIndex = 0
  404.   
  405.   Do
  406.     intStart = InStr(intStart, strTarget, "ad", vbTextCompare)
  407.     intStart = intStart + 2
  408.     If intStart > intShowIndex + 2 Then
  409.       intShowIndex = intStart
  410.     End If
  411.   Loop While intStart > 2
  412.   
  413.   If intShowIndex > 0 Then
  414.     intSlashIndex = InStr(intShowIndex, strTarget, "a", vbTextCompare)
  415.     If intSlashIndex > 0 Then
  416.       ExtractAdNumber = Mid(strTarget, intShowIndex, intSlashIndex - intShowIndex)
  417.     Else
  418.       ExtractAdNumber = ""
  419.     End If
  420.   Else
  421.     ExtractAdNumber = ""
  422.   End If
  423. End Function
  424.  
  425. Public Function ExtractADDirectory(strTarget As String) As String
  426.   Dim intShowIndex, intSlashIndex As Integer
  427.   Dim intStart As Integer
  428.   
  429.   intStart = 1
  430.   intShowIndex = 0
  431.   intSlashIndex = 0
  432.   
  433.   Do
  434.     intStart = InStr(intStart, strTarget, "admaster", vbTextCompare)
  435.     intStart = intStart + 8
  436.     If intStart > intShowIndex + 8 Then
  437.       intShowIndex = intStart
  438.     End If
  439.   Loop While intStart > 8
  440.   
  441.   If intShowIndex > 0 Then
  442.     intSlashIndex = InStr(intShowIndex, strTarget, "\", vbTextCompare)
  443.     If intSlashIndex > 0 Then
  444.       ExtractADDirectory = Mid(strTarget, 1, intSlashIndex)
  445.     Else
  446.       ExtractADDirectory = ""
  447.     End If
  448.   Else
  449.     ExtractADDirectory = ""
  450.   End If
  451. End Function
  452. Public Function ExtractShowNumber(strTarget As String) As String
  453.   Dim intShowIndex, intSlashIndex As Integer
  454.   Dim intStart As Integer
  455.   
  456.   intStart = 1
  457.   intShowIndex = 0
  458.   intSlashIndex = 0
  459.   
  460.   Do
  461.     intStart = InStr(intStart, strTarget, "show", vbTextCompare)
  462.     intStart = intStart + 4
  463.     If intStart > intShowIndex + 4 Then
  464.       intShowIndex = intStart
  465.     End If
  466.   Loop While intStart > 4
  467.   
  468.   If intShowIndex > 0 Then
  469.     intSlashIndex = InStr(intShowIndex, strTarget, "\", vbTextCompare)
  470.     If intSlashIndex > 0 Then
  471.       ExtractShowNumber = Mid(strTarget, intShowIndex, intSlashIndex - intShowIndex)
  472.     Else
  473.       ExtractShowNumber = ""
  474.     End If
  475.   Else
  476.     ExtractShowNumber = ""
  477.   End If
  478. End Function
  479.