setting of querystring

W

Warren Siu

I'm going to set query to MailMerge like this, but it does
not work. What is the problem?
strMerge = "SELECT * FROM 'rst' WHERE [TI Ref] >= " & Me!
txtFrom & " AND [TI Ref] <= " & Me!txtTo
ActiveDocument.MailMerge.DataSource.QueryString = strMerge
 
P

Peter Jamieson

Assuming TI Ref is numeric and the values in Me!txtFrom and Me!txtTo are
always numeric, the problem is probably not in the WHERE clause. you
probably need to chage 'rst' to one of the following:
rst (i.e. no quoting)
[rst]
`rst` (i.e. using single back quotes (they are on the key to the left of
the 1 key on a UK keyboard, not sure about other keyboards)

If TI Ref is alphanumeric you will need to quote Me!txtFrom and Me!txtTo,
e.g.

strMerge = "SELECT * FROM 'rst' WHERE [TI Ref] >= '" & Me!
txtFrom & "' AND [TI Ref] <= '" & Me!txtTo & "'"

You may also need to use cstr(Me!txtFrom) etc. in that case.
 
C

Cindy Meister -WordMVP-

Hi Warren,
I'm going to set query to MailMerge like this, but it does
not work. What is the problem?
strMerge = "SELECT * FROM 'rst' WHERE [TI Ref] >= " & Me!
txtFrom & " AND [TI Ref] <= " & Me!txtTo
ActiveDocument.MailMerge.DataSource.QueryString = strMerge
What, more exactly, does Me! represent, here?

And if you're getting an error message, what is it? "does not
work" is really rather vague...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan
24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
P

Peter Jamieson

1. You need to connect to the datasource from Word using the OpenDataSource
method - just setting the querystring is not enough.

2. You do not need the set db= and set rst= statments for the mailmerge
(although you may need them for something else).

3. I'm not sure what 'rst' is in your SELECT statement. But WOrd can only
work with data stored in the database, not data available within an open
form. So my guess is that you actually need something more like

strMerge = "SELECT * FROM [TI_UME Query] WHERE [TI Ref] >= " & _
Me!txtFrom & " AND [TI Ref] <= " & Me!txtTo
myMerge.OpenDataSource _
Name:="f:\UNE_K 2000.mdb", _
SQLStatement:=strMerge

--
Peter Jamieson
MS Word MVP

Warren Siu said:
Dear Cindy and Peter
Thank you for your reply.
Actually I am making an Access Form this is for printing
records to be specified in textbox "txtFrom" and "txtTo".
The statements are as following.
MergeWord.Visible = True
Set MergeDoc = MergeWord.Documents.Open(FileName:="f:\TI
Record K.doc")
Set myMerge = ActiveDocument.MailMerge
Set db = OpenDatabase("f:\UNE_K 2000.mdb")
Set rst = db.OpenRecordset("TI_UME Query")strMerge
= "SELECT * FROM 'rst' WHERE [TI Ref] >= " & Me!txtFrom
& " AND [TI Ref] <= " & Me!txtTo
myMerge.DataSource.QueryString = strMerge
.
.
.
The program just halted at the last line with error
massage: "Run-time error '4198', Command failed." shown.
According to the Office assistance it is allow to set the
arrange of MailMerge DataSource with QueryString, but I
can not figure the problem.
Thank you for your advice in advance.
-----Original Message-----
Hi Warren,
I'm going to set query to MailMerge like this, but it does
not work. What is the problem?
strMerge = "SELECT * FROM 'rst' WHERE [TI Ref] >= " & Me!
txtFrom & " AND [TI Ref] <= " & Me!txtTo
ActiveDocument.MailMerge.DataSource.QueryString = strMerge
What, more exactly, does Me! represent, here?

And if you're getting an error message, what is it? "does not
work" is really rather vague...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan
24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)

.
 

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