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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dialog Box for selecting Path/Filename!!! 3

Status
Not open for further replies.

neemi

Programmer
May 14, 2002
519
GB
Anyone,

I need to be able to pop up a dialog box for the user to be able to select the file that they are after, and have its path and filename entered into a text box on the form. Which in turn is saved in a table.

This path and filename is then to be used to link the required tables from the file to the current database.

I have code which opens up a dialog box but I can only browse down to Folder level.

Does anybody have any code which will enable me to browse down to file level and insert this full path/filename into a textbox.

Please Help!!

Any suggestions much appreciated..

Peace Out!

neemi
 
If you post your code here, I am sure this is just going to be a tweak to get it to the file level vice the folder level... Terry
**************************
* General Disclaimor - Please read *
**************************
Please make sure your post is in the CORRECT forum, has a descriptive title, gives as much detail to the problem as possible, and has examples of expected results. This will enable me and others to help you faster...
 
Private Sub CommandBtn_Click()
Dim RetVal As Long
Dim FileName As String
Dim Path As String
RetVal = FileDialog(hwnd, FileName, "Executable Files(*.EXE)" + String(1, 0) + "*.EXE" + String(1, 0), "", "EXE")
If RetVal > 0 Then txtCtrl = Left(FileName, InStr(FileName, Chr(0)) - 1)
Exit Sub
End Sub




Have a module with following Fns

Option Compare Database
Option Explicit

Private Const BIF_RETURNONLYFSDIRS = 1
Private Const MAX_PATH = 260

Public Const OFN_EXPLORER = &H80000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_NOLONGNAMES = &H40000
Public Const OFN_NOREADONLYRETURN = &H8000
Public Const OFN_PATHMUSTEXIST = &H800
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_OVERWRITEPROMPT = &H2
Public Const OFN_LONGNAMES = &H200000

'Open File structure
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

Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" _
(lpbi As BrowseInfo) As Long

Private Declare Function SHGetPathFromIDList Lib "shell32" _
(ByVal pidList As Long, _
ByVal lpBuffer As String) As Long

Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" _
(ByVal lpString1 As String, ByVal _
lpString2 As String) As Long

Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type


Public Function FileDialog(hwnd As Long, pFileName As String, pFilter As String, pDir As String, pDefExt As String) As Long
Dim op As OPENFILENAME

op.lStructSize = Len(op)
op.hWndOwner = hwnd
op.lpstrFilter = pFilter
op.lpstrFile = String(255, 0)
op.nMaxFile = 255
op.lpstrInitialDir = pDir
op.flags = OFN_EXPLORER Or OFN_HIDEREADONLY Or OFN_LONGNAMES Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
op.lpstrDefExt = pDefExt
' MsgBox op.nFileExtension
' MsgBox op.nFilterIndex
FileDialog = GetOpenFileName(op)
' MsgBox op.nFileExtension
' MsgBox op.nFilterIndex

pFileName = op.lpstrFile
End Function
'======= Code Ends Here

This should surely work out for you
 
Hi Neemi,

See the following function named ap_OpenFile which opens up the file dialog and then returns the filename with full path. Capture it and assign it to ur text box. Let me know it helped u or not on eswarn@lycos.com.


Regards
Eswar
----------------------

Public Function ap_OpenFile(Optional ByVal strFileNameIn _
As String = "", Optional strDialogTitle _
As String = "Name of File to Open")

Dim lngReturn As Long
Dim intLocNull As Integer
Dim strTemp As String
Dim ofnFileInfo As OPENFILENAME
Dim strInitialDir As String
Dim strfilename As String

'-- if a file path passed in with the name,
'-- parse it and split it off.

If InStr(strFileNameIn, &quot;\&quot;) <> 0 Then

strInitialDir = Left(strFileNameIn, InStrRev(strFileNameIn, &quot;\&quot;))
strfilename = Left(Mid$(strFileNameIn, _
InStrRev(strFileNameIn, &quot;\&quot;) + 1) & _
String(256, 0), 256)

Else

strInitialDir = Left(CurrentDb.Name, _
InStrRev(CurrentDb.Name, &quot;\&quot;) - 1)
strfilename = Left(strFileNameIn & String(256, 0), 256)

End If

With ofnFileInfo
.lStructSize = Len(ofnFileInfo)
.lpstrFile = strfilename
.lpstrFileTitle = String(256, 0)
.lpstrInitialDir = strInitialDir
.hwndOwner = Application.hWndAccessApp
.lpstrFilter = &quot;All Files (*.*)&quot; & Chr(0) & &quot;*.*&quot; & Chr(0)
.nFilterIndex = 1
.nMaxFile = Len(strfilename)
.nMaxFileTitle = ofnFileInfo.nMaxFile
.lpstrTitle = strDialogTitle
.flags = cdlOFNHideReadOnly Or _
cdlOFNNoChangeDir
.hInstance = 0
.lpstrCustomFilter = String(255, 0)
.nMaxCustFilter = 255
.lpfnHook = 0
End With

lngReturn = ap_GetOpenFileName(ofnFileInfo)

If lngReturn = 0 Then
strTemp = &quot;&quot;
Else

'-- Trim off any null string
strTemp = Trim(ofnFileInfo.lpstrFile)
intLocNull = InStr(strTemp, Chr(0))

If intLocNull Then
strTemp = Left(strTemp, intLocNull - 1)
End If

End If

ap_OpenFile = strTemp

End Function

Calling syntax: Textbox.text = ap_OpenFile(, &quot;Select File &quot;)
 
Hey All,

Thanks for all your help, all the code worked like a dream, I ended up using Rajeesh's code though but have kept a copy of all.

Hey Rajeesh..

How can I tweak the code that you have given me to bring up the same dialog box but just to folder level..??

Or can anyone else help..

Thanks again.

Neemi
 
Add this function and call this for the Folder Level Option

Public Function GetSelectedPath(hwnd As Long, szTitle As String, ByRef sPath As String) As Boolean
'Opens a Treeview control that displays the directories in a computer
Dim lpIDList As Long
Dim sBuffer As String
Dim tBrowseInfo As BrowseInfo

GetSelectedPath = False
With tBrowseInfo
.hWndOwner = hwnd
.lpszTitle = lstrcat(szTitle, &quot;&quot;)
.ulFlags = BIF_RETURNONLYFSDIRS
End With

lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
If Len(sBuffer) > 0 Then
sPath = sBuffer
GetSelectedPath = True
Else
MsgBox &quot;Invalid file&quot;, vbInformation, &quot;Select file&quot;
End If
End If
End Function
'' Function Ends Here


Calling method:
GetSelectedPath Me.hwnd, &quot;Browse Test&quot;, zPathStr
 
I'm trying to do a similar thing as Neemi. I have a text field on a form called 'ResultsLink' that I need to store the path and filename of an .xls file related to this particular record in Access. I envision that when the user tabs into this field, the Open file dialog box will open. When they find their file, highlight it and click OPEN, the path will store in the field.

I was trying to work with Rajeessh's code above, but am relatively new to Access and not real sure where to set things up.

Under properties for my text box, I have:

Private Sub ResultsLink_Enter()
Dim RetVal As Long
Dim FileName As String
Dim Path As String
RetVal = FileDialog(hwnd, FileName, &quot;All Files(*.***)&quot; + String(1, 0) + &quot;*.***&quot; + String(1, 0), &quot;&quot;, &quot;***&quot;)
If RetVal > 0 Then txtCtrl = Left(FileName, InStr(FileName, Chr(0)) - 1)
Exit Sub
End Sub

For the remaining paragraphs of Rajeessh's code, I created a module in Access called 'OpenFileModule' and saved it there.

When I tab into the ResultsLink field, the Open file dialog box comes up and I can browse to the file that I need. However, when I click OPEN, nothing it returned to the text field. Am I not setting up the module right?

Thanks!
 
hey raj
I tried your code but It gave me error for

option compare database

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top