View Single Post
  #8   Report Post  
Posted to microsoft.public.word.mailmerge.fields
NeedHelp NeedHelp is offline
external usenet poster
 
Posts: 5
Default Set a field before running query

Well, I'm back after getting side tracked. So I've been playing
around to get familiar with ActiveDocument.MailMerge stuff in VBA.

So my current thought process is this...
* The Word document is set to use queryA from xxxSW.mdb
* There's a field (Year) in a Linked table (Parameters)
* queryA uses Year
* I want to use InputBox to allow the user to modify Year
* I would use the user input and modify the Param table
* After the modification, I want to "refresh" the result for queryA

I'm still trying to understand how Word is deals with DataSources
so I am confused. How can I deal with multiple DataSources
(the Parameters table and the queryA query) before Word tries
to start merging?

Right now in AutoOpen(), I'm getting stuck at OpenDataSource:
ActiveDocument.MailMerge.OpenDataSource _
Name:="d:\member\memberSW.mdb", _
LinkToSource:=True, _
ReadOnly:=False, _
AddToRecentFiles:=False, _
SQLStatement:="Select * From Parameters", _
Connection:="TABLE Parameters", _
Revert:=False
The above opens a "Select Table" window and it is using another
..mdb file. Using the "Database" Dropdown list, I have to select
"memberSW.mdb". Then, because it's a link, I have to use Options
and select Synonyms. After I select Parameters I get prompted
with multiple "Invalid Merge Field" windows. Which apparently
is because Word is trying to tie my MergeField definitions with
the Parameters table (which doesn't match).

But the thing is, why am I getting the "Select Table" window
in the first place? Why doesn't the OpenDataSource do what I
think it should do?

Note: If I remove the "SQLStatement", it opens the correct
file (memberSW.mdb), but still opens the "Select Table" window
and there's no "Options" button to select Synonyms and the
linked Parameters table.


I hope this is solvable...


Peter Jamieson wrote:
In this case I would consider an AutoOpen or AutoNew macro. In Word you can
put it in any module in the document or the template it is attached to - use
AutoNew if it's in a template, and AutoOpen in a document.

Then you would need to capture the value. The simplest way is to use

Dim strValue As String
strValue = InputBox("Prompt text for the box")

To build the SQL, look at your query. Suppose it is something like

SELECT [t2].* FROM [Table2] [t2] WHERE Year([t2].[PaidDate]) =
theyeartheuserwants

(I am assuming you do not actually need to use Table1 in this case)

Then your query text can - in theory - be

Dim strQuery As String
strQuery = "SELECT [t2].* FROM [Table2] [t2] WHERE Year([t2].[PaidDate]) = "
& strValue

Then you can set up the merge data source, e.g.

ActiveDocument.MailMerge.OpenDataSource _
Name:="the full pathname of your Access file.mdb", _
SQLStatement:=strQuery

I say "in theory" because when a query constructed using the above technique
is potentially insecure - the user may be able to enter something in the
InputBox that makes Access execute a completely unexpected query. So it is
advisable to validate the data - in this case, probably check that it is a
credible 4-digit year. I leave you to do that.

InputBox (sorry, I think I said something else before) is a rather
old-fashioned way to get a user response these days, so you might want to go
for a Userform instead, but if so, I suggest you find out how to do that
elsewhere. There may be articles on the http://word.mvps.org site that tell
you how. Yes, you could also try to grab a value that was entered in
response to an ASK or FILLIN field in Word, but then it can be a bit harder
to make Word run the macro at the point you want.

I have to leave it to you to explore that, at least until tomorrow.

Peter Jamieson


"NeedHelp" wrote in message
...
Peter, I would like to use the VBA approach, but I'm not sure how
to get started. Are there events available where I could prompt
the user to enter data when the document opens? Or a button that
the user pushes to trigger something?

I found a template ELGMADR.DOT which, after linking to my database,
allowed me to generate a list. I see the module for the template,
but I'm still not sure how I can "trigger" an event to enter data.



Peter Jamieson wrote:
Well...

If your user has Access on their system, and you make the connection to
Access using the old DDE method, you can connect to a parameter query in

snip

You could also consider using Word VBA's OpenDataSOurce method to issue
the correct SQL (e.g. prompt for the value before the merge using a
Userform or Inputline, construct the SQL, and issue it via
OpenDataSource) or to EXECUTE the parameter query and pass the parameter
you want (not something I have used "for real"), and possibly a couple of
other approaches.

Peter Jamieson