hi,
i want values from an Access table to appear in a combo box in Word and i'm
confused at how to start. can anyone point me in the right direction? thanks.
-roger
Doug Robbins - 22 Apr 2005 15:02 GMT
Here are a couple of methods. The first can be used to load a multicolumn
combo box (or list box); the second will load a single column combo box
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
Set db = Nothing
End Sub
Private Sub UserForm_Initialize()
'allocate memory for the database object as a whole and for the active
record
Dim myDataBase As Database
Dim myActiveRecord As Recordset
'Open a database
Set myDataBase = OpenDatabase("E:\Access97\Ely\ResidencesXP.mdb")
'Access the first record from a particular table
Set myActiveRecord = myDataBase.OpenRecordset("Owners", dbOpenForwardOnly)
'Loop through all the records in the table until the end-of-file marker is
reached
Do While Not myActiveRecord.EOF
ListBox1.AddItem myActiveRecord.Fields("Owner")
'access the next record
myActiveRecord.MoveNext
Loop
'Then close the database
myActiveRecord.Close
myDataBase.Close
End Sub

Signature
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
> hi,
> i want values from an Access table to appear in a combo box in Word and
> i'm
> confused at how to start. can anyone point me in the right direction?
> thanks.
> -roger
Dian D. Chapman, MVP - 22 Apr 2005 22:23 GMT
Doug's info is right no the money.
However, if you want to mess with a sample, I have a demo of how to
connect to an Access DB that you can play with and study the open
code. However, I use ADO, which is just the newer data wrapper to the
DAO code Doug gave you, but the idea is pretty much the same. The
sample might just help you see how it can work...although the data
only goes into a form field versus a combo box.
See the # 5 article in the Please Fill Out This Form series at this
link: http://www.mousetrax.com/techpage.html#autoform
And if you want to learn more about ADO, this book is a great resource
http://www.mousetrax.com/books.html#ado
Dian D. Chapman, Technical Consultant
Microsoft MVP, MOS Certified
Editor/TechTrax Ezine
Free MS Tutorials: http://www.mousetrax.com/techtrax
Free Word eBook: http://www.mousetrax.com/books.html
Optimize your business docs: http://www.mousetrax.com/consulting
Learn VBA the easy way: http://www.mousetrax.com/techcourses.html
>hi,
>i want values from an Access table to appear in a combo box in Word and i'm
>confused at how to start. can anyone point me in the right direction? thanks.
>-roger