3 4
Visio can link shape properties to database records with a two-way connection that you can keep up to date. In programmatic terms, the link is persistent, which means that after you set up the link between shapes and database records, the connection remains in effect as long as both the drawing file and database exist. When you change the value of linked shape properties, you can refresh the database with the values. Likewise, when the database changes, you can update shapes. Clearly, this functionality is a powerful way to tie drawings and data together. Your drawing file can become a dynamic catalog of information that accurately reflects the contents of a database. At a minimum, you can create a shape-to-database link to import values into custom property fields.
There are limits to what Visio can do when it creates a link between a shape and a database record. Be aware of the following limitations:
Note
Follow these steps to link shape properties:
For details about creating a data source, see "Connecting to an ODBC Data Source."
If you select Excel Files, the Select Workbook dialog box appears. Locate the file (.xls) that you want to use, and then click OK.
Note
InsideOut
You can select any shape on the drawing page and link it to a database record, but you get a more efficient result when you link master shapes instead. Each shape or master shape must be linked individually, so setting up links can be a time-consuming process no matter what type of shape you link. However, the advantage of linking master shapes is that you can drag the master onto the page to create many instances of the shape, and each instance can be linked to a different record in the database.
Visio includes a sample diagram and stencil of linked master shapes that demonstrate this feature. They're installed by default in the C:\Program Files\Microsoft Office\Visio10\1033\Samples\Visio Extras folder. The Sample Database Shapes.vss stencil contains a variety of linked master shapes. For example, when you drag the Office Card shape onto the drawing page, Visio displays the Select Database Record dialog box, in which you can select an employee record. The Office Card shape has built-in intelligence that adds the value of the database record to text fields on the shape, as Figure 24-5 shows. The Sample Database Airplane Seating.vsd drawing in the same folder shows another example of linked shapes in a drawing that's worth exploring.
You can open a master for editing, and then use the Link To Database command to link it to a database record. Or you can link a shape on the drawing page and then drag it to a stencil to create a master that includes links. To add functionality similar to that of the office card shape, in which dragging the shape onto the page causes the Select Database Record dialog box to open, you must click the Advanced tab in the Link To Database dialog box and then select the Select Record check box under Shape Drop Event.
Figure 24-5. The shapes on the Sample Database Shapes stencil are linked to records in an Access database (Dbsample.mdb) stored in the same folder as the stencil.
By default, Visio adds commands to the shortcut menus for linked shapes so that you can more easily manage the database connection. When you use the Link To Database command, however, you can specify which actions and events you will be able to control from the shortcut menu. Action and event are Visio programming terms. An action in this context means a command that causes an action to occur, such as refreshing linked shapes. An event is simply something that happens, such as a user opening a drawing file. A drop event occurs when a shape is added to the page (copied and pasted or dragged from a stencil). In the Link To Shapes dialog box, you can click the Advanced button to display the Advanced dialog box, which contains these additional options, as Figure 24-6 shows.
Figure 24-6. You can specify which shortcut commands Visio creates with the options under Shape Actions and Shape Drop Event in the Advanced dialog box.
You can specify the following actions:
Copying a linked shape either copies the same link or doesn't copy any link.
If you want to be able to copy and paste linked shapes—with links intact—you must specify an option that's available only when you click Advanced in the Link To Database dialog box. In the Advanced dialog box, the Shape Drop Event options control what happens when a shape is added to a page either through copying and pasting or by dragging a master from a stencil. If you select the Select Record option, Visio will prompt you to select a database record when you copy and paste a linked shape. That way, you can associate copies of shapes with new records. By default, the Refresh Shape option is selected, which means that copies of shapes are refreshed with the latest data from the same database record unless Select Record is also selected.
You can specify the following drop events:
If you want to create a table in your database and define its fields at the same time you link those fields to shape records, you can do so in the Link To Database dialog box. Instead of selecting a table in the Table/View box, click the New button to display the New dialog box, shown in Figure 24-7. Here, you can name a new table and identify its fields and their data types. Visio creates the table in your database and displays its fields in the Link To Database dialog box.
To create a new table, in the Table Name box type a name for the table that doesn't conflict with the names of other tables in the database. Then type the name of a field you want to include in the Name box, identify its data type in the Type box, and click Add. Make sure that at least one field is identified as a primary key, or Visio won't be able to refresh the link.
Figure 24-7. Rather than link to an existing table, you can create a new table in an existing database with the options in the New dialog box.
This section helps you understand exactly how Visio creates and stores the shape-to-database links that you create. After you link a shape to a database record, Visio creates a cell named User.ODBCConnection in the User-Defined Cells section of the ShapeSheet window. You can select a linked shape and then choose Window, Show ShapeSheet to see this cell and its contents, as Figure 24-8 shows.
The Link To Database command doesn't include one option that the Database Wizard does: the ability to generate new master shapes based on information contained in a database. This feature is particularly useful for organizations that maintain large databases of inventory or specification information. You can generate shapes that represent each type of part or each size of item in your database. However, to take full advantage of this option, you might want to design your own master shapes and then program them to respond to the database input.
To see how this feature works, experiment with the sample master shapes that are linked to the Dbsample.mdb database (C:\Program Files\Microsoft Office\Visio10\1033\Samples\Visio Extras\Sample Database Shapes.vss).
Figure 24-8. Visio stores the information about a shape-database link as a formula that specifies the primary key, field, and most recent value.
The formula in the User.ODBCConnection cell is a very long line of code that looks a lot more straightforward when you break each line at the vertical bar like this:
ODBCDataSource=Visio Database Samples
ODBCQualifier=
ODBCTable=Office - Card Details
1
Name=Prop.Name
3
Department=Prop.Department=0
Extension=Prop.Extension=0
Title=Prop.Title=0
This example is taken from the Office Card shape on the Sample Database Shapes stencil. Here's what Visio records with each parameter:
Table 24-1. Constants for Units of Measure
Constant | Unit of Measure | Constant | Unit of Measure |
---|---|---|---|
0 | String (text) | 66 | Feet |
1 | Formula | 68 | Miles |
32 | Nondimensional number | 69 | Centimeters |
33 | Percent | 70 | Millimeters |
40 | Dates | 71 | Meters |
50 | Points | 72 | Kilometers |
51 | Picas | 75 | Yards |
53 | Didots | 76 | Nautical miles |
54 | Ciceros | 81 | Degrees |
63 | Default page units | 83 | Radians |
64 | Default drawing units | 84 | Minutes (of an angle) |
65 | Inches | 85 | Seconds (of an angle) |