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:

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 number 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 control.
#http://www.cajundelights.com/cajun.htm# The Cajun Delights Web page. The text “http://www.cajundelights.com” appears in the field or control 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. The text “http://www.cajundelights.com/ cajun.htm” is displayed in the field or control.
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 control.
#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 control 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. The text “c:\windows\ personal\resume.doc” is displayed in the field or control.
#\\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). The text “\\databases\ samples\northwind.mdb” is displayed in the field or control.
Suppliers Form##Form Suppliers The Suppliers form in the current database. The words “Suppliers Form” are displayed in the field or control.
#c:\windows\personal\1996 Sales.ppt#13 Slide 13 in the 1996 Sales Microsoft PowerPoint presentation located in the \Windows\Personal folder. The text “c:\windows\personal\1996 Sales.ppt” is displayed in the field or control.
#c:\windows\personal\budget.xls#Sheet1!A2 The A2 cell in Sheet1 of the Budget.xls file located in the \Windows\Personal folder. The text “c:\windows\personal\budget.xls” is displayed in the field or control.

You can display the stored hyperlink format by pressing F2 when the insertion point is in the Hyperlink field. You can edit the stored hyperlink in this form as long as you enter number signs (#) in the appropriate locations. You can add or edit the displaytext part of a hyperlink field by right-clicking a hyperlink in a table, pointing to Hyperlink on the shortcut menu, and then typing the display text in the Display Text box.

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.
Protocol Protocol name Description
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 Sets or returns the address of a hyperlink for a label, image control, or command button. The HyperlinkAddress property is equivalent to setting or returning the Address property for the control in Visual Basic; for example, object.HyperlinkAddress is equivalent to object.Hyperlink.Address. You can also set the HyperlinkAddress property in the control’s property sheet.
HyperlinkSubAddress property Sets or returns the location within the Microsoft Office document or object specified by the HyperlinkAddress property. When no HyperlinkAddress property is specified, HyperlinkSubAddress specifies a database object in the current database. The HyperlinkSubAddress property is equivalent to setting or returning the SubAddress property for the control in Visual Basic; for example, object.HyperlinkSubAddress is equivalent to object.Hyperlink.SubAddress. You can also set the HyperlinkSubAddress property in the control’s property sheet.
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.”

The HyperlinkPart Function

The HyperlinkPart function returns information about data stored in a Hyperlink field. The syntax for the HyperlinkPart function is:

object.HyperlinkPart(hyperlink As Variant, part As Integer)

The following table describes the arguments of the HyperlinkPart function.

Argument Description
object Optional. The Application object.
hyperlink Required. A Variant that represents the data stored in a Hyperlink field.
part Optional. An intrinsic constant that represents the information you want returned by the HyperlinkPart function.

You can set the part argument to the following constants.

Constant Value Description
acDisplayedValue 0 (Default) The underlined text displayed in a hyperlink.
acDisplayText 1 The displaytext part of a Hyperlink field.
acAddress 2 The address part of a Hyperlink field.
acSubAddress 3 The subaddress part of a Hyperlink field.

Note   If you use the HyperlinkPart function in an SQL statement or a query, the part argument is required and you can’t set it to the constants listed in the preceding table — you must use the value instead.

You use the HyperlinkPart function to return one of three values stored in a Hyperlink field (displaytext, address, or subaddress) or the displayed value. The value returned depends on the setting of the part argument. If you don’t use the part argument, the HyperlinkPart function returns the value Microsoft Access displays for the hyperlink (which corresponds to the acDisplayedValue setting for the part argument).

When a value is provided in the displaytext part of a Hyperlink field, the value displayed by Microsoft Access will be the same as the displaytext setting. When there’s no value in the displaytext part of a Hyperlink field, Microsoft Access displays the value of the address or subaddress part of the Hyperlink field, depending on which value is first present in the field.

The following table shows the values returned by the HyperlinkPart function for data stored in a Hyperlink field.

Hyperlink field data HyperlinkPart function returned values
#http://www.microsoft.com/# acDisplayedValue: http://www.microsoft.com/acDisplayText: No value returned.acAddress: http://www.microsoft.com/acSubAddress: No value returned.
Microsoft#http://www.microsoft.com/# acDisplayedValue: MicrosoftacDisplayText: MicrosoftacAddress: http://www.microsoft.com/acSubAddress: No value returned.
Customers##Form Customers acDisplayedValue: CustomersacDisplayText: CustomersacAddress: No value returned.acSubAddress: Form Customers
##Form Customers acDisplayedValue: Form CustomersacDisplayText: No value returned.acAddress: No value returned.acSubAddress: Form Customers

The following example uses all four of the part argument constants to display information returned by the HyperlinkPart function for each record in a table containing a Hyperlink field. To try this example, paste the DisplayHyperlinkParts procedure into the Declarations section of a module. You can call the DisplayHyperlinkParts procedure from the Debug window, passing to it the name of a table that contains hyperlinks and the name of the field that contains Hyperlink data, as shown in the following example.

DisplayHyperlinkParts "MyHyperlinkTableName", "MyHyperlinkFieldName"

Sub DisplayHyperlinkParts(strTable As String, strField As String)
	Dim dbs As Database, rst As Recordset
	Dim strMsg As String

	Set dbs = CurrentDb
	Set rst = dbs.OpenRecordset(strTable)

	While Not rst.EOF		' For each record in table.
		strMsg = "DisplayValue = " & HyperlinkPart(rst(strField), acDisplayedValue) _
			& vbCrLf & "DisplayText = " & HyperlinkPart(rst(strField), acDisplayText) _
			& vbCrLf & "Address = " & HyperlinkPart(rst(strField), acAddress) _
			& vbCrLf & "SubAddress = " & HyperlinkPart(rst(strField), acSubAddress)
		' Show parts returned by HyperlinkPart function.
		MsgBox strMsg
		rst.MoveNext
	Wend
End Sub

When you use the HyperlinkPart function in a query, the part argument is required. For example, the following SQL statement uses the HyperlinkPart function to return information about data stored as a Hyperlink data type in the URL field of the Links table:

SELECT Links.URL, HyperlinkPart([URL],0)
AS Display, HyperlinkPart([URL],1)
AS Name, HyperlinkPart([URL],2)
AS Addr, HyperlinkPart([URL],3) AS SubAddr
FROM Links;

See Also   For another example of using the HyperlinkPart function, see “Displaying a Document in the WebBrowser Control by Using a Hyperlink Stored in a Table” later in this chapter.