Connecting Tech Pros Worldwide Help | Site Map

how to use basic Common Dialog Box

  #1  
Old November 12th, 2005, 05:23 PM
deko
Guest
 
Posts: n/a
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


  #2  
Old November 12th, 2005, 05:23 PM
Allen Browne
Guest
 
Posts: n/a

re: how to use basic Common Dialog Box


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" <dje422@hotmail.com> wrote in message
news:vhXzb.32848$4W6.2586@newssvr29.news.prodigy.c om...[color=blue]
> I need to use the Common Dialog box, but I'm having trouble getting[/color]
started.[color=blue]
> 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
>
>[/color]


  #3  
Old November 12th, 2005, 05:23 PM
deko
Guest
 
Posts: n/a

re: how to use basic Common Dialog Box


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


  #4  
Old November 12th, 2005, 05:23 PM
Allen Browne
Guest
 
Posts: n/a

re: how to use basic Common Dialog Box


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" <dje422@hotmail.com> wrote in message
:PCXzb.32851$e37.29303@newssvr29.news.prodigy.com. ..[color=blue]
> 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
>
>[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How will we use .Net in future versions of Access Lyle Fairfield answers 5 February 20th, 2006 03:35 PM
Showing an picture in a dialog box (WinAPI) Joel ELISE answers 6 November 14th, 2005 10:23 PM
Common dialog control returns wrong filename !! S.W. Rasmussen answers 3 July 17th, 2005 10:16 PM
common dialog! John Lauwers answers 5 July 17th, 2005 09:31 PM