Re: Populating a list box from a database

From: Mara M. Smith (Smith_at_discussions.microsoft.com)
Date: 11/29/04


Date: Mon, 29 Nov 2004 10:09:48 -0800

Doug,
I like your solution below. I tried it and it worked. I was wondering if
you or anyone else could provide a solution to one of my problems related to
this. I will be using the code you used when the form initializes but I used
my access database to populate a 'combo box'.

After making a selection in the 'combo box', if I click 'ok' on my user
form, I would like to pull a specific field(for example, the phone number of
the person I selected in the combo box) from that same database that
populated the 'combo box'. I would then want to insert that phone number in
a 'bookmark' named 'Phone_Number' in the Word file I am using.

How can I do this?

Thanks,
MM

"Doug Robbins" wrote:

> Use the following method (You need to set a reference to the DAO 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
>
>
> --
> Please respond to the Newsgroup for the benefit of others who may be
> interested. Questions sent directly to me will only be answered on a paid
> consulting basis.
>
> Hope this helps,
> Doug Robbins - Word MVP
> "Dingbat" <Dingbat@discussions.microsoft.com> wrote in message
> news:187666DA-38BC-49B5-AA40-91C406A04B61@microsoft.com...
> > Can I populate a list box in word from values within an access database?
> >
> > I am using Microsoft Word 2002/Access 2002.
> >
> > I want to be able to dynamically build the values within the list box from
> > the result of a query within access
>
>
>



Relevant Pages

  • Re: Extract fields from a database dependant on a user inputted ke
    ... Dim rs As DAO.Recordset ... ' Load the ListBox with the retrieved records ... here's how you get the data from the selected record into the document ... Populate the fields contained in the document with the retrieved data. ...
    (microsoft.public.word.vba.general)
  • 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: Populate a combo box
    ... Private Sub UserForm_Initialize ... Dim rs As DAO.Recordset ... ' Load the ListBox with the retrieved records ... > I would like to have this done from a table in an Access database. ...
    (microsoft.public.word.vba.general)
  • Re: Automated Mail Merge Displaying unwanted Dialog Boxes
    ... For creating an Access database (it's actually a .NET example but I think it ... There's no way to specify the delimiters ... no way to specify the delimiter in the OpenDataSource call or, ... Dim wrdApp As Word.Application ...
    (microsoft.public.word.mailmerge.fields)
  • Re: DB insert problems with apostrophe in a Surname and Datetime value
    ... dim Field1 as string ... > I'm copying data across from an Access database to an MSDE database ... > Also I have a 'date/time' value in my Access database that I have to copy ... > 'loop through all customer details in Access database, ...
    (microsoft.public.dotnet.framework.adonet)