Results 1 to 6 of 6
  1. #1
    Salty Mariner is offline Intermediate
    Windows 11 Access 2021
    Join Date
    Dec 2023
    Location
    Corpus Christi, TX
    Posts
    69

    Unexpected result while populating a listbox

    I am using the code below to populate a listbox. the general idea is to populate the listbox with the path in column(0) and the file name in Column(1) Column (0) will remain hidden and I will use follow hyperlink to open the file.
    Code:
    '------------------------------------------------------------
    ' SUBROUTINE NAME: GetFilePathNames()
    ' PORPOSE: populate a llistbox withe the path and file names
    ' in a folder
    ' REQUIRES: A reference to the Microsoft Scripting RunTime
    ' CREATED BY: Andrew Dulavitz   (adulavitz@hotmail.com)
    ' DATE: January 2024
    '-------------------------------------------------------------
    Private Sub GetFilePathNames()
    On Error GoTo Err_Handler
    
    
    
    
        Dim strFolderPath As String
        Dim fso As Object ' FileSystemObject
        Dim fld As Object ' Folder
        Dim fil As Object ' File
        Dim i As Integer  ' Counter
        
        strFolderPath = Me.cboDocumentFolder.Column(2)
        Me.lstFolderDocs.RowSource = "" ' Clear any previous list items
        If strFolderPath = "" Then Exit Sub
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fld = fso.GetFolder(strFolderPath)
        
        i = 0 'Initialize the counter
        For Each fil In fld.Files 'Loop through the files in the folder
            Me.lstFolderDocs.AddItem fil.Path & ";" & fil.Name 'Add the file path and name to the list box
            i = i + 1 'Increment the counter
        Next fil
        
        'Me.lstFolderDocs.ListIndex = 0  ' Select the first item in the listbox
    
    
    
    
    Exit_Handler:
        Exit Sub
    
    
    
    
    Err_Handler:
        clsErrorHandler.HandleError "frmCrewInfo", "GetFilePathNames"
            Resume Exit_Handler
    End Sub

    Everything seems to work as it should until the end result is displayed.



    Click image for larger version. 

Name:	Screenshot 2024-01-05 123654.png 
Views:	13 
Size:	44.6 KB 
ID:	51306


    Note the text highlighted in Yellow. It has somehow bled over from the path. This is strange because if I pause he code and type "? fil.Name" in the immediate window I get only the file name. Likewise if I replace fil.Path with some random text string I get to bleed over from that text string. This seems like it should be pretty straightforward but I am quite baffled by this behavior.


    Any ideas on what could be going on here?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    The comma?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Salty Mariner is offline Intermediate
    Windows 11 Access 2021
    Join Date
    Dec 2023
    Location
    Corpus Christi, TX
    Posts
    69
    Quote Originally Posted by Welshgasman View Post
    The comma?
    There is a semicolon to separate the data for the columns but no comma.

    Code:
    Me.lstFolderDocs.AddItem fil.Path & ";" & fil.Name
    Strange because it's only carrying over a fragment of the path.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    There is a comma in the file path?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Note in your comments you state requires MS Scripting runtime however you are late binding the FSO so it isn't required.

    It appears the comma in the path may have something to do with it. Try delimiting the fil.path with quotes.

    in my test, without quotes, the path ends at the comma. Delimiting it corrects that.

    Code:
        For Each fil In fol.Files
            lbx.AddItem fil.Name & ";""" & fil.Path & """"
        Next fil

    note I have file name first then file path

    in your case you may need
    Code:
    lbx.AddItem Chr(34) & fil.Path & Chr(34) & ";" & fil.Name
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    Salty Mariner is offline Intermediate
    Windows 11 Access 2021
    Join Date
    Dec 2023
    Location
    Corpus Christi, TX
    Posts
    69
    Quote Originally Posted by Welshgasman View Post
    There is a comma in the file path?
    My apologies Welshgasman you were quite correct. I was naming the folders in the format of [LastName] & ", " & [FirstName] & " " & [MiddileInit] doing this in a query and was creating the folder in Code. It never occured to me to look at my naming convention for folders. Easy enough fix we can just use an underscore between LastName and FirstName.

    Tested and it works as intended. Now to go fix those queries.

    Thank you so much. Problem solved!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2022, 11:21 AM
  2. Unexpected result from a dropdown control
    By George in forum Modules
    Replies: 6
    Last Post: 07-11-2016, 01:09 PM
  3. Replies: 4
    Last Post: 06-24-2013, 07:34 AM
  4. Replies: 1
    Last Post: 01-09-2013, 02:42 PM
  5. Replies: 17
    Last Post: 08-19-2011, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums