Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display Open File Dialog and save result 2

Status
Not open for further replies.

FrankPV

Technical User
Oct 11, 2000
20
US
Hello:

I am working on a form where I would like to click on a command button and display the open file box (like the one that displays when you click the open folder on the toolbar). The user will have the option of choosing a folder, subfolder, different drive, etc., The resulting path and file name would be saved in a field in the associated table. The file path is associated with an image that displays in another control on the form. Any help would be appreciated.

Thank you.
 
Below is a function that I use as a module that makes an API call to open a file. Here is in example on how you would use it. I am going to Assume the the button is on the same form as a text box to display the file path and name. then your code would look like this

Me.TextBox_Name = ap_FileOpen()

use this code below in a module

Option Compare Database
Option Explicit

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

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

Public Function ap_FileOpen(Optional strTitle As String = "Open File", _
Optional strFileName As String = "", _
Optional strFilter As String = "") As String

Dim OpenFile As OPENFILENAME
Dim lngReturn As Long

If Len(strFileName) = 0 Then
strFileName = String(255, 0)
End If

'Note: if you are looking for certain files put the extension of that file where you see the .txt and change Text Files to The appropriate file type Name
If Len(strFilter) = 0 Then
strFilter = "Text Files (*.txt)" & Chr(0) & _
"*.txt" & Chr(0)
End If

OpenFile.lStructSize = Len(OpenFile)

OpenFile.lpstrFilter = strFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = strFileName + _
Space(255 - Len(strFileName))
OpenFile.nMaxFile = 255
OpenFile.lpstrFileTitle = strFileName
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = CurrentProject.Path
OpenFile.lpstrTitle = strTitle
OpenFile.flags = 0

ap_GetOpenFileName OpenFile

ap_FileOpen = Left(OpenFile.lpstrFile, _
InStr(OpenFile.lpstrFile, Chr$(0)) - 1)

End Function

 
Thanks. It works well. I wonder if I could have the code point to a specific default directory instead of the directory of the database. Either that or have the program point to the directory last used by the user in the current session.

Thanks again.

 
Change This
OpenFile.lpstrInitialDir = CurrentProject.Path
to this
OpenFile.lpstrInitialDir = "C:\What_Ever_Path"

HTH
 
ToeShot - I can only hope you'll get this message as I'm stuck!! I copied the code as specified into a module, then tried to get a macro using the RunCode function to point to ap_FileOpen(), but it didn't work.

What I'm wanting is for you to be able to hit a 'Browse...' button, the open file dialogue to open, you to be able to select a photo file, which is then displayed as a linked object in an OLE Object field, and also for the full path to be displayed in a text field (which is stored in the database itself)

Hope you can help me! Step-by-step instructions would be appreciated - my grasp of VBA is tenuous at best!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top