Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Jan 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Selecting a file (XP)

    How do I prompt the users to browse and select a text file in Access (via VBA) I know that in Excel it is Application.GetFileName("Excel.........etc

    Thanks in advance

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting a file (XP)

    Access 2002 has a new FileDialog object. You can look it up in the online help in the Visual Basic Editor. Example:

    Dim strFile As String
    With Application.FileDialog(msoFileDialogOpen)
    If .Show = True Then
    strFile = .SelectedItems(1)
    End If
    End With
    MsgBox strFile

    By the way, you need a reference to the Microsoft Office 10.0 Object Library (in Tools | References...) to be able to use msoFileDialogOpen.

  3. #3
    New Lounger
    Join Date
    Jan 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting a file (XP)

    Thanks, perfect.

    I am not sure if the user is in fact using Access 2002, and I see it doesn't work for Access 2000. Is there a way to do it in Access 2000?

  4. #4
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Selecting a file (XP)

    I have not tried the following use of the Common Dialogs in Access 2000, but it should work for all VBA versions.<pre> Declare Function GetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" (pOpenfilename As OpenFileName) As Long

    Declare Function GetActiveWindow Lib "user32" () As Long

    Public 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

    Sub BrowseForFile()
    Dim FName As OpenFileName
    FName.lStructSize = Len(FName)
    FName.hwndOwner = GetActiveWindow()
    FName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" _
    + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
    FName.lpstrFile = Space$(254)
    FName.nMaxFile = 255
    FName.lpstrFileTitle = Space$(254)
    FName.nMaxFileTitle = 255
    FName.lpstrInitialDir = "C:" 'Set the initial directory to browse from
    FName.lpstrTitle = "Select Template to Install"
    FName.flags = 0
    If GetOpenFileName(FName) Then
    MsgBox "Selected Filename : " & Trim(FName.lpstrFile)
    Else
    MsgBox "Cancel was pressed"
    End If
    End Sub</pre>

    This is an example which you might wish to convert into a function.

    Andrew C

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting a file (XP)

    Andrew has already posted code you can use. You can also download a demo database from <post#=304810>post 304810</post#>; it contains a class module that acts as a wrapper around the API code. The form in the database demonstrates how to use it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •