Microsoft KB Archive/148632

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

Article ID: 148632

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q148632



SUMMARY
Advanced: Requires expert coding, interoperability, and multi-user skills.

This article shows you 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 URL (Uniform Resource Locator) to the API function.

NOTE: Microsoft Access 97 has this functionality built in.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access for Windows 95" manual.



MORE INFORMATION
To use the Windows API ShellExecute, you must first declare the function in a standard Visual Basic for Applications module. After you have declared the function, you can use the function by following one of the two examples described later in this article.

Declaring the Windows API ShellExecute
 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 Connect to the World Wide Web
NOTE: This functionality is built in to Microsoft Access 97.   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 switch the table to Datasheet view. Enter 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> Use the AutoForm: Columnar Wizard to create a new form based on the WebSites table.</li> Switch the form to Design view and add the following command button:

Command button:

Name: cmdConnect

Caption: Connect to Web

</li>  Set the cmdConnect button's OnClick property 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> Switch the form to 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> On the View menu, click Debug Window.</li> In the Debug 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>