Microsoft KB Archive/210096

= ACC2000: Start Files or Hyperlinks with Windows API ShellExecute =

Article ID: 210096

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210096



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access database (.mdb).



SUMMARY
This article describes how to use the Windows 32-bit application programming interface (API) ShellExecute function to start an application associated with a given file extension without having to know the name of the associated application. For example, you can start Microsoft Paint by passing the file name Bubbles.bmp to the ShellExecute function. Or, you can connect to the World Wide Web (by using a Web browser installed on your computer) by passing a hyperlink or Uniform Resource Locator (URL) to the API function.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To use the Windows API ShellExecute function, you must first declare the function in a standard Visual Basic for Applications module. After you declare the function, you can use the function by following one of the two examples described later in this article.

Declaring the Windows API ShellExecute
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

 Start Microsoft Access and open the sample database Northwind.mdb and create a new module named Module1.  Add the following code to the Declarations section: Option Explicit

Declare Function ShellExecute Lib "shell32.dll" Alias _ "ShellExecuteA" (ByVal Hwnd As Long, ByVal lpOperation _  As String, ByVal lpFile As String, ByVal lpParameters _   As String, ByVal lpDirectory As String, ByVal nShowCmd _   As Long) As Long

Global Const SW_SHOWNORMAL = 1  Close and save Module1.

To test the ShellExecute function, use one of the following examples.

Example 1: How to Open a Location on the World Wide Web
NOTE: This functionality is built into Microsoft Access 2000.  Create a new table with the following structure:

Table: WebSites

FieldName: SiteID

DataType : AutoNumber

Indexed: Yes (No Duplicates)

FieldName: SiteURL

DataType : Text

  Save the table as WebSites and open the table in Datasheet view. Type the following three records: <pre class="fixed_text">  SiteID   SiteURL 1     ftp.microsoft.com 2     www.microsoft.com/kb.htm 3     http://www.microsoft.com/devonly </li> In the Database window, click Forms, and then click New.</li> In the New Form dialog box, click AutoForm: Columnar and select WebSites as the table where the object's data comes from. Click OK.</li> On the View menu, click Design View. Add the following command button to the Detail section of the form:

Command button

Name: cmdConnect

Caption: Connect to Web

</li>  Set the On Click property of the cmdConnect button to the following event procedure: Private Sub cmdConnect_Click On Error GoTo cmdConnect_Click_Error

Dim StartDoc As Long If Not IsNull(Me!SiteURL) Then StartDoc = ShellExecute(Me.Hwnd, "open", Me!SiteURL, _         "", "C:\", SW_SHOWNORMAL) End If

Exit Sub

cmdConnect_Click_Error: MsgBox "Error: " & Err & " " & Error Exit Sub End Sub </li> On the View menu, click Form View.</li> Click the Connect To Web button. Note that your Web browser is started automatically and displays the Web site for the URL listed in the current record.</li></ol>

Example 2: How to Open a File in Its Associated Application
  Open Module1 and create the following procedure: Function StartDoc (DocName As String) On Error GoTo StartDoc_Error

StartDoc = ShellExecute(Application.hWndAccessApp, "Open", DocName, _     "", "C:\", SW_SHOWNORMAL) Exit Function

StartDoc_Error: MsgBox "Error: " & Err & " " & Error Exit Function End Function </li> In the Immediate window, type the following line, and then press ENTER:

StartDoc "Bubbles.bmp"

Note that the function starts Microsoft Paint, which loads the Bubbles.bmp file.</li></ol>