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

How to include a Browse folder button on form

Status
Not open for further replies.

javedi

Technical User
Apr 30, 2007
196
GB
Hi

I've had a look around for threads on how i can include a browse button to open a folder that will contain images from a server location and have not found any. I have placed a text field in a table with the path of a folder but am unsure how i can get Access 2007 to browse it.

Any idea's please?

Thanks,
Javedi
 
You may use this function:
Code:
Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, f As Object
Set SA = CreateObject("Shell.Application")
Set f = SA.BrowseForFolder(0, "Choose a folder", 16 + 32 + 64, strStartDir)
If (Not f Is Nothing) Then
  PickFolder = f.Items.Item.path
End If
Set f = Nothing
Set SA = Nothing
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
How are ya javedi . . .

Loading graphics into access across a network is not a good idea! Its much better if the graphics are local to the the machine.

In any case, see my post here: thread702-1289131

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

Be sure to see thread181-473997
Also faq181-2886
 
Thanks PHV. I'll give it go.

TheAceMan1, I'm hot but fine thanks. I'm not looking to attach images but to hold a path in a table field with a browse button pointing to it so users can click on that button and see what images are stored in that folder for the given record.

I'm hoping PHV's function will do that?

Javedi
 
javedi . . .

Roger That!

As another resource see my post here: thread702-1287463

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

Be sure to see thread181-473997
Also faq181-2886
 
HI PHV

I've tried using your code but it has not worked for me. Where do i place the code and where do i specify the field for the button?

Thanks again,
Javedi
 
Sorry, I misunderstood your issue.
My function is a Browse for folder dialog.
 
javedi . . .

My last post is not what you need either.
javedi said:
[blue] . . . I have placed a text field in a table with the path of a folder but am [purple]unsure how i can get Access 2007 to browse it.[/purple][/blue]
The question is: [blue] . . . browse the folder to do what?[/blue]

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

Be sure to see thread181-473997
Also faq181-2886
 
TheAceMan1,

Basically to open the folder after the user has clicked on the button. The path of the folder will be in a field.

Does this help clarify?

Javedi

 
Probably more elegant ways of doing it, but say your control is named "txtPath", try

[tt]dim r as variant
if dir(me!txtPath.value ,vbDirectory)<>"" then
r = shell("Explorer.exe " & me!txtPath)
end if[/tt]

Roy-Vidar
 
Thanks RoyVidar,

How do i link this to the field on the table? and do i place the code behind the button?

Many thanks,
Javedi
 
I'm assuming you have a control on your form displaying the path (i e, bound to the table field), called txtPath.

The above code could then be copy pasted into the on click event of a button. Alter name of control as necessary.

Roy-Vidar
 
javedi,

If all that doesn't work out for you, and you are simply wanting to see files in some folder, you should be able to just use something like possibly the FileDialog, as detailed here:

It's a way to browse to files and select them, but in a controlled method. If you are wanting just a copy of Windows Explorer to pop up browsing the folder, then RoyVidar's trick may be the ticket.

--

"If to err is human, then I must be some kind of human!" -Me
 
As a side not on that, I've used the FileDialog object in many databases, and it works wonderfully if you are wanting it to be used to pick a folder or pick a file (or even pick multiple files or folders.

You just code it in your VBA window according to your specific needs.

Here's an example of one that I used in a database that gets used quite regularly here at work:

Code:
Public Sub ImportFiles()
    Dim db As DAO.Database
    Dim fd As FileDialog
    Dim rs As DAO.Recordset
    Dim vrtSelectedItem As Variant
    Dim strFileName() As String
    Dim strTableName As String 'to store the name of the table being created/imported
    Dim x As Integer 'counter for getting the table name
    Dim strFullPath() As String 'For Excel or Text file path (to get file name from)
    Dim lngFileName As String 'For Excel file name or Text file name, depending upon type file imported
            'The combination of strFullPath() and lngFileName will give what needed to use the filename
            '  to name the new table for each item selected.
    Set db = CurrentDb
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    With fd
        .InitialFileName = "G:\"
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
                Debug.Print vrtSelectedItem
                If InStr(vrtSelectedItem, ".txt") Then
                    strFileName = Split(vrtSelectedItem, ".")
                    If UBound(strFileName) > 1 Then
                        x = 0
                        strTableName = strFileName(0)
                        For x = 1 To UBound(strFileName) - 1
                            strTableName = strTableName & "." & strFileName(x)
                        Next x
                    Else
                        strTableName = strFileName(0)
                        Debug.Print strTableName
                    End If
                    DoCmd.TransferText acImportDelim, , strTableName, vrtSelectedItem '"AllText"

                ElseIf InStr(vrtSelectedItem, ".xls") Then
                    DoCmd.TransferSpreadsheet , , strTableName, vrtSelectedItem, True
                Else

                End If
            Next vrtSelectedItem

        Else
            On Error Resume Next
            MsgBox "You clicked the cancel button."
        End If
    End With
    db.Close
    Set db = Nothing
    Set fd = Nothing
End Sub

Then I just call that code from the module with this code in a button event on the form:

Private Sub cmdMyButton_Click()
ImportFiles
End Sub

It's not all the cleanest code, as I've made some changes at different times, and really, I'm not 100% finished with it - some day I'll get back around to messing with it, but for now, it works for the needed application. [wink]

I'm sure there is some extra stuff in there that I probably don't even NEED, b/c I had it there for adding some additional functionality in which I've just not gotten around to completing.

For instance, I don't think I even need the DAO.Database and Recordset at all (for now), though I do have reasons fo r them being there... but the code that was/is going to use that is all commented out currently.

--

"If to err is human, then I must be some kind of human!" -Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top