Launch a query in code and access fields

Simmo2010

Registered User.
Local time
Yesterday, 22:37
Joined
Nov 28, 2007
Messages
18
Guys,

For whatever reason I have three queries (different) that I want to use to populate one Word template. I know the code to open the document and do the word bit but I dont know the code to run a query and then access the fields in it.... any help much appreciated.
 
If you want to access various fields, and records from a query, then use that query as a RecordSet within VBA. Lookup RecordSet in access VBA help for more in-depth information. If you have any questions after reading up on RecordSets, ask away!
 
Yep worked it out, sorry I should have posted! Basically I used this code (just in case useful to anyone else):

Dim db As Database
Dim rst As Recordset
Dim objWord As Word.Application

Set db = CurrentDb

'Open Word Document
Set objWord = CreateObject("Word.Application")

With objWord
'Make the application visible.
.Visible = True

'Open the document.
.Documents.Open ("C:\AllCompDetails.doc")
End With

'Move to each bookmark and insert text from the form.

.Selection.Text = (CStr(Me.[Company Name].Value))
.ActiveDocument.Bookmarks("TrustContact").Select
.Selection.Text = (CStr(Me.[Trust Contact Person].Value))
.ActiveDocument.Bookmarks("TrustName").Select
.Selection.Text = (CStr(Me.[Trust Company.Trust Company].Value))
.ActiveDocument.Bookmarks("CompanyKey").Select
.Selection.Text = (CStr(Me.Company_Key))
.ActiveDocument.Bookmarks("TrustEmail").Select
.Selection.Text = (CStr(Me.[Trust Email]))
.ActiveDocument.Bookmarks("CRIBNr").Select
.Selection.Text = (CStr(Me.[CRIB Number]))

'Now Open First Query and populate Document with field information

Set rst = db.OpenRecordset("qryPageOne")
With objWord
.ActiveDocument.Bookmarks("CompanyName").Select
.Selection.Text = rst("cmp_name")
.ActiveDocument.Bookmarks ("IncorpDate")
.Selection.Text = rst("cmp_inc_date")
.ActiveDocument.Bookmarks ("Notary")
.Selection.Text = rst("cmp_notary")


End With
 

Users who are viewing this thread

Back
Top Bottom