SQL Server is well known for its programmability. You don't need to be a programmer, however, to use the data
stored in a SQL Server database. In fact, because Microsoft provides a SQL Server ODBC driver and an OLE
DB provider, any application capable of using such middleware can access a SQL Server database. Of the literally hundreds
of ODBC- or OLE DB–compliant applications, one of the best known and most widely used is Microsoft Office.
All the major Microsoft Office 2003 applications provide powerful tools that let you incorporate and use data from
a SQL Server database. For example, Microsoft Office Word 2003's mail merge capability lets you use contact information
stored in your SQL Server database to automatically address and print letters, envelopes, and other items for
mailing to customers or employees. Microsoft Office Access 2003 has powerful front-end and report-building capabilities
that let you create customized data entry screens and reports without having to master writing code. And you
can pair Microsoft Office Excel 2003 PivotTables with SQL Server or Analysis Services to
quickly rearrange, summarize, and analyze large amounts of data. It's worthwhile learning
how to access your SQL Server database from all three of these Office applications.
Word Mail Merge
Word's Mail Merge function offers a powerful
way to combine the text, graphics, and
logos supported by Word documents with
data from a SQL Server database. To create a
basic mail merge using Word and SQL Server,
start Word and click Tools, Letters and Mailings,
Mail Merge. On the right side of the
screen, a task pane offers a selection of document
types. Under Select document type, click
Letters, then click Next: Starting document.
Under Select starting document,choose Use the
current document and click Next: Select recipients.
Choose Use an existing list and click Browse
to display the Select Data Source window.
To create a Word data source that connects
to SQL Server, double-click +New SQL
Server Connection.odc. This action launches
the Data Connection Wizard, which lets you
enter your SQL Server connection, authentication,
and database selection information.
On the Connect to Database Server dialog
box, enter the name of your SQL Server
system. Under Log on credentials, choose the
type of login you want to use. You can either
select Use Windows Authentication, or you
can select Use the following User Name and
Password and type a SQL Server username
and password. When you click Next, the
wizard displays the Select Database and Table
dialog box you see in Figure 1.
As you can see, I've selected the Employees
table from the sample Northwind database,
which is easier to work with than the newer
AdventureWorks sample database.(SQL Server
2005 doesn't include Northwind, but you can download the scripts to install
the Northwind and pubs sample
databases at http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&displaylang=en.) After you select the desired
database and table, click Next to display the
Save Data Connection File and Finish dialog
box. The connection information is saved by
default using the names of the server, database,
and table. For example, my data connection
was saved as sql2005 Northwind
Employees.odc.
Click Finish, then select and sort the
columns that you want to include in the
mail merge from the Mail Merge Recipients
dialog box. After you click OK to
include the rows and columns you've
chosen, Mail Merge returns you to your
Word document. You'll see your selected
data source in the Use an existing list section.
Click Next: Write your letter to begin composing
your letter or memo.
After writing your letter in Word as usual,
insert the merge fields where you
want them to appear. For instance,
click the location in the letter
where you want to insert a
merged address and choose the
Address block link from the Mail
Merge Write your letter task pane.
A dialog box shows you the
address format. Next,click Match
Fields to display the Match Fields
dialog box shown in Figure 2.
The Match Fields dialog box
lets you match SQL Server
column names to the data elements
that will be inserted into
your document. As Figure 2 shows, the Match Fields dialog box was able to correctly match most of the
columns on the right to the address block
fields on the left. You can do the same for a
greeting line by selecting the Greeting line link
in the Mail Merge task pane. To insert values
from other database columns into the text,
just position your cursor where you want to
insert an item, click the More items link in the
Mail Merge task pane, and scroll through the
list to select the columns you want.
After matching all the merge fields, click
Next: Preview your letters to see how the finished
letter will look. All the merged text
should appear in the document. If everything
looks fine,click Next: Complete the merge,
then click Print. After you save the document,
you can run the mail merge again in
the future by reopening the document,
selecting Mail Merge from the Word
toolbar, then selecting the Print option.
Prev. page  
[1]
2
3
next page