Previous Page

Using Hyperlinks in Microsoft Access Applications

Hyperlinks can jump to two kinds of objects: a Web page or other content on the Internet or an intranet, or to a Microsoft Office document (Microsoft Word document, Microsoft Excel worksheet, Microsoft PowerPoint slide, or Microsoft Access database object) stored on a local hard disk or a LAN.

There are two ways you can use hyperlinks in Microsoft Access applications:

 Create a field with the Hyperlink data type to store hyperlink addresses in a table, and then bind that field to a text box on a form. Like other bound fields, as the user moves from record to record, the value in the text box changes to display the current record's hyperlink value. For example, you can use hyperlinks in this way to create an application in which users can jump to Web pages, or to other content on the Internet or an intranet, from a predefined list of addresses. You can also use hyperlinks in this way to create an application that displays and manages Microsoft Office documents.

 Create a label, image control, or command button on a form that references a specified hyperlink address. In this case, the hyperlink doesn't change as you move from record to record. For example, you can use hyperlinks in this way to navigate to other database objects within the same database, or to open a Web page on an intranet that contains updated information on how to use your application.

Regardless of how a hyperlink is defined in your application, if the hyperlink jumps to a database object or opens another Microsoft Office document, you can use the Web toolbar to navigate between the hyperlinks you've previously followed. For example, in the following illustration, the Products form has a command button with a hyperlink that opens a report. Once you have clicked the hyperlink, you can use the Web toolbar to navigate between other objects you've opened with hyperlinks.

Similarly, if you follow a hyperlink from a Microsoft Access form to open a Microsoft Word document, you can click the Back button on the Web toolbar in Microsoft Word to return to the form.

Note   By default, after a user clicks a hyperlink on a form, Microsoft Access continues to display the Web toolbar when the user closes the form. If you want to prevent this, you can use Visual Basic code in the OnClose event of the form to hide the toolbar. To see an example of Visual Basic code that does this, open the Products form in the Northwind sample application in Design view, and then display the event procedure in the OnClose event of the form.

Storing Hyperlinks in Tables

In Microsoft Access 97, a field in a table can store hyperlinks as data. To create a Hyperlink field, add a field in table Design view and set its DataType property to Hyperlink. You can also create a Hyperlink field in table Datasheet view by clicking Hyperlink Column on the Insert menu.

You can follow a hyperlink stored in a table by clicking it in the table, but more typically, the field will be bound to a text box control on a form.

   To add a text box that is bound to a Hyperlink field to a form

1   Open the form in Design view.

2   Make sure the form's RecordSource property is set to the table that contains the Hyperlink field, or to a query that includes the table.

3    Click Field List on the toolbar.

4   Drag the Hyperlink field from the field list to the form.

To see an example of how to use a Hyperlink field, open the Suppliers form in the Northwind sample application. The Home Page text box on the form is bound to the HomePage field in the Suppliers table. Clicking a hyperlink in the text box starts your Web browser and displays the supplier's home page.

You can also use hyperlinks in Microsoft Access to jump to database objects and other Microsoft Office documents. For example, you could create a document management application that uses a Hyperlink field to store paths to Microsoft Word documents on a network. Users of such an application could add records to track new documents, or click the hyperlink in a previously added record to open the specified document.

The Hyperlink Field Storage Format

A Hyperlink field stores up to three pieces of information: the displaytext, the address, and the subaddress. Each piece is separated by the pound sign (#), in the following format:

displaytext#address#subaddress

The following table describes each piece of the Hyperlink field storage format.

Piece

Description

Required?

displaytext

The text the user sees in the Hyperlink field in a table, or in a text box bound to the Hyperlink field. You can set the display text to any text string. For example, you may want the display text to be a descriptive name for the Web site or object specified by the address and subaddress. If you do not specify display text, Microsoft Access displays the value of address instead.

No

address

A valid URL that points to a page or file on the Internet or an intranet, or the path to a file on a local hard drive or LAN. If you enter a path on a LAN, you can omit a mapped drive letter and use the universal naming convention (UNC) format: \\server\share\path\filename. This prevents the path from becoming invalid if the database is later copied to another computer's hard drive or into a shared network folder.

Yes, unless subaddress points to an object in the current database (.mdb) file.

subaddress

The location within a file or document; for example, a database object, such as a form or report. When referring to a database object, the name of the object should be preceded by its type: Table, Query, Form, Report, Macro, or Module. Other possible values for subaddress include a bookmark in a Microsoft Word document, an anchor in an HTML document, a Microsoft PowerPoint slide, or a cell in a Microsoft Excel worksheet.

No

Each piece of the Hyperlink field storage format can be up to 2,000 characters. The maximum length of the entire Hyperlink field value is 6,000 characters.

The following table gives examples of valid Hyperlink field values.

Hyperlink field value

Jumps to

Cajun Delights#http://www. cajundelights.com/cajun.htm#

The Cajun Delights Web page. Only the words "Cajun Delights" are displayed in the field or text box.

#http://www.cajundelights.com/cajun.htm#

The Cajun Delights Web page. The text "http://www.cajundelights.com" appears in the field or text box because no display text is specified.

#http://www.cajundelights. com/cajun.htm#Price

The HTML anchor with the NAME attribute Price on the Cajun Delights Web page.

Resume#c:\windows\personal\resume.doc#

A Microsoft Word file named Resume.doc located in the \Windows\Personal folder. Only the word "Resume" is displayed in the field or text box.

#c:\windows\personal\resume.doc#

A Microsoft Word file named Resume.doc located in the \Windows\Personal folder. The text "c:\windows\personal\resume.doc" appears in the field or text box because no display text is specified.

#c:\windows\personal\resume. doc#Qualifications

The section in the Resume.doc Microsoft Word file marked with the bookmark name Qualifications.

#\\databases\samples\northwind.mdb#Form Suppliers

The Suppliers form in the Northwind sample application located in the Samples share on the Databases server on a LAN (UNC format path).

Suppliers Form##Form Suppliers

The Suppliers form in the current database. The words "Suppliers Form" are displayed in the field or text box.

#c:\windows\personal\1996 Sales.ppt#13

Slide 13 in the 1996 Sales Microsoft PowerPoint presentation located in the \Windows\Personal folder.

#c:\windows\personal\budget.xls#Sheet1!A2

The A2 cell in Sheet1 of the Budget.xls file located in the \Windows\Personal folder.

You can display the stored hyperlink format by pressing F2 when the insertion point is in the Hyperlink field.

See Also   For more information on the Hyperlink field storage format, search the Help index for "hyperlinks, addresses."

Entering a URL as a Hyperlink Address

To create a hyperlink that jumps to a Web page or other Internet content, you must enter a valid URL as the hyperlink address. You can enter a URL that points to any Internet file type or resource supported by the browser or to an ActiveX control, such as the WebBrowser control, that will be used to display or run it. You enter most URLs in the following format:

protocol://serveraddress/path

Protocol specifies the Internet protocol used to establish the connection to the server, and is generally followed by a colon and two slash marks. Serveraddress specifies what is usually called the domain name of the Internet server. Path specifies the location and name of the page or file on the Internet server. For example, the URL to the home page of the Microsoft Access Developer Forum is:

 http://www.microsoft.com/accessdev/

When you type a URL into a Hyperlink field, Microsoft Access automatically recognizes the following Internet protocols.

Protocol

Protocol name

Description

http

Hypertext Transfer Protocol

Jumps to Web pages that contain text, graphics, sound, and other digital information from a Web server on the World Wide Web.

ftp

File Transfer Protocol

Transfers files between computers on the Internet.

gopher

Gopher protocol

Displays information on a Gopher server.

wais

WAIS protocol

Accesses a Wide Area Information Servers database.

file

File protocol

Opens a file on a local hard drive or LAN.

https

Hypertext Transfer Protocol with privacy

Establishes an HTTP connection that uses Secure Sockets Layer (SSL) encryption.

mailto

MailTo protocol

Opens your electronic mail program to send a message to the specified Internet e-mail address. A URL that uses the MailTo protocol has a different format:
(mailto:username@domain)

msn

Microsoft Network protocol

Jumps to a location on The Microsoft Network.

news

News protocol

Starts a newsreader and opens the specified Usenet newsgroup. A URL that uses the News protocol has a different format:
(news:newsgroupname)

nntp

Network News Transfer Protocol

Performs the same function as News protocol, except two slashes follow the colon (nntp://newsgroupname).

mid

Musical Instrument Digital Interface (MIDI) protocol

Plays MIDI sequencer files if the user's computer has a sound card.

cid

CompuServe® Dialer (CID) protocol

Establishes a point-to-point protocol (PPP) connection with the Internet through CompuServe's network.

prospero

Prospero protocol

Opens files on the Prospero distributed file system.

telnet

Telnet protocol

Starts a telnet terminal emulation program. A terminal emulation program is a command-line interface that you can use to issue commands on a remote computer. For example, by using telnet to connect to a UNIX server, you can issue UNIX commands to perform operations on that server.

rlogin

Rlogin protocol

Starts an Rlogin terminal emulation program.

tn3270

TN3270 protocol

Starts a TN3270 terminal emulation program.

pnm

RealAudio protocol

Plays RealAudio streaming audio from a RealAudio server. Streaming audio and other streaming media formats establish a connection to the server and start playing immediately without downloading an entire file.

mms

Microsoft Media Server (MMS) protocol

Plays media such as ActiveMovie streaming format files (.asf) from an MMS server.

If you create a field by importing a column of data and all records in the imported data begin with one of these protocols, Microsoft Access automatically sets the data type of the imported field to Hyperlink. Similarly, if you create a new table in Datasheet view, and every entry you make in a field begins with one of these protocols, Microsoft Access sets the data type of the new field to Hyperlink when you save the table.

Creating a Label, Image Control, or Command Button That Follows a Hyperlink

To add a label, image control, or command button that follows a hyperlink to a form, set the HyperlinkAddress and HyperlinkSubAddress properties of the control to point to the content on the Internet or an intranet, or to the Microsoft Office document or Microsoft Access database object you want to jump to.

Note   The HyperlinkAddress and HyperlinkSubAddress property settings correspond to the address and subaddress values entered for a Hyperlink field. For more information on these values, see "The Hyperlink Field Storage Format" earlier in this chapter.

Additionally, to create the hyperlink display text for a label or command button control, you must set the Caption property. No text displays for an image control, so there is no corresponding display text setting.

You can also create a label that follows a hyperlink by opening the form in Design view, and then using the Hyperlink command (Insert menu). However, this method won't define the display text. To define display text, you must set the label's Caption property.

See Also   For more information on creating a label, image control, or command button that follows a hyperlink, search the Help index for "hyperlinks, creating."

Using Visual Basic Methods and Properties to Work with Hyperlinks

Microsoft Access 97 provides a several methods and properties that you can use to work with hyperlinks in Visual Basic code. The following table summarizes these methods and properties.

Method or property name

Description

Follow method

The Follow method has the same effect as clicking a hyperlink. When you use the Follow method, you don't need to know the address specified by a control's HyperlinkAddress or HyperlinkSubAddress property, or by the Hyperlink field that is bound to a text box control. You only need to know the name of the control that contains the hyperlink.

FollowHyperlink method

Follows a hyperlink specified in code or passed to the method from an unbound text box. For example, you can prompt a user to type a hyperlink address in a dialog box, and then use the FollowHyperlink method to navigate to that address. You can also use the FollowHyperlink method to specify a hyperlink for controls other than labels, image controls, and command buttons, or text boxes bound to Hyperlink fields.

AddToFavorites method

Adds the hyperlink address specified in the referenced control to the Favorites folder.

Hyperlink property

Returns a reference to a hyperlink object in code. You can use the Hyperlink property to access the properties and methods of any control that contains a hyperlink.

HyperlinkAddress property

Specifies or determines the address of a hyperlink for a label, image control, or command button.

HyperlinkSubAddress property

Specifies or determines the location within the Microsoft Office document or object specified by the HyperlinkAddress property.

HyperlinkPart function

Parses the three parts of a hyperlink stored in a table.

See Also   For more information on these methods and properties, search the Help index for the name of the method or property.

Example of Using the Follow Method

This example uses the Follow method to automatically open the Web page specified in a text box bound to a Hyperlink field on a form. Add the following code to the OnCurrent event of a form. Note that you must use the Hyperlink property to return a reference to the object that contains the hyperlink.

Private Sub Form_Current()
   Dim txt As TextBox
   On Error GoTo Error_Form1
   ' Set reference to the txtAddress text box bound to a Hyperlink field.
   Set txt = txtAddress
   ' Follow the hyperlink.
   txt.Hyperlink.Follow
Exit_Form1:
   Exit Sub
Error_Form1:
   MsgBox Err & ": " & Err.Description
   Resume Exit_Form1
End Sub

Example of Using the FollowHyperlink Method

This example uses the FollowHyperlink method to add a hyperlink to a control that doesn't support the HyperlinkAddress or HyperlinkSubAddress properties. Add the following code to the Click event of an unbound object frame named OLEUnbound1 to start a Web browser and open the specified hyperlink address when you click the image.

Private Sub OLEUnbound1_Click()
   Dim strAddress As String
   On Error GoTo Error_OLEUnbound1
   ' Set reference to hyperlink address.
   strAddress = "http://www.microsoft.com"
   ' Follow hyperlink address.
   Application.FollowHyperlink strAddress, , True
Exit_OLEUnbound1:
   Exit Sub
Error_OLEUnbound1:
   MsgBox Err & ": " & Err.Description
   Resume Exit_OLEUnbound1
End Sub

Tip   Controls that don't support the HyperlinkAddress or HyperlinkSubAddress properties don't provide any feedback to the user to indicate that they contain a hyperlink. One way to inform a user that the control contains a hyperlink is to set the control's ControlTipText property so that a text message appears when users rest the pointer on the control.

Note   You can also use the FollowHyperlink method to prompt a user to enter a hyperlink address and then to follow it. For an example of how to do this, search the Help index for "FollowHyperlink method."

© 1996 Microsoft Corporation. All rights reserved.

Next Page


Casa de Bender