populate combobox from csv

N

natanz

I have been working with a document template, for which i am setting up
a userform to collect some info, that will then be put into the new
document.

here is the code i have so far:

Private Sub UserForm_Initialize()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim noOfRecords As Integer

'open the database
Set db = OpenDatabase("K:\source\projectlist.xls", False, False,
"excel 8.0")

'retrieve the recordset
Set rs = db.OpenRecordset("SELECT * FROM `Projnumbers`")
Set rs2 = db.OpenRecordset("SELECT * FROM `Projnames`")

'determine the number or retrieved records
With rs
.MoveLast
noOfRecords = .RecordCount
.MoveFirst
End With

' With rs2
' .MoveLast
' noOfRecords = .RecordCount
' .MoveFirst
' End With


'set the number of columns = number of fields in the recordset
'ListBox1.ColumnCount = rs.Fields.Count

'load the listbox with the retrieved records
ListBox1.Column = rs.GetRows(noOfRecords)
ListBox2.Column = rs2.GetRows(noOfRecords)

'cleanup
rs.Close
db.Close

Set rs = Nothing
Set db = Nothing
Set rs2 = Nothing

End Sub

as you can see it is connecting to an excel file and filling two
comboboxes with named ranges in the excel file. For reasons beyond my
control, at least for now, it would be better to connect to a TXT file,
or a CSV file. But those files don't have any provisions for named
ranges, at least not that i know of. Any help is appreciated.
 
D

Doug Robbins

How about from a Word document?

This routine loads a listbox with client details stored in a table in a
separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.


Private Sub UserForm_Initialize()
Dim sourcedoc As Document, i As Integer, j As Integer, myitem As Range,
m As Long, n As Long
' Modify the path in the following line so that it matches where you
saved Clients.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.doc")
' Get the number or clients = number of rows in the table of client
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of client details
j = sourcedoc.Tables(1).Columns.Count
' Set the number of columns in the Listbox to match
' the number of columns in the table of client details
ListBox1.ColumnCount = j
' Define an array to be loaded with the client data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' Load data into ListBox1
ListBox1.List() = MyArray
' Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

The Initialize statement will populate the listbox with the data from the
table.

To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top