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:
SiteID SiteURL
----------------------------------------
1 ftp.microsoft.com
2 www.microsoft.com/kb.htm
3 http://www.microsoft.com/devonly
- In the Database window, click Forms, and then click New.
- In the New Form dialog box, click AutoForm: Columnar and select WebSites as the table where the object's data comes from. Click OK.
- 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
- 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
- On the View menu, click Form View.
- 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.
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
- 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.
NOTES
- The Windows API ShellExecute() function differs from the Visual Basic Shell() function in that you can pass the ShellExecute() function the name of a document, and it will start the associated application, and then pass the file name to the application. You can also specify the working folder for the application.
- The return value for the StartDoc() function is the same as for the Shell() function. It is the Windows instance value of the application that was started.