Fill combo boxes from Access tables

K

Kevin R

I have a Word template (dot) with a UserForm that opens and has several combo
boxes for the user to make selections. It then uses these selections to
build a letter. I've also created an Access database with tables and fields
to populate the various combo boxes on the UserForm in Word. I've managed to
figure out how to do this with 1 combo box (cboHOName) on the form and 1
table/field from Access but now want to populate the other combo boxes as
well from the other tables inside the same Access database. For example in
tblOffices I have an Address1 and Address2 field with corresponding combo
boxes (cboAddress1 & cboAddress2) on the Word UserForm and tblTitles with the
EETitle field and combo box cboEETitle. I'm just not sure how to modify my
code to incorporate these as well. Here's what I have, please help!

Private Sub UserForm_Initialize()

Dim dbDatabase As Database
Dim rsTemplates As Recordset
Dim i As Integer
Dim aResults()

'Activates the Database connection.
Set dbDatabase = OpenDatabase("K:\AcsData\ROTemplatesDB.mdb")

'Opens the Offices table.
Set rsROTemplates = dbDatabase.OpenRecordset("tblOffices", dbOpenSnapshot)

i = 0

With rsROTemplates
'Populate the combobox with the values in the cboHOName field.

Do Until .EOF
cboHOName.AddItem (i)
cboHOName.Column(0, i) = .Fields("HOName")
.MoveNext
i = i + 1
Loop

End With

End Sub
 
D

Doug Robbins - Word MVP

I would be surprised if you really want the Address1 and Address2 is
comboboxes that are separate from the cboHOName combobox. Rather, that
should be a multicolumn combobox that is loaded with the Name and Address
details in separate columns and you then use the Bound Column property of
the combobox to access each element of the the Name and Address for
insertion into your document.

To populate a multicolumn combobox or listbox use:

Private Sub UserForm_Initialize()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
' Open the database
Set db = OpenDatabase("D:\Access\ResidencesXP.mdb")
' Retrieve the recordset
Set rs = db.OpenRecordset("SELECT * FROM Owners")
' Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
' Set the number of Columns = number of Fields in recordset
ListBox1.ColumnCount = rs.Fields.Count
' Load the ListBox with the retrieved records
ListBox1.Column = rs.GetRows(NoOfRecords)
' Cleanup
rs.Close
db.Close
Set rs = Nothing
'insert repeated code here - modify the table name of course.
Set db = Nothing
End Sub

You would need to repeat the following section of code in the above routine
to load a second combobox with data from a different table.

Set rs = db.OpenRecordset("SELECT * FROM Owners")
' Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
' Set the number of Columns = number of Fields in recordset
ListBox1.ColumnCount = rs.Fields.Count
' Load the ListBox with the retrieved records
ListBox1.Column = rs.GetRows(NoOfRecords)
' Cleanup
rs.Close
db.Close
Set rs = Nothing



--
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
 
K

Kevin R

Maybe I worded my question wrong. My table has 3 columns - HOName, Address1
(Street) and Address2 (City). What I need to happen is when I pick the
office from the combobox for it to populate the text boxes (txtAddress1 &
txtAddress2) on the form with what ever the street and city. I need to keep
the 3 items separate because each are referenced separately and in multiple
places in the document when it is generated. Maybe what I want to do just
isn't doable. Thanks.
 
D

Doug Robbins - Word MVP

The information in the following differs somewhat from what you are doing,
and it concatenates the data from each of the address details, but it will
show you how the BoundColumn property is used to access the data from each
column of the listbox or combobox:

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.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

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

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub

The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.



--
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