469,621 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

how to use basic Common Dialog Box

I need to use the Common Dialog box, but I'm having trouble getting started.
Bear with me as this is new ground for me...

First, I get an error trying to declare these functions -- compile error:
User-defined type not defined - am I missing a reference? which one?

Private Declare Function GetOpenFileName _
Lib "COMDLG32.DLL" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName _
Lib "COMDLG32.DLL" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetActiveWindow _
Lib "user32" () As Long
Then, how do I use this code (below)? Do I simply create a
cmdGetFileFromAPI_Click command button on my form?
Do I need anything else in order to launch the Common Dialog Box?

Sub GetFileFromAPI()
Dim OFN As OPENFILENAME
With OFN
.lStructSize = Len(OFN) ' Size of structure.
.nMaxFile = 260 ' Size of buffer.
' Create buffer.
.lpstrFile = String(.nMaxFile - 1, 0)
Ret = GetOpenFileName(OFN) ' Call function.
If Ret <> 0 Then ' Non-zero is success.
' Find first null char.
n = InStr(.lpstrFile, vbNullChar)
' Return what's before it.
MsgBox Left(.lpstrFile, n - 1)
End If
End With
End Sub
Nov 12 '05 #1
3 14272
From any code window, choose References from the Tools menu.
Check the box beside the MS Common Dialog library.
If it is not there, it may not be correctly registered.

Quite honestly, it's not worth messing with that thing. It's buggy, and
prone to versioning problems. Better to just ignore it, and use the API
calls instead. This link explains how to do that for the File Open/Save
dialog:
http://www.mvps.org/access/api/api0001.htm

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"deko" <dj****@hotmail.com> wrote in message
news:vh******************@newssvr29.news.prodigy.c om...
I need to use the Common Dialog box, but I'm having trouble getting started. Bear with me as this is new ground for me...

First, I get an error trying to declare these functions -- compile error:
User-defined type not defined - am I missing a reference? which one?

Private Declare Function GetOpenFileName _
Lib "COMDLG32.DLL" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName _
Lib "COMDLG32.DLL" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetActiveWindow _
Lib "user32" () As Long
Then, how do I use this code (below)? Do I simply create a
cmdGetFileFromAPI_Click command button on my form?
Do I need anything else in order to launch the Common Dialog Box?

Sub GetFileFromAPI()
Dim OFN As OPENFILENAME
With OFN
.lStructSize = Len(OFN) ' Size of structure.
.nMaxFile = 260 ' Size of buffer.
' Create buffer.
.lpstrFile = String(.nMaxFile - 1, 0)
Ret = GetOpenFileName(OFN) ' Call function.
If Ret <> 0 Then ' Non-zero is success.
' Find first null char.
n = InStr(.lpstrFile, vbNullChar)
' Return what's before it.
MsgBox Left(.lpstrFile, n - 1)
End If
End With
End Sub

Nov 12 '05 #2
I found this code, which seems to be working.

As Matt Drudge would say, developing...
Private Sub cmdTest_Click()
Dim fDialog As Office.FileDialog
Dim varFile As Variant

'Clear listbox contents.
Me.FileList.RowSource = ""

'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Allow user to make multiple selections in dialog box
.AllowMultiSelect = True

'Set the title of the dialog box.
.Title = "Please select one or more files"

'Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Access Databases", "*.MDB"
.Filters.Add "Access Projects", "*.ADP"
.Filters.Add "All Files", "*.*"

'Show the dialog box. If the .Show method returns True, the
'user picked at least one file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
'Loop through each file selected and add it to our list box.
For Each varFile In .SelectedItems
Me.FileList.AddItem varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
Nov 12 '05 #3
The File Dialog object is not very useful either.

It requires a reference to the Office library, doesn't work with older
versions of Access, doesn't work with runtime/mde files, and doesn't work at
all if you need the Save As functionality (which it offers but doesn't
support).

Better to use the API call as per:
http://www.mvps.org/access/api/api0001.htm

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"deko" <dj****@hotmail.com> wrote in message
:PC*******************@newssvr29.news.prodigy.com. ..
I found this code, which seems to be working.

As Matt Drudge would say, developing...
Private Sub cmdTest_Click()
Dim fDialog As Office.FileDialog
Dim varFile As Variant

'Clear listbox contents.
Me.FileList.RowSource = ""

'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Allow user to make multiple selections in dialog box
.AllowMultiSelect = True

'Set the title of the dialog box.
.Title = "Please select one or more files"

'Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Access Databases", "*.MDB"
.Filters.Add "Access Projects", "*.ADP"
.Filters.Add "All Files", "*.*"

'Show the dialog box. If the .Show method returns True, the
'user picked at least one file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
'Loop through each file selected and add it to our list box.
For Each varFile In .SelectedItems
Me.FileList.AddItem varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by CaribSoft | last post: by
23 posts views Thread by George | last post: by
4 posts views Thread by Adam | last post: by
reply views Thread by Bo Gusman | last post: by
6 posts views Thread by Steve Barnett | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.