How to use the Common Dialog API in a database in Access 2003 or Access 2007
Artikel
Gëllt fir:
Microsoft Office Access 2007, Microsoft Office Access 2003
Original KB number: 888695
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. The functionality is included only in the Microsoft Office 2000 Developer Edition or in the Microsoft Office XP Developer Edition.
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.
Steps to replace the Common Dialog functionality
Microsoft Office Access 2003
In Access, open the sample database that is named Northwind.mdb.
Notiz
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, typeText1, 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.
Click Caption, type Text1, and then close the Properties dialog box.
Add a command button to Form1, right-click the command button, click Properties, click Name, type Command1, click Caption, and then type Command1.
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.
Modify the code in the Command1_Click procedure to the following:
On the Insert menu, click Module, and then insert the following code into Module1:
VB
PrivateDeclareFunction GetOpenFileName Lib"comdlg32.dll"Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) AsLongPrivate Type OPENFILENAME
lStructSize AsLong
hwndOwner AsLong
hInstance AsLong
lpstrFilter AsString
lpstrCustomFilter AsString
nMaxCustFilter AsLong
nFilterIndex AsLong
lpstrFile AsString
nMaxFile AsLong
lpstrFileTitle AsString
nMaxFileTitle AsLong
lpstrInitialDir AsString
lpstrTitle AsString
flags AsLong
nFileOffset AsInteger
nFileExtension AsInteger
lpstrDefExt AsString
lCustData AsLong
lpfnHook AsLong
lpTemplateName AsStringEnd Type
Function LaunchCD(strform As Form) AsStringDim OpenFile As OPENFILENAME
Dim lReturn AsLongDim sFilter AsString
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = strform.hwnd
sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0) & _
"JPEG Files (*.JPG)" & Chr(0) & "*.JPG" & 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 = "C:\"
OpenFile.lpstrTitle = "Select a file using the Common Dialog DLL"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0Then
MsgBox "A file was not selected!", vbInformation, _
"Select a file using the Common Dialog DLL"Else
LaunchCD = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
EndIfEndFunction
On the Debug menu, click Compile Northwind, and then close the Visual Basic Editor.
On the View menu, click Form View.
Click Command1, and then click a file in the window that opens.
The path of the file appears in the Text1 text box.
Microsoft Office Access 2007
In Access 2007, open the sample database that is named Northwind.accdb.
On the Create tab, click Form in the Forms group.
On the Format tab, click the down arrow below View, and then click Design View.
Add a text box to Form1, right-click the text box, and then click Properties.
Click the All tab, click Name, and then type Text1.
Right-click the label control that is associated with the Text1 text box, click Properties, and then click the All tab.
Click Caption, and then type Text1.
Add a command button to Form1, right-click the command button, click Properties, click Name, type Command1, click Caption, and then type Command1.
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.
Modify the code in the Command1_Click procedure to resemble the following code example.