In a new module, place the following code. Note: you will need to set a reference to DAO if you don't already have one, and a reference to whatever version of Microsoft Word you have. From the code window, Tools/References
Public Sub SetQuery(strQueryName As String, strSQL As String)
'set the query from which the merge document will pull its info
Dim qdfNewQueryDef As QueryDef
Set qdfNewQueryDef = CurrentDb.QueryDefs(strQueryName)
qdfNewQueryDef.Sql = strSQL
qdfNewQueryDef.Close
RefreshDatabaseWindow
End Sub
Public Sub OpenWordDoc(strDocName As String)
'Set the directory for any letters generated
Const strDir As String = "G:\Victims"
Dim gobjWord As Word.Application
Dim gobjDoc As Word.Document
strDocName = strDir & strDocName
'Open the letter in Word
On Error GoTo WordError
Set gobjWord = New Word.Application
gobjWord.Application.Visible = True
Set gobjDoc = gobjWord.Documents.Open(strDocName)
'Dim WordInstance As Object
'Make document visible for editing
gobjWord.Application.Visible = True
'reset the variables
Set gobjWord = Nothing
Set gobjDoc = Nothing
Exit Sub
WordError:
MsgBox "Err #" & Err.Number & " occurred." & Err.Description, vbOKOnly, "Word Error"
gobjWord.Quit
End Sub
A hyperlink will only open a document, you're right about that. Change the hyperlink to a label (you can even change its font and background settings to make it look like a hyperlink, if you want.
Call the SetQuery and OpenWordDoc code from the click event of the label. It will look something like this:
dim strSQL as string
strSQL = "SELECT * From MyTable WHERE MyTable.CustID = '" & NameOfForm.CustID & "'"
Call SetQuery ("NameOfMyQuery", strSQL)
Call OpenWordDoc("NameOfDocToOpen)