Import Access Query into Form Fieldson in a Template

A

Apprentice

I have a User Form with Form Fields that have bookmarks that identify each
field. I also have an Access Database that has corresponding fields.

Corresponding fields:

ProjectTitle
ProjectCatagory
ProjectLeader
TeamMember1
TeamMember2

Is it possible using VBA to import from the Access Query into the opened
Template, the corresponding fields? Some how the user would have to select
the Record wanted, but I think the fields could already be identified in the
code.

Also, would the Template have to be unprotected?
 
D

Doug Robbins - Word MVP

Use a listbox or a combobox on a userform (a real userform, not the sort
that you are talking about) and use the following code to populate it with
the records from the Access query

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



See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

Use the .BoundColumn property of the listbox or combobox to get the values
from the individual fields of the selected record and save the values as
variables in the document so that they can be displayed by the use of
{docvariable } fields.








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