Using an .odc File to Mailmerge with SQL Server

A

Adam Froio

Hi,

I'm using an .odc file to pull data from SQL Server 2000 into a Word 2003
mailmerge.

My problem is getting the syntax correct so that I can dynamically create
sql statements to use for the merge.

For example, this works:
Word.MailMerge.OpenDataSource Name:="c:\mailmerge\CustomerData.odc",
SQLStatement:="SELECT * FROM [vwCustomerData]"

This, however, does not:
Word.MailMerge.OpenDataSource Name:="c:\mailmerge\vwCustomerData.odc",
SQLStatement:="SELECT * FROM [vwCustomerData] WHERE State = 'CT'"

I can't seem to find more than a couple examples on how to use an .odc in
this way and I can find none that use a sql statement any more complicated
than the first example above.

Any help or advice would be very much appreciated.

Thanks!
Adam
 
P

Peter Jamieson

I can't see anything obviously wrong with your SQL and similar statements
work OK here. However, you may need to surround CT with straight quotes
('CT') rather than the type of quote I think you have used in your message.
Other things I have noticed are
a. the .odc does not (as far as I can see) let you specify any SQL
explicitly. It just lets you specify a fully qualified table name. So you
have to specify any SQL in the OpenDataSource, or by setting
ActiveDocument.MailMerge.DataSource.QueryString
b. in fact, the .odc can be a completely blank file (which means you can
use a single .odc for all SQL Server queries) as long as you specify the
correct connection string in the COnnection parameter of the OpenDataSource
call. Once you have done one successful connection, you can generally get a
suitable connect string by looking at
ActiveDocument.MailMerge.DataSource.ConnectString (which will probably be
truncated to 255 characters, but you can usually get rid of some of quite a
lot of the parameters in the string)
c. the SQL interpreter used can be a bit touchy about the syntax and
sometimes fully qualifying the field names and or using table alias names
can make a difference, e.g. try

SELECT vwC.* FROM [vwCustomerData] vwC WHERE vwC.State = 'CT'

d. you will probably be limited to a 255-character query string.

Peter Jamieson
 
A

Adam Froio

Thanks Peter, I'll try your suggestions and post back on what I find.

Adam


Peter Jamieson said:
I can't see anything obviously wrong with your SQL and similar statements
work OK here. However, you may need to surround CT with straight quotes
('CT') rather than the type of quote I think you have used in your message.
Other things I have noticed are
a. the .odc does not (as far as I can see) let you specify any SQL
explicitly. It just lets you specify a fully qualified table name. So you
have to specify any SQL in the OpenDataSource, or by setting
ActiveDocument.MailMerge.DataSource.QueryString
b. in fact, the .odc can be a completely blank file (which means you can
use a single .odc for all SQL Server queries) as long as you specify the
correct connection string in the COnnection parameter of the
OpenDataSource call. Once you have done one successful connection, you can
generally get a suitable connect string by looking at
ActiveDocument.MailMerge.DataSource.ConnectString (which will probably be
truncated to 255 characters, but you can usually get rid of some of quite
a lot of the parameters in the string)
c. the SQL interpreter used can be a bit touchy about the syntax and
sometimes fully qualifying the field names and or using table alias names
can make a difference, e.g. try

SELECT vwC.* FROM [vwCustomerData] vwC WHERE vwC.State = 'CT'

d. you will probably be limited to a 255-character query string.

Peter Jamieson
Adam Froio said:
Hi,

I'm using an .odc file to pull data from SQL Server 2000 into a Word 2003
mailmerge.

My problem is getting the syntax correct so that I can dynamically create
sql statements to use for the merge.

For example, this works:
Word.MailMerge.OpenDataSource Name:="c:\mailmerge\CustomerData.odc",
SQLStatement:="SELECT * FROM [vwCustomerData]"

This, however, does not:
Word.MailMerge.OpenDataSource Name:="c:\mailmerge\vwCustomerData.odc",
SQLStatement:="SELECT * FROM [vwCustomerData] WHERE State = 'CT'"

I can't seem to find more than a couple examples on how to use an .odc in
this way and I can find none that use a sql statement any more
complicated than the first example above.

Any help or advice would be very much appreciated.

Thanks!
Adam
 
A

Adam Froio

Using the alias seem to do the trick -- thanks!

I used the following syntax:
SQLStatement:="SELECT * FROM [vwCustomerData] vwCD WHERE vwCD.State = 'CT'"

Thanks again!


Adam Froio said:
Thanks Peter, I'll try your suggestions and post back on what I find.

Adam


Peter Jamieson said:
I can't see anything obviously wrong with your SQL and similar statements
work OK here. However, you may need to surround CT with straight quotes
('CT') rather than the type of quote I think you have used in your
message. Other things I have noticed are
a. the .odc does not (as far as I can see) let you specify any SQL
explicitly. It just lets you specify a fully qualified table name. So you
have to specify any SQL in the OpenDataSource, or by setting
ActiveDocument.MailMerge.DataSource.QueryString
b. in fact, the .odc can be a completely blank file (which means you can
use a single .odc for all SQL Server queries) as long as you specify the
correct connection string in the COnnection parameter of the
OpenDataSource call. Once you have done one successful connection, you
can generally get a suitable connect string by looking at
ActiveDocument.MailMerge.DataSource.ConnectString (which will probably be
truncated to 255 characters, but you can usually get rid of some of quite
a lot of the parameters in the string)
c. the SQL interpreter used can be a bit touchy about the syntax and
sometimes fully qualifying the field names and or using table alias names
can make a difference, e.g. try

SELECT vwC.* FROM [vwCustomerData] vwC WHERE vwC.State = 'CT'

d. you will probably be limited to a 255-character query string.

Peter Jamieson
Adam Froio said:
Hi,

I'm using an .odc file to pull data from SQL Server 2000 into a Word
2003 mailmerge.

My problem is getting the syntax correct so that I can dynamically
create sql statements to use for the merge.

For example, this works:
Word.MailMerge.OpenDataSource Name:="c:\mailmerge\CustomerData.odc",
SQLStatement:="SELECT * FROM [vwCustomerData]"

This, however, does not:
Word.MailMerge.OpenDataSource Name:="c:\mailmerge\vwCustomerData.odc",
SQLStatement:="SELECT * FROM [vwCustomerData] WHERE State = 'CT'"

I can't seem to find more than a couple examples on how to use an .odc
in this way and I can find none that use a sql statement any more
complicated than the first example above.

Any help or advice would be very much appreciated.

Thanks!
Adam
 

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