Results 1 to 9 of 9
  1. #1
    DigitalAdrenaline is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2012
    Posts
    100

    dim File As FileDialog - not working

    Hi Guys, I'm watching a video using VBA. The statement I'm using is part of a procedure to press a button and place an image into a box. But, it's not working. Access is throwing up: User-defined Type Not defined. Just wondering if anyone has any ideas how to correct.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    ive used:
    user picks a file in folder
    then loads the picked file into box

    Code:
    sub btnPick_click()
    dim sPath as string
    
    sPath = UserPick1File("c:\folder\")
    if sPath<>"" then image1.picture= sPath
    
    end sub
    
    
    
    Public Function UserPick1File(Optional pvPath)
    Dim strTable As String
    Dim strFilePath As String
    Dim sDialog As String, sDecr  As String, sExt As String
    
    
    '===================
    'YOU MUST ADD REFERENCE : Microsoft Office 11.0 Object Library, in vbe menu, TOOLS, REFERENCES
    '===================
    
    With Application.FileDialog(msoFileDialogFilePicker)   
        .AllowMultiSelect = False
        .Title = "Locate a file to Import"
        .ButtonName = "Import"
       ' .Filters.Clear
       ' .Filters.Add "All Files", "*.*"
        .InitialFileName = pvPath
        .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
        
            If .Show = 0 Then
               'There is a problem
               Exit Function
            End If
        
        'Save the first file selected
        UserPick1File = Trim(.SelectedItems(1))
    End With
    End Function

  3. #3
    DigitalAdrenaline is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2012
    Posts
    100
    Thanks ranman, Yes, I found it in the references. Code now appears to be working. But.. When I navigate to my folder, no jpg images are there. The folder is blank. Access is searching for a folder name not a file name. Not sure where to go from here.

    Private Sub Command381_Click()
    Dim File As FileDialog
    Set File = Application.FileDialog(msoFileDialogFolderPicker)
    File.AllowMultiSelect = False
    If File.Show Then
    Me.txtFileLocation = File.SelectedItems.Item(1)
    End If

    End Sub

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    version 11 probably isn't because Ranman is using 2010 and you are using 2016 - you should have a later version

  5. #5
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    Assign the .Filter Object with necessary parameters to display required files.

    Code:
    .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1

  6. #6
    DigitalAdrenaline is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2012
    Posts
    100
    Hi apr pillai, thanks, but I'm not sure where to place your code within the code I've written. Which line should I place your statement? I'm very new to VBA. Thanks.

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Set File = Application.FileDialog(msoFileDialogFolderPicker)
    Well, one thing is you are using the FOLDER picker option instead of the file picker option - "msoFileDialogFilePicker"


    Here is the code I use. I did modify it a little for you...... <snip> means I cut out code that didn't apply to you.
    Code:
    <snip>
        Dim d As DAO.Database
        Dim strFileName As String
        Dim fd As FileDialog
    
        Set d = CurrentDb
    
    
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .Title = "Select Beginning Balances file to import"    '1) To set the caption of the dialog box, set the Title property
            .InitialFileName = CurrentProject.Path        '2) Set the oddly named InitialFileName property to determine the initial folder selected
            .InitialView = msoFileDialogViewDetails        '3) Set the InitialView property to control how your files appear on screen (as a list, icons, etc.)
            .AllowMultiSelect = False
            .Filters.Clear        '4) To set the filters (you can have as many as you like) first clear any existing ones, then add them one by one
    
            'what I use
    '        .Filters.Add "CSV Files", "*.csv"
    '        .FilterIndex = 1                       ' if there's more than one filter, you can control which one is selected by default
    '        .ButtonName = "Choose CSV file"        '5) Set the ButtonName property to control the text on
    
    
            'for you 
            .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1   ' if there's more than one filter, you can control which one is selected by default
            .ButtonName = "Choose Image file"        '5) Set the ButtonName property to control the text on the button.
    
    
            If .Show = -1 Then
                strFileName = .SelectedItems(1)       ' get the file name & path
                ' MsgBox strFileName               'display name and path of file chosen
            Else
                MsgBox "You chose cancel"         'didn't choose anything (clicked on CANCEL)
                Set d = Nothing
                Exit Sub
            End If
        End With
    
        If Len(Trim(strFileName & "")) > 0 Then
    <snip>

  8. #8
    DigitalAdrenaline is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2012
    Posts
    100
    Now that's embarrassing. Thanks ssanfu, I can't believe I missed that. Thanks for your code assistance. I will implement that accordingly. Cheers.

  9. #9
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    We all have those moments.......

    Good luck with your project....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-07-2016, 07:11 AM
  2. Use FileDialog to get file with txt extension
    By newbieX in forum Programming
    Replies: 4
    Last Post: 01-11-2016, 07:28 PM
  3. Replies: 1
    Last Post: 02-09-2015, 03:24 PM
  4. Replies: 1
    Last Post: 12-27-2014, 12:38 PM
  5. FileDialog
    By Tomfernandez1 in forum Programming
    Replies: 9
    Last Post: 04-13-2011, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums