Results 1 to 10 of 10

Thread: [RESOLVED] Fill combobox CSV

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    6

    Resolved [RESOLVED] Fill combobox CSV

    Hi,

    I want to fill a combox with data out of a colum of a CSV-file. Any suggestions?

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Fill combobox CSV

    What application are you using?
    Does the CSV file have a single column? If not, do you always want data from the same column number?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    6

    Re: Fill combobox CSV

    I'm using Word -VB6.3. The CSV has more than one column, but it's alway the same. Dependent of this column I would like to fill other textboxes.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Fill combobox CSV

    Sorry, I should have asked this question earlier.
    Does the column contain unique values? If not, do you need to create a unique set to poulate the combo?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    6

    Re: Fill combobox CSV

    Yes the column contains unique values.

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Fill combobox CSV

    OK
    Here's an eample how to do this using a textsteam and a FileSystemObject.
    You will need to add a reference to the Microsoft Scripting Runtime library in order for this to work.

    You will have to change some elements to get this working.
    1/ Change the value of sPath to the the correct path for your csv file.
    2/ Change combobox1 to the name of your combo.
    3/ Change "ThisDocument.ComboBox1.AddItem saCsvData(1)" such that the array element number matches the column you wish to use.
    4/ If your csv file has headers, you will not want to add the data from the the first line of the textstream.

    Option Explicit

    VB Code:
    1. Sub PopCombo()
    2. Dim fsDataFile As Scripting.FileSystemObject
    3. Dim sText As TextStream
    4. Dim sPath As String
    5. Dim sFullLine As String
    6. Dim saCsvData() As String
    7.    
    8.     ' Delete all value currently in the combo
    9.     ThisDocument.ComboBox1.Clear
    10.    
    11.     'Create a new FileSystemObject
    12.     Set fsDataFile = New Scripting.FileSystemObject
    13.    
    14.     'The fully qualified path to your lookup file
    15.     sPath = "C:\book2.csv"
    16.    
    17.     'Create a text stream from your FileSystemObject
    18.     Set sText = fsDataFile.OpenTextFile(sPath, ForReading, False, TristateUseDefault)
    19.    
    20.     'Loop through each line in the text stream
    21.     Do While Not sText.AtEndOfStream
    22.         'Get the complete line of text
    23.         sFullLine = sText.ReadLine
    24.        
    25.         'split the line of text into a 1 dimensional array
    26.         'using comma as the delimeter
    27.        
    28.         saCsvData = Split(sFullLine, ",", 2)
    29.        
    30.         ThisDocument.ComboBox1.AddItem saCsvData(1)
    31.        
    32.     Loop
    33.    
    34.     Set sText = Nothing
    35.     Set fsDataFile = Nothing
    36. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    6

    Re: Fill combobox CSV

    Thanks for your reply.

    Can I also apply this to a userform? The values are separated by ","
    Last edited by JameZz; Feb 15th, 2006 at 10:27 AM.

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Fill combobox CSV

    Yes, you can apply this to a form - if you mean "Can I use this to pouplate a combobox on a form?"

    The best way to do this is to convert this to a function that returns an array of values. When you initialize the form you can call this function and then use the resulting array to populate your combobox.
    I've change the function so that now you can pass the path to the file, the column number you want to return and the appropriate delimeter for the file. This means that you can use this to populate many different combos (or any other controls) from many different delimited files.
    e.g for a TAB delimited file you would pass a value of Chr(9) to the function.

    Here's the function
    VB Code:
    1. Function GetCSVList(Path As String, Column As Long, Delimiter As String) As Variant
    2. Dim fsDataFile As Scripting.FileSystemObject
    3. Dim sText As TextStream
    4. Dim sFullLine As String
    5. Dim saCsvData() As String
    6. Dim saList() As String
    7.    
    8.     'Create a new FileSystemObject
    9.     Set fsDataFile = New Scripting.FileSystemObject
    10.    
    11.     'Create a text stream from your FileSystemObject
    12.     Set sText = fsDataFile.OpenTextFile(Path, ForReading, False, TristateUseDefault)
    13.    
    14.     ReDim saList(0)
    15.    
    16.     'Loop through each line in the text stream
    17.     Do While Not sText.AtEndOfStream
    18.         'Get the complete line of text
    19.         sFullLine = sText.ReadLine
    20.        
    21.         'split the line of text into a 1 dimensional array
    22.         'using comma as the delimeter
    23.         saCsvData = Split(sFullLine, Delimiter)
    24.        
    25.         'Add the new value and then
    26.         'Increase the size of the return array
    27.         saList(UBound(saList)) = saCsvData(Column - 1)
    28.         ReDim Preserve saList(UBound(saList) + 1) As String
    29.     Loop
    30.    
    31.     'Remove the extra line form the array
    32.     ReDim Preserve saList(UBound(saList) - 1) As String
    33.    
    34.     'Clear Object Variables
    35.     Set sText = Nothing
    36.     Set fsDataFile = Nothing
    37.        
    38.     'Return the functions value
    39.     GetCSVList = saList
    40. End Function

    And here's how you would use it to populate a combo.
    VB Code:
    1. Private Sub UserForm_Initialize()
    2. Dim vComboList As Variant
    3. Dim lItemNum As Long
    4.  
    5.     vComboList = GetCSVList(Path:="C:\Data\test.csv", Column:=2, Delimiter:=",")
    6.    
    7.     Me.ComboBox1.Clear
    8.     For lItemNum = LBound(vComboList) To UBound(vComboList)
    9.         Me.ComboBox1.AddItem vComboList(lItemNum)
    10.     Next lItemNum
    11.    
    12.     Me.ComboBox1.ListIndex = 0
    13. End Sub
    Last edited by DKenny; Feb 15th, 2006 at 11:42 AM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    6

    Re: Fill combobox CSV

    The displayed value of the combobox is "test". I would like to delete the "" but I can't do by the delimeter..

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] Fill combobox CSV

    We can use the Replace method to remove the double quotes. I would suggest changing the _Initialize event proc to handle this, rather than changing the function.

    The best spot to do this is where we are adding the item to the combo.
    VB Code:
    1. Me.ComboBox1.AddItem Replace(vComboList(lItemNum), """", "")
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width