Microsoft KB Archive/888695

= How to use the Common Dialog API in a database in Access 2003 or Access 2007 =

Article ID: 888695

Article Last Modified on 6/4/2007

-

APPLIES TO


 * Microsoft Office Access 2007
 * Microsoft Office Access 2003

-





INTRODUCTION
This article describes how to use the Common Dialog API in Microsoft Office Access 2003 or in Microsoft Office Access 2007 to replace the Common Dialog Box functionality that is included only in the Microsoft Office 2000 Developer Edition or in the Microsoft Office XP Developer Edition.



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.

Microsoft Office Access 2003
 In Access, open the sample database that is named Northwind.mdb.

Note The Northwind.mdb database for Access 2003 is typically located in the C:\Program Files\Microsoft Office\OFFICE11\Samples folder. Under Objects in the Northwind Database window, click Forms. In the Database window toolbar, click New. In the New Form dialog box, click Design View, and then click OK. Add a text box to Form1, right-click the text box, and then click Properties. Click the All tab, click Name, type Text1, and then close the Properties dialog box. Right-click the label control that is associated with the Text1 text box, click Properties, and then click the All tab.</li> Click Caption, type Text1, and then close the Properties dialog box.</li> Add a command button to Form1, right-click the command button, click Properties, click Name, type Command1, click Caption, and then type Command1 .</li> Click the Event tab, click [Event Procedure] in the On Click list, and then click the ellipsis button to start the Microsoft Visual Basic Editor.</li>  Modify the code in the Command1_Click procedure to the following: Private Sub Command1_Click Me!Text1 = LaunchCD(Me) End Sub </li>  On the Insert menu, click Module, and then insert the following code into Module1: Private Declare Function GetOpenFileName Lib &quot;comdlg32.dll&quot; Alias _ &quot;GetOpenFileNameA&quot; (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type

Function LaunchCD(strform As Form) As String Dim OpenFile As OPENFILENAME Dim lReturn As Long Dim sFilter As String OpenFile.lStructSize = Len(OpenFile) OpenFile.hwndOwner = strform.hwnd sFilter = &quot;All Files (*.*)&quot; & Chr(0) & &quot;*.*&quot; & Chr(0) & _ &quot;JPEG Files (*.JPG)&quot; & Chr(0) & &quot;*.JPG&quot; & Chr(0) OpenFile.lpstrFilter = sFilter OpenFile.nFilterIndex = 1 OpenFile.lpstrFile = String(257, 0) OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1 OpenFile.lpstrFileTitle = OpenFile.lpstrFile OpenFile.nMaxFileTitle = OpenFile.nMaxFile OpenFile.lpstrInitialDir = &quot;C:\&quot; OpenFile.lpstrTitle = &quot;Select a file using the Common Dialog DLL&quot; OpenFile.flags = 0 lReturn = GetOpenFileName(OpenFile) If lReturn = 0 Then MsgBox &quot;A file was not selected!&quot;, vbInformation, _ &quot;Select a file using the Common Dialog DLL&quot; Else LaunchCD = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1)) End If End Function </li> On the Debug menu, click Compile Northwind, and then close the Visual Basic Editor.</li> On the View menu, click Form View.</li> Click Command1, and then click a file in the window that opens.

The path of the file appears in the Text1 text box.</li></ol>

Microsoft Office Access 2007
<ol> In Access 2007, open the sample database that is named Northwind.accdb.</li> On the Create tab, click Form in the Forms group.</li> On the Format tab, click the down arrow below View, and then click Design View.</li> Add a text box to Form1, right-click the text box, and then click Properties.</li> Click the All tab, click Name, and then type Text1 .</li> Right-click the label control that is associated with the Text1 text box, click Properties, and then click the All tab.</li> Click Caption, and then type Text1 .</li> <li>Add a command button to Form1, right-click the command button, click Properties, click Name, type Command1, click Caption, and then type Command1 .</li> <li>Click the Event tab, click [Event Procedure] in the On Click list, and then click the ellipsis button (...) to start the Microsoft Visual Basic Editor.</li> <li> Modify the code in the Command1_Click procedure to resemble the following code example. Private Sub Command1_Click Me!Text1 = LaunchCD(Me) End Sub </li> <li> On the Insert menu, click Module, and then insert code that resembles the following code example into Module1. Private Declare Function GetOpenFileName Lib &quot;comdlg32.dll&quot; Alias _ &quot;GetOpenFileNameA&quot; (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type

Function LaunchCD(strform As Form) As String Dim OpenFile As OPENFILENAME Dim lReturn As Long Dim sFilter As String OpenFile.lStructSize = Len(OpenFile) OpenFile.hwndOwner = strform.hwnd sFilter = &quot;All Files (*.*)&quot; & Chr(0) & &quot;*.*&quot; & Chr(0) & _ &quot;JPEG Files (*.JPG)&quot; & Chr(0) & &quot;*.JPG&quot; & Chr(0) OpenFile.lpstrFilter = sFilter OpenFile.nFilterIndex = 1 OpenFile.lpstrFile = String(257, 0) OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1 OpenFile.lpstrFileTitle = OpenFile.lpstrFile OpenFile.nMaxFileTitle = OpenFile.nMaxFile OpenFile.lpstrInitialDir = &quot;C:\&quot; OpenFile.lpstrTitle = &quot;Select a file using the Common Dialog DLL&quot; OpenFile.flags = 0 lReturn = GetOpenFileName(OpenFile) If lReturn = 0 Then MsgBox &quot;A file was not selected!&quot;, vbInformation, _ &quot;Select a file using the Common Dialog DLL&quot; Else LaunchCD = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1)) End If End Function </li> <li>On the Debug menu, click Compile Northwind, and then close the Visual Basic Editor.</li> <li>On the Format tab, click the down arrow below View, and then click Form View.</li> <li>Click Command1, and then click a file in the window that opens.

The path of the file appears in the Text1 box.</li></ol>

Additional query words: ACC2003 ACC2007

Keywords: kbsampledatabase kbprogramming kbautomation kbexpertiseinter kbhowto KB888695

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.