Allow user to choose a file path in access vba code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fatema
    New Member
    • Feb 2014
    • 1

    Allow user to choose a file path in access vba code

    Hi,
    I am trying to declare a variable as a FileDialog object in VB for MS Access 2007

    Dim fd As FileDialog

    But, I am receiving an error message “ Compiler error : User-Defined type not defined”

    I already checked the reference and checked the box mcrosoft office 12.0 access database engine object library.
    But still i am getting the same error msg
    Can you please help here?
    what is the appropriate VBA reference to the Microsoft Office 2007 Object library
    my code is here

    Code:
    Sub Main()
    
        'Declare a variable as a FileDialog object.
        Dim fd As FileDialog
    
        'Create a FileDialog object as a File Picker dialog box.
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
        'Declare a variable to contain the path
        'of each selected item. Even though the path is a String,
        'the variable must be a Variant because For Each...Next
        'routines only work with Variants and Objects.
        Dim vrtSelectedItem As Variant
    
        'Use a With...End With block to reference the FileDialog object.
        With fd
    
            'Use the Show method to display the File Picker dialog box and return the user's action.
            'The user pressed the action button.
            If .Show = -1 Then
    
                'Step through each string in the FileDialogSelectedItems collection.
                For Each vrtSelectedItem In .SelectedItems
    
                    'vrtSelectedItem is a String that contains the path of each selected item.
                    'You can use any file I/O functions that you want to work with this path.
                    'This example simply displays the path in a message box.
                    MsgBox "The path is: " & vrtSelectedItem
    
                Next vrtSelectedItem
            'The user pressed Cancel.
            Else
            End If
        End With
    
        'Set the object variable to Nothing.
        Set fd = Nothing
    
    End Sub


    Code:
    Public Sub cmdSelect_Click()
    Main
    End Sub
    Last edited by zmbd; Feb 7 '14, 09:48 PM. Reason: [Z{Please use the [CODE/] button to format posted script and formated text - Please read the FAQ}]
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    From another bytes.com thread with the same issue:

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      For other's looking having similar issues.
      Allen Browne has a cross reference table for which version to use for which version of Office you have installed Solving Problems with Library References The table is towards the bottom of this link.

      Please do not use the older versions of these files with your newer applications. These are provided for legacy support only and can result in some very unusual issues.

      The method to add these references is the same since 2003 so here's the 2010:
      Check or Add an Object Library Reference Office 2010

      You should also refer to this insights article:
      Select a File or Folder using the FileDialog Object This code has been reviewed and used by a great number of people.

      Comment

      Working...