VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Populates a list control (listbox, combobox, etc) from the contents of a text file.

by Russ Green (2 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 17th November 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Populates a list control (listbox, combobox, etc) from the contents of a text file.

Rate Populates a list control (listbox, combobox, etc) from the contents of a text file.



    ByVal FullPath As String, Optional ByVal Delimiter As String = vbCrLf) As Boolean

        Dim objFso As New Scripting.FileSystemObject()

        Dim objTextStream As Scripting.TextStream
        Dim strContents As String
        Dim arrContents() As String
        Dim lCtr As Long, lCount As Long

        Try
            'Ensure file exists
            If Dir(FullPath) = "" Then
                MsgBox("The file " & FullPath & " does not exist", MsgBoxStyle.OKOnly, "Load contract lists")
                Exit Function
            End If

            'Get file contents
            objTextStream = objFso.OpenTextFile(FullPath, Scripting.IOMode.ForReading)
            strContents = objTextStream.ReadAll
            objTextStream.Close()

            'Split file contents based on delimiter
            arrContents = Split(strContents, Delimiter)
            lCount = UBound(arrContents)

            'Clear List Control.
            ListControl.Items.Clear()

            'Populate list control
            ListControl.Items.AddRange(arrContents)

            PopulateListControlFromFile = True

        Catch ex As Exception
            objTextStream = Nothing
            objFso = Nothing
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Load contract lists")
        End Try

    End Function

Download this snippet    Add to My Saved Code

Populates a list control (listbox, combobox, etc) from the contents of a text file. Comments

No comments have been posted about Populates a list control (listbox, combobox, etc) from the contents of a text file.. Why not be the first to post a comment about Populates a list control (listbox, combobox, etc) from the contents of a text file..

Post your comment

Subject:
Message:
0/1000 characters