View Full Version : Output to Word


Dobie
11-09-2001, 01:31 AM
--------------------------------------------------------------------------------
I have a form which displays clients records, ie address details is there any way I can have a button to click and open word with the clients address automaticaly entered, but just for one record and not the whole database. And in effect call the button write out going correspondence. ???

DJN
11-09-2001, 04:07 AM
Have a look a mail merge in the help file. That should point you in the right direction.

The_Doc_Man
11-09-2001, 06:09 AM
I'll add that you can make your task easier by doing some of the work ahead of time in the Word file. This sounds like a lot of work and might seem to be incredibly messy, but it ought to work.

I'm going to assume the main table's name is My_Table and that the name you want to select is available in a text box on your form as [TheName].

Build a temporary table. Here I'll call it Temp_Table. Make sure it has the fields you need for this document. The name field might be called [LastName]. Define Temp_Table BEFORE you define the merge template.

Make a merge-master template that uses Temp_Table as its record source. Hint: You will PROBABLY want to use "catalog" in the merge options if this is a formal letter.

Next, define an Append query to populate this source table with the fields you need (call the query Populate_My_Table) and a Delete query to erase it totally (call it Erase_My_Table).

Then on your form, put some code behind the scenes of your action button.

Dim loRecs as Long
Dim stQry as String
Dim stSQL as String

...
stSQL = "DELETE * FROM [Temp_Table] WHERE [LastName] <> """ & Trim$([TheName]) & """"
stQry = "[LastName] = """ & Trim$([TheName]) & """"

DoCmd.SetWarnings False
DoCmd.OpenQuery Erase_My_Table
DoCmd.OpenQuery Populate_My_Table
loRecs = DCount("[LastName]", "Temp_Table", stQry)
If loRecs = 0 Then
MsgBox "No record available with that name"
GoTo Cannot_Do_It
ElseIf loRecs <> 1 Then
MsgBox "Too many records available with that name"
Goto Cannot_Do_It
Else
DoCmd.RunSQL stSQL
DoCmd.SetWarnings True
End If
... do the Word thing

Cannot_Do_It:
DoCmd.SetWarnings True

Then open your Word with the mailmerge options as it points to that file. (Obviously, the above example doesn't take into account duplicate last names. I will assume you can expand on this yourself to take such cases into account.)