To open "Open Dialog" box from Access

I need a simple task - in VBA code to open a regular Windows "Open" dialog box, lets say in "C:\Temp".

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dim oCDLG As Object
Set oCDLG = CreateObject("MSComDlg.CommonDialog")
oCDLG.InitDir = "C:\Temp"


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
you have to have the common dialog box object in your system folder first, to do this.
i guess file name was commdlg32.ocx
then you click tool>references and put ticker on this object.. if you are missing that file than search web to download and insert it to your system32 in winnt or system folder in win 95/98
after this process common dialog box object appears on your toolbox..
place this object on a form also a button to form to open this dialog box once clicked..
then you will qualify to write a code in vba

this is name of my common dialog box object: ActiveXCtl5

Form_MAIN.ActiveXCtl5.Filter = "All files|*.*"
Form_MAIN.ActiveXCtl5.FileName = ""
If Form_MAIN.ActiveXCtl5.FileName <> "" Then
    strSourceFile = Form_MAIN.ActiveXCtl5.FileName
    Exit Sub
End If
for a specific directory add an additional two line code similiar to sanopal's..
Ryan ChongSoftware Team LeadCommented:
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

misha1Author Commented:

 Exectly what I need !
misha1Author Commented:

 Just the follow up question,
 This code from samopal worked good:
 Dim oCDLG As Object
 Set oCDLG = CreateObject("MSComDlg.CommonDialog")
 oCDLG.InitDir = "C:\Temp"

 It opened the dialog box in the folder that I needed.
 And I see files that I need - BUT when selectiong the file
 and cliking Open button files don't open. I've tried it
 with many different files and folders - the same result.
 Also double-clicking on the files in this dialog box doesn't help.
 I've tried dubuynu's solution -  the same result.

 That's strange.
 Help me solve it and I will post another question just
 with points for the one who helps me.

Commondialog control only helps you to select a file name. You can get it using :

debug.print oCDLG.FileName

If you whould like to open selected file, use ShellExecute function :

Declare Function ShellExecute Lib "shell32.dll" Alias _
   "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
   As String, ByVal lpFile As String, ByVal lpParameters _
   As String, ByVal lpDirectory As String, ByVal nShowCmd _
   As Long) As Long

Global Const SW_SHOWNORMAL = 1

Sub ShellExecuteExample()

   StartDoc = ShellExecute(ME.hwnd, "open", oCDLG.FileName, "", _
      "C:\", SW_SHOWNORMAL)
End Sub

misha1Author Commented:
  Thanks, will try it.
misha1Author Commented:
Ok it worked good.
 Will post a question - "Just for samopal" with 25
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.