Re: Populate a combo box

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



The following will also work for a combo box. You will need a reference
(Tools>References) in the VBE to the Microsoft DAO 3.# object library.

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


--
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
"FredEp57" <FredEp57@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F2E50214-79EC-43AB-8B56-C0CC65A6DBDD@xxxxxxxxxxxxxxxx
>I have read here in the groups that a combo box can be populated. However,
>I
> am a very novice with VBA and cannot find any information on this.
>
> I would like to have this done from a table in an Access database. Is
> this
> possible? What would the code look like?
>
> Thanks
> F


.



Relevant Pages

  • Re: retriving data from excel with in word
    ... > getting the error runtime error 3170 ... > Private Sub UserForm_Initialize ... > Dim rs As DAO.Recordset ... > ' Load the ListBox with the retrieved records ...
    (microsoft.public.word.vba.userforms)
  • Re: inserting ACCESS fields in a word document
    ... The the following routine will populate a listbox or combobox with data from ... Dim rs As DAO.Recordset ... ' Load the ListBox with the retrieved records ...
    (microsoft.public.word.docmanagement)
  • Re: Populating a list box from a database
    ... > Dim rs As DAO.Recordset ... > ' Load the ListBox with the retrieved records ... >> Can I populate a list box in word from values within an access database? ...
    (microsoft.public.word.vba.general)
  • Re: Datacombo in Word forms
    ... Private Sub UserForm_Initialize ... Dim rs As DAO.Recordset ... ' Load the ListBox with the retrieved records ...
    (microsoft.public.word.vba.userforms)
  • Re: Using access data to fill-in pre-printed report forms
    ... database and insert into a Word document the data that you find there" at: ... Private Sub UserForm_Initialize ... Dim rs As DAO.Recordset ... ' Load the ListBox with the retrieved records ...
    (microsoft.public.word.mailmerge.fields)