Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 10th, 2004, 01:42 AM
Registered User
 
Join Date: Jul 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default File Field - How to browse for a file?

Hi,

I have a field to store the path and name of a file, and I want to design a form (access 2000) to browse for the file, How can I do this? Maybe I should use an Activex component, but I do not know which, how to configure and use it? Do you have form examples?

Thanks,
Salvador Hernandez
 
Old July 10th, 2004, 02:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hello Salvador,

No ActiveX controls necessary. You can just use the Windows Open File dialog with an API call. The following will load the full path and file name of a file into a textbox for you.

Just place a textbox and command button on your form and place the following in the command buttons OnClick event:

**Code********

Private Sub cmdFindFile_Click()
    On Error GoTo Errorhandler
    Me.txtFile = OpenFile(Me.txtFile)
    Exit Sub
Errorhandler:
    MsgBox "Error: " & Err.Description & " (" & Err.Number & ")"

End Sub

**End Code********

Then place the API call in a standard module, along with a little utility function to handle null characters...

**Code********

Option Compare Database
Option Explicit

Type FileSelInfo
    hwndOwner As Long
    strApp As String * 255
    strTitle As String * 255
    strButton As String * 255
    strFile As String * 4096
    strDir As String * 255
    strFilter As String * 255
    lngIndex As Long
    lngView As Long
    lngFlags As Long
End Type

Declare Function GetFileInfo Lib "msaccess.exe" Alias "#56" (FSI As FileSelInfo, ByVal fOpen As Integer) As Long

Function OpenFile(Optional strFile As Variant = Null, Optional strFilter As String = "All Files (*.*)", Optional strDir As String = "", Optional strTitle As String = "", Optional strButton As String = "")
'Use to choose a file to open
Dim FSI As FileSelInfo

    On Error GoTo Errorhandler
    With FSI
        .lngFlags = 0
        .strFilter = RTrim(strFilter) & vbNullChar
        .lngIndex = CInt("0")
        If Not IsNull(strFile) Then
            .strFile = RTrim(strFile) & vbNullChar
        End If
        .strTitle = RTrim(strTitle) & vbNullChar
        .strButton = RTrim(strButton) & vbNullChar
        .strDir = RTrim(strDir) & vbNullChar
    End With

    If GetFileInfo(FSI, True) = 0 Then OpenFile = TrimNull(FSI.strFile)
    GoTo Done

Errorhandler:
    MsgBox "Error: " & Err.Description & " (" & Err.Number & ")"
Done:
End Function

Private Function TrimNull(str As String) As String
Dim i As Integer
    TrimNull = str
    i = InStr(str, vbNullChar)
    If i > 0 Then TrimNull = Left(str, i - 1)
    TrimNull = (RTrim(TrimNull))
End Function

**Code********

HTH,

Bob





 
Old July 27th, 2004, 10:16 AM
Registered User
 
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Found this code very useful to open existing files. Could someone please post the corresponding save file code.

Thanks,

Rihallix
 
Old July 27th, 2004, 12:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Rihallix,

What version of Access are you using? If its 2K2 or 2K3, you can simplify all this by using the FileDialog property of the Application object:

From the Help file:

' Display the Save As dialog box.

Dim dlgSaveAs As FileDialog

Set dlgSaveAs = Application.FileDialog( _
    FileDialogType:=msoFileDialogSaveAs)

dlgSaveAs.Show

' Display the Open dialog box and allow a user to select multiple files to open.

Dim dlgOpen As FileDialog

Set dlgOpen = Application.FileDialog( _
    FileDialogType:=msoFileDialogOpen)

With dlgOpen
    .AllowMultiSelect = True
    .Show
End With

If you need the API call let me know.

HTH,

Bob



 
Old July 27th, 2004, 01:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Check out the following sample db assembled by Candace Trip for API calls to invoke three of the Windows Common Dialogs: color, open file, and save. The sample is at the bottom of the "Access 2000/2002" section of the page and is called "Windows Common Dialogs."

http://www.candace-tripp.com/_pages/..._downloads.asp

Or if you have a copy of the Access 2x Developer's Handbook, Ken Getz has but together a cool Common Dialog class module that encapsulates API calls for invoking the file open/save, color and font dialogs.

HTH,

Bob






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a file browse dialog box gaurisharya Beginning VB 6 4 April 30th, 2008 12:43 AM
To browse a file from client system Indraja ASP.NET 2.0 Basics 2 May 3rd, 2007 07:56 AM
how to browse a file from computer for attachment? rocksbhavesh ASP.NET 1.0 and 1.1 Professional 2 March 26th, 2007 08:47 AM
Browse File mgnishan PHP How-To 2 March 23rd, 2007 01:54 PM
Browse to File Control? Aaron Edwards Access 3 September 13th, 2005 01:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.