Microsoft KB Archive/303066

= How to use the Common Dialog API in an Access database =

Article ID: 303066

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Access 97 Standard Edition
 * Microsoft Access 2000 Standard Edition
 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q303066



Moderate: Requires basic macro, coding, and interoperability skills.



INTRODUCTION
This article describes how to use the Common Dialog API to replace the Common Dialog Box functionality that is included only with Microsoft Office 97 Developer Edition, Office 2000 Developer Edition, and 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.  Open the sample database Northwind.mdb. Create a new form. Name the form Form1. Open the new form in Design view. Add a command button to Form1. Set both the Name property and the Caption property to &quot;Command1.&quot; Add a text box to Form1. Set the Name property to &quot;Text1.&quot; Right-click Command1, click Properties, and then click the Event tab. In the On Click event procedure, click [Event Procedure] from the drop-down list, and then click the ellipsis to start the Visual Basic Editor.</li>  Modify the Command1_Click procedure to the following: Private Sub Command1_Click Me!Text1 = LaunchCD(Me) End Sub </li> On the Insert menu, click Module.</li>  Copy and paste the following code sample in the new module. 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 and Save All Modules if you are using Access 97. If you are using Access 2000 or Access 2002, click Compile Northwind, and then close the Code window.</li> On the View menu, click Form View.</li> Click Command1. Click a file from the browse window. Put the path of that file in the Text1 box.</li></ol>

Additional query words: inf

Keywords: kbhowto kbprogramming kbui KB303066

-

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

© Microsoft Corporation. All rights reserved.