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

Select A Directory And Place Its Path Into A Field 1

Status
Not open for further replies.

StuartBombay

Programmer
Feb 11, 2008
56
US
I'm using access 2003, I would like my users to be able to, from a form, browse to a file, then by selecting that file store the file's full path in a bound field of the underlying table.
Starting with, what is the best method for browsing to the directory in the first place? Once I've answered that I can do something along the lines of "on change make the text box equal to the selected item", but I'm having trouble getting started with the directory search. I would like it to have the look and feel of other windows application's 'OPEN' dialogs.

Thanks.
 
How are ya StuartBombay . . .

The API [blue]GetFileNameFromBrowse[/blue] should do. But first whats the initial drive, folder, and file extension you'll be searching for (if any)?

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
1. On your form create an unbound text field to hold the file name or file path

2. Create a button with the following in the OnClick event

Code:
Dim retFile As String, dlg As Variant, s As String

Set dlg = Application.FileDialog(msoFileDialogFilePicker)

With dlg
'The title can be anything you want
    .Title = "Select a data file"
    .AllowMultiSelect = False
' Change the line below to reflect what type of files you want the user to be able to see, *.* will show all file types
    .Filters.Add "Your Db Application Data Files", "*.mdb"
' The initial file name can be anything you choose, the example below will start you at the root of your "C" drive
    .InitialFileName = "c:\"
If .Show = -1 Then s = .SelectedItems(1)
End With

If s <> "" Then
'*** Use the line below is if you only want the File Name ***
'retFile = Right(s, Len(s) - InStrRev(s, "\"))

'*** Use the line below to return the full path of the file ***
retFile = s

Me.YourFieldName.Value = retFile

End If

Of course you can remove the comment lines I have inserted to make the code look a bit cleaner.

Hope this helps !!!
 
Hey TheAceMan1 and BradCollins:
Thanks for the quick and very helpful replies. AceMan, to answer your question, ideally I would like the path to default to whatever folder the access database is in. I don't know if there is a '\\' or a '..' sort of way of doing that. If there is then the database and the files (jpg's in this case) could move together and be placed anywhere and the user can place a shortcut to the database wherever they want it.
If there's a better way, I'm open to it. But I will be building a quick shell then sending the project off to another department for someone else to build on, so I need to make the explanation to them short and simple.
Thanks again!
 
StuartBombay . . .

Roger That! However I'm at work (several emergencies going on) and will pick this up as soon as I get home . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Replace this:
.InitialFileName = "c:\"
with this:
.InitialFileName = CurrentProject.Path

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
StuartBombay . . .

Don't know if this is resolved, but here's the API method.
[ol][li]Open a new module in the modules window.[/li]
[li]Click the save button and name the module [blue]modBrowseFiles[/blue].[/li]
[li]Delete everything in the module then copy/paste the following code:
Code:
[blue]Option Compare Database
Option Explicit

Private Const VER_PLATFORM_WIN32_NT = 2

Private Type OSVERSIONINFO
   dwOSVersionInfoSize As Long
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformId As Long
   szCSDVersion As String * 128
End Type

Private Declare Function GetVersionEx Lib "kernel32" _
                         Alias "GetVersionExA" _
                        (ByRef lpVersionInformation As OSVERSIONINFO) As Long

Private Declare Function GetFileNameFromBrowseW Lib "shell32" _
                         Alias "#63" _
                        (ByVal hwndOwner As Long, _
                         ByVal lpstrFile As Long, _
                         ByVal nMaxFile As Long, _
                         ByVal lpstrInitialDir As Long, _
                         ByVal lpstrDefExt As Long, _
                         ByVal lpstrFilter As Long, _
                         ByVal lpstrTitle As Long) As Long

Private Declare Function GetFileNameFromBrowseA Lib "shell32" _
                         Alias "#63" _
                        (ByVal hwndOwner As Long, _
                         ByVal lpstrFile As String, _
                         ByVal nMaxFile As Long, _
                         ByVal lpstrInitialDir As String, _
                         ByVal lpstrDefExt As String, _
                         ByVal lpstrFilter As String, _
                         ByVal lpstrTitle As String) As Long

Public Function IsWinNT() As Boolean
   Dim myOS As OSVERSIONINFO
   
   myOS.dwOSVersionInfoSize = Len(myOS)
   GetVersionEx myOS
   IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)

End Function

Public Function BrowseFiles(frm As Form)
   Dim sSave As String, idx As Integer
   
   sSave = Space(255)
   
   [green]'Arguement definitions:
      'hwndOwner
      'Initialize edit control & return path to file (Buffer)
      'Size in characters of above buffer
      'Initial Dir
      'Default extension
      'Filter Strings
      'Title (Title Bar)[/green]
   
   If IsWinNT Then
      [green]'unicode version of the function[/green]
      GetFileNameFromBrowseW _
         frm.hwnd, _
         StrPtr(sSave), _
         255, _
         StrPtr(CurrentProject.Path), _
         StrPtr("jpg"), _
         StrPtr("JPG files (*.jpg)" + Chr$(0) + "*.jpg" + Chr$(0) + _
                "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), _
         StrPtr("Browse JPG Files")
   Else
      [green]'ANSI version of the function[/green]
      GetFileNameFromBrowseA _
         frm.hwnd, _
         sSave, _
         255, _
         CurrentProject.Path, _
         "jpg", _
         "JPG files (*.jpg)" + Chr$(0) + "*.jpg" + Chr$(0) + _
         "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0), _
         "Browse JPG Files"
   End If
   
   idx = InStr(1, sSave, vbNullChar)
   
   If idx Then
      sSave = Left(sSave, idx - 1)
      idx = InStrRev(sSave, "\")
      BrowseFiles = Left(sSave, idx)
   Else
      BrowseFiles = ""
   End If

End Function[/blue]
Note: the code is set to return the full path without filename. If you require full path with filename:
Code:
[blue][purple][b]Change:[/b][/purple]
   If idx Then
      sSave = Left(sSave, idx - 1)
      idx = InStrRev(sSave, "\")
      BrowseFiles = Left(sSave, idx)
   Else
      BrowseFiles = ""
   End If
[purple][b]To:[/b][/purple]
   If idx Then
      BrowseFiles = Left(sSave, idx - 1)
   Else
      BrowseFiles = ""
   End If[/blue]
[/li]
[li]Finally in the [blue]On Dbl Click[/blue] event of the [blue]bound textbox[/blue]:
Code:
[blue]   Me![purple][B][I]TextboxName[/I][/B][/purple] = BrowseFiles(Me)[/blue]
[/li][/ol]
Give it a whirl and let me know. [thumbsup2]

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Thank you AceMan, I did find that code from your earlier post and changed the .txt to .jpg -
It works great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top