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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merge record to Word

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I am using the following code to merge a record to microsoft word:

Code:
Private Sub cmdMergeIt_Click()
'creates an SQL statement to be used in the query def
On Error GoTo ErrorHandler
DoCmd.SetWarnings False
Me.Refresh
DoCmd.OpenQuery "qryAddTempTitle"
' user enters a zip code in a text box on the form;
' the query's criteria is set to pull records for
'that zip code

'Dim strPostalCode As String
'strPostalCode = txtPostalCode.Value
Dim strSQL As String
'replace the SQL statement below with the SQL statement
'from your query. This sample shows how to use single quotes
'to incorporate string values from the form's fields
'into the SQL statement. For dates, use # instead of the
'single quotes
strSQL = "SELECT TOP 1 TitlesTemp.* FROM TitlesTemp"


Dim strDocumentName As String  'name of the Word template document
strDocumentName = "ReportTemplate.dotx"
'use your template document name above

Call SetQuery("qryGetLastTitle", strSQL)
'use your query name above
Dim strNewName As String  'name to use when saving
'the merged document
'this next line of code makes the document name pattern
'like this: Custom Labels January 11, 2005.doc
'strNewName = "Title Check Report " & Format(CStr(Date), "dd MM yyyy")
strNewName = Me.AccountNumber.Value & "_Title Check Report "
'use your file name pattern above
Call OpenMergedDoc(strDocumentName, strSQL, strNewName)
DoCmd.OpenQuery "qryDeleteTempTitle"
DoCmd.SetWarnings True
Me.Page1.SetFocus
Exit Sub
ErrorHandler:
    MsgBox "Error #" & Err.Number & " occurred. " & Err.Description, vbOKOnly, "Error"
    Exit Sub
End Sub

The code works fine on one of my forms. I create new form and add a button and attach the code to it but I get the error:

Code:
 Compile Error: Sub of Function not defined
.

The error occurs on the line:

Code:
Call SetQuery("qryGetLastTitle", strSQL)
 

Where is the definition of SetQuery?

I bet it is in the Class Module for the original form, probably a Subroutine that uses the CreateQueryDef Method to actually make the query. It has to either be in both Class Modules (the original form and the new one) or better, cut and paste it into a Standard Module so either form can have access to it.

In the original form (the one that works) put your cursor over SetQuery and right click. From the shortcut list pick Definition. That will take you to the code that is the SetQuery subroutine. Cut that code and place it in a Standard Module (make sure you have any Dim statements that you need from the declarations section of the module) and it should work from any form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top