views:

380

answers:

4

I want to show a dialog where the user can pick a file, click OK, and then the path to the file will be saved in the database.

I have just one problem, I can't figure out how tho show the dialog window. Do you?

+1  A: 

You can use the WinAPI for that. Import

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

You also have to import the OPENFILENAME structure.

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

Then you fill out the structure and call GetOpenFileName.

 Dim of As OPENFILENAME

of.lStructSize = Len(of)
of.hwndOwner = Access.hWndAccessApp
of.hInstance = vbNull
of.lpstrFilter = m_strFilter ' *.doc for example
of.nFilterIndex = 1
of.lpstrFile = String(257, 0)
of.nMaxFile = Len(of.lpstrFile) - 1
of.lpstrFileTitle = of.lpstrFile
of.nMaxFileTitle = of.nMaxFile
of.lpstrInitialDir = m_strDirectory ' Folder to start
of.lpstrTitle = m_strTitle ' Title of dialog window
of.Flags = 0

If GetOpenFileName(of) <> 0 Then
    filename = VBString(of.lpstrFile)
end if

Where VBString is a helper function to convert a null-terminated string.

Private Function VBString(str As String) As String
   Dim pos As Integer
   pos = InStr(1, str, Chr(0), vbTextCompare)
   VBString = Left(str, pos - 1)
End Function
dwo
Hi! Thanks a lot. I have no clue how this code works. I guess its VBA? It would be very helpful if it was possible to copy paste the code right away, because I don't understand your answer :\
Johan
@Johan, yes, it is VBA. It uses the Windows API, which works just fine in VBA but is really geared toward C and C++ programmers. That's why it doesn't look like any other VBA you've seen. David W. Fenton is right in principle about preferring the Windows API over the Office-specific version, but if this code is way beyond your grasp you may want to stick with what you're familiar with. Believe me, you don't want to get into the habit of just copying and pasting code you don't understand. If it breaks after you upgrade your Office version, that's a relatively easy thing to fix.
John M Gant
+1  A: 

Similar to @dwo's answer: How to display the Common 'File-Open' Dialog to Choose a File

Create a new module and paste the code in your new module.
In the above link there is also an example on how to use it.

Nick D
A: 

Do not forget the fileDialog object, easy, allows multiple selection, fileOpen, folder selection, etc, to be used this way:

Dim m_fileList As FileDialog, _
    i as long

'my choice here: pick up multiple files. Other options are available'
Set m_fileList = Application.FileDialog(msoFileDialogFilePicker)
m_fileList.AllowMultiSelect = True
m_fileList.InitialFileName = myDefaultFolder
m_fileList.InitialView = msoFileDialogViewDetails
m_fileList.Title = yourTitle
m_fileList.InitialFileName = myDefaultFileName

'we can add a file extension filter serie'
'm_fileList.filters(i) = ...'

If m_fileList.Show = -1 And m_fileList.SelectedItems.Count > 0 Then
    For i = 1 To m_fileList.SelectedItems.Count
        debug.print m_fileList.selectedItems(i).
    Next i
End If
Philippe Grondier
See my comment to Remou's recommendation of the same thing.
David-W-Fenton
+1 to bring back to 0. This approach does have its drawbacks, as David W. Fenton notes, but it's much simpler to understand, which is a worthwhile consideration since the OP apparently doesn't have the VBA (or VB6) background to understand Window API functions (no offense to the OP, just an observation).
John M Gant
@David I did not find neither Remou's recommendations nor your comments.
Philippe Grondier
anyway this code has been working for the last 6 years and on 3 different OS's, meaning that it can be considered as quite reliable, can't it?
Philippe Grondier
A: 

This code did it.

Johan
By using that approach, you've built a vulnerability to failure into your app the next time you upgrade to a new Office version. See my comment to @Remou's post. Also, two people recommended this to you so you should accept one of them as the answer.
David-W-Fenton
Well, using the Windows-API is pretty invulnerable against Office-updates. My experience is: the less references in your project - the better it works over time.
dwo