Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running sql in vb code to put information into a document

Status
Not open for further replies.

Earth

Technical User
May 8, 2000
59
AU
I have a word document that I can successfuly open from Access code. I can also fill out some bookmarks on this document using fields from the calling form in Access.

My problem is adding additional information to this document that has to come from a query. I have written the query, and it works perfectly. I have attempted to fit the SQL into the code, but to no avail. This is the code that I'm working with - some of it is not mine and I don't know what it does.

Dim Result As Long
Dim WordApp As Object
Dim ReadOnly As Integer
Dim objword As Word.Application
Dim objDoc As Word.Document
Dim dbMidbase As Database
Dim rsMidbase As Recordset
Dim sSql As String

Set objword = New Word.Application
objword.Visible = True

Set objDoc = objword.Documents.Open("document", , 1)
objword.Activate

objDoc.Bookmarks("Company").Range.text = CompanyCombo
objDoc.Bookmarks("Person").Range.text = PersonName
objDoc.Bookmarks("Date").Range.text = TDate

sSql = "SELECT Documents.DRegistrationNumber, Jobs.[Job No], Jobs.Department, Jobs.[Job Name] FROM Documents INNER JOIN Jobs ON Documents.DOurJob = Jobs.[Job No]WHERE (((Documents.DRegistrationNumber)=[Forms]![Transmissions]![TDRegCombo]))"

Set dbMidbase = OpenDatabase("\\midbdata97.mdb") 'note this is my data file, not my application file
Set rsMidbase = dbMidbase.OpenRecordset(sSql)
objDoc.Bookmarks("Project").Range.text = rsMidbase.Fields("Job Name") 'this part doesn't work

Set objDoc = Nothing
Set objword = Nothing
Set dbMidbase = Nothing
Set rsMidbase = Nothing

Any suggestions? Is there a better way to do this?

Regards and thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top