|
-
Feb 15th, 2006, 08:38 AM
#1
Thread Starter
New Member
[RESOLVED] Fill combobox CSV
Hi,
I want to fill a combox with data out of a colum of a CSV-file. Any suggestions?
-
Feb 15th, 2006, 08:53 AM
#2
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 
-
Feb 15th, 2006, 08:59 AM
#3
Thread Starter
New Member
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.
-
Feb 15th, 2006, 09:00 AM
#4
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 
-
Feb 15th, 2006, 09:02 AM
#5
Thread Starter
New Member
Re: Fill combobox CSV
Yes the column contains unique values.
-
Feb 15th, 2006, 09:18 AM
#6
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:
Sub PopCombo()
Dim fsDataFile As Scripting.FileSystemObject
Dim sText As TextStream
Dim sPath As String
Dim sFullLine As String
Dim saCsvData() As String
' Delete all value currently in the combo
ThisDocument.ComboBox1.Clear
'Create a new FileSystemObject
Set fsDataFile = New Scripting.FileSystemObject
'The fully qualified path to your lookup file
sPath = "C:\book2.csv"
'Create a text stream from your FileSystemObject
Set sText = fsDataFile.OpenTextFile(sPath, ForReading, False, TristateUseDefault)
'Loop through each line in the text stream
Do While Not sText.AtEndOfStream
'Get the complete line of text
sFullLine = sText.ReadLine
'split the line of text into a 1 dimensional array
'using comma as the delimeter
saCsvData = Split(sFullLine, ",", 2)
ThisDocument.ComboBox1.AddItem saCsvData(1)
Loop
Set sText = Nothing
Set fsDataFile = Nothing
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Feb 15th, 2006, 10:08 AM
#7
Thread Starter
New Member
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.
-
Feb 15th, 2006, 11:16 AM
#8
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:
Function GetCSVList(Path As String, Column As Long, Delimiter As String) As Variant
Dim fsDataFile As Scripting.FileSystemObject
Dim sText As TextStream
Dim sFullLine As String
Dim saCsvData() As String
Dim saList() As String
'Create a new FileSystemObject
Set fsDataFile = New Scripting.FileSystemObject
'Create a text stream from your FileSystemObject
Set sText = fsDataFile.OpenTextFile(Path, ForReading, False, TristateUseDefault)
ReDim saList(0)
'Loop through each line in the text stream
Do While Not sText.AtEndOfStream
'Get the complete line of text
sFullLine = sText.ReadLine
'split the line of text into a 1 dimensional array
'using comma as the delimeter
saCsvData = Split(sFullLine, Delimiter)
'Add the new value and then
'Increase the size of the return array
saList(UBound(saList)) = saCsvData(Column - 1)
ReDim Preserve saList(UBound(saList) + 1) As String
Loop
'Remove the extra line form the array
ReDim Preserve saList(UBound(saList) - 1) As String
'Clear Object Variables
Set sText = Nothing
Set fsDataFile = Nothing
'Return the functions value
GetCSVList = saList
End Function
And here's how you would use it to populate a combo.
VB Code:
Private Sub UserForm_Initialize()
Dim vComboList As Variant
Dim lItemNum As Long
vComboList = GetCSVList(Path:="C:\Data\test.csv", Column:=2, Delimiter:=",")
Me.ComboBox1.Clear
For lItemNum = LBound(vComboList) To UBound(vComboList)
Me.ComboBox1.AddItem vComboList(lItemNum)
Next lItemNum
Me.ComboBox1.ListIndex = 0
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 
-
Feb 16th, 2006, 02:22 AM
#9
Thread Starter
New Member
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..
-
Feb 16th, 2006, 09:33 AM
#10
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|