Attribute VB_Name = "mod_Crystal_GetFilename_from_path"
Option Compare Database
Option Explicit
'
' 8-23-07
'
Sub RunGetfilename()
   ' to test the GetFilename function,,
   ' click your mouse in this procedure
   ' To Run --> press F5
   ' (or choose Run, Run Sub/UserFrom from the menu)
   
   'dimension a variable to hold the filename returned
   Dim mFilename As String
   
   'run the Getfilename code for
   'a test path and filename
   mFilename = GetFilename("c:\path\filename. ext")
   
   'output the results to a message box
   MsgBox mFilename, , "GetFilename Results"
   
End Sub

Function GetFilename(pPathFilename As String) As String

   'written by Crystal
   'strive4peace
   
   'PARAMETERS
   ' pPathFilename is drive, path, and filename
   '    examples:
   '    -- "c:\path\filename.ext"
   '    -- "filename.ext"
   '    -- MyVariable
   '    -- "c:\My Documents\Excelfile.xls"
   
   'DEFAULT VALUE
   '  if nothing is assigned,
   '  return an empty string
   GetFilename = ""
   
   ' dimension a variable to hold the
   ' position of a character in a string
   Dim mCharPos As Integer
   
   '-----------------------------------------
   ' Len("string")
   '    --> count number of characters in string
   ' Trim("string")
   '    --> chop off leading and trailing
   '        spaces from string
   '  NZ(expression, default)
   '    --> convert Null expression values
   '        to whatever is specified in
   '        optional second argument (default)
   '    if 2nd argument not specified,
   '      Access uses zero (0)
   '      or empty string (zls)
   '      whichever it decides is applicable
   '-----------------------------------------
   
   ' if passed parameter has nothing in it, exit
   If Len(Trim(Nz(pPathFilename, ""))) = 0 Then
      Exit Function
   End If
    
   '-----------------------------------------
   ' NOTE:
   '    SPACE UNDERSCORE
   '    at the end of a line
   '    means a statement is continued on
   '    another line
   '-----------------------------------------
   ' InStr("string", "look")
   '    --> return character position
   '        of "look" in "string"
   '        if not found, zero (0) is returned
   '-----------------------------------------
   
   ' if passed parameter does not contain
   ' a back or forward slash then
   '    assign the filename to what was passed
   '    and exit
   
   If InStr(pPathFilename, "\") = 0 _
      And InStr(pPathFilename, "/") = 0 Then
      
      GetFilename = pPathFilename
       
      Exit Function
   End If
   
   '-----------------------------------------
   ' Mid(string, mCharPos, #)
   '    --> get substring from MIDdle of string
   '        start at mCharPos
   '        get # number of characters
   '-----------------------------------------
   
   ' loop from the last to the first character
   ' of the passed parameter
   ' look for a file/directory separator character
   
   For mCharPos = Len(pPathFilename) To 1 Step -1
   
      'if the next character is a slash or back slash
      ' then we found what we were looking for ...
      
      If Mid(pPathFilename, mCharPos, 1) = "\" _
         Or Mid(pPathFilename, mCharPos, 1) = "/" Then
      
        GoTo GotSlash
        
      End If
      
   Next mCharPos
   
'this is a line label
GotSlash:
   
   'path -- not used here
   '--> = Left(pPathFilename, mCharPos)
   
   
   '-----------------------------------------
   ' Right(string, #)
   '    --> get substring from Right (end) of string
   '        get # number of characters
   '-----------------------------------------
   
   ' here is the filename
   ' assign it to the function name
   ' to "return" the value
   
   GetFilename = Right( _
      pPathFilename _
      , Len(pPathFilename) - mCharPos)
   
End Function
