Trying to use SQL in VBA to send to Word template...

R

ryan_eng

I've looked all over the web and this forum for ways to send data from the
current record to a word template. So far everything works great except for
one value that requires a query to retrieve another value.

The following is the latest incarnation of the code I'm using.
--------------------------------------------------------------------
Public Sub print_rel_sheet_Click()

Dim objWord As Word.Application
Dim releasesheet As Word.Document
Dim strAC_type As String
Dim strSQL As String
Dim rs As Recordset
Dim db As Database

Set objWord = New Word.Application
objWord.Documents.Add "X:\00-elisen\Elisen
Database\Elisen_Drawing_Release_Sheet.dot"
Set releasesheet = objWord.ActiveDocument

If Not IsNull(Me!AC_type) Then
strSQL = "SELECT tbl_Drawing_AC_prefix.AC_type FROM
tbl_Drawings_AC_prefix WHERE AC_dwg_prefix =" & (Me!AC_type)
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)
If Not rs.EOF Then
strAC_type = Nz(rs.Fields(0).Value)
rs.Close
End If
End If

With objWord.ActiveDocument
.FormFields("Dwg_Title").Result = Nz(Me!Dwg_Title)
.FormFields("Dwg_No").Result = Nz(Me!Dwg_No)
.FormFields("Dwg_Project_No").Result = Nz(Me!Project_No)
.FormFields("AC_type").Result = strAC_type
.FormFields("SN_effy").Result = Nz(Me!SN_effy)
.FormFields("No_Shts").Result = Nz(Me!No_Shts)
.FormFields("Drawn_by").Result = Nz(Me!Drawn_by)
.FormFields("Checked_by").Result = Nz(Me!checked_by)
.FormFields("Release_Date").Result = Nz(Me!Dwg_Rel_Date)
End With
objWord.Visible = True
objWord.PrintPreview = True

End Sub
---------------------------------------------------------------------------

I tried using the code located a
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnima02/html/ima0102.asp
but I get errors related to accessing a network drive. Since the database
and the template are in the same folder I don't see why there should be
problems with accessing the drive.

Any input is appreciated! Thanks in advance
RYAN
 

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