Solved

MS ACCESS FileDialog Cancel Button Problems Still Returning File Names

Posted on 2016-10-27
4
32 Views
Last Modified: 2016-11-01
I am using this code below that was shared with me and it appears parts are missing. I use it to pick images and place that returned path into two separate fields in my subform from my main form. Subform controls are "ProductImageLocalPath" and "ProductImageFileNm".

ProductImageFileNm = File Name Only

ProductImageLocalPath = Full Path to the file

But when I click the cancel button the control "ProductImageLocalPath" doesn't populate anything but the control "ProductImageFileNm" still populates with whatever filename is first in the folder I browsed to. How can I get it to cancel properly and if the control field is dirty then undo and delete it? THANK YOU!


Code Shared With Me:
Option Compare Database
Option Explicit




Function GetFile_Browse( _
   Optional psPathFile As String = "" _
   , Optional psDirectory As String = "" _
   , Optional psTitle As String = "" _
   , Optional pFilters As String = "JPG" _
   , Optional psReturnFilenameOnly As String _
   ) As String
   
'Requires reference to Microsoft Office #.0 Object Library.
's4p 130325...141204
'
   'PARAMETERS
   ' psPathFile -- if sent, will be parsed for the start directory
   ' psDirectory -- if psPathFile not sent, specifies start directory. default is the FE directory.
   ' psTitle = Titlebar of the dialog box
   ' pFilters -- file extension to browse to, ie: JPG, PDF, XLS*
   ' psReturnFilenameOnly - returns the name of the file stripped from the path if sent
   
   On Error GoTo Proc_Err
   
   Dim fDialog As Object 'late binding
'   Dim fDialog As Office.FileDialog 'early binding
   Dim varFile As Variant
   
   Dim sPathFile As String _
      , sStr As String _
      , nPos As Long
      
   If Len(psPathFile) > 0 Then
      'get the directory from psPathFile
      nPos = InStrRev(psPathFile, "\")
      If nPos > 0 Then
         psDirectory = Left(psPathFile, nPos)
      Else
         sPathFile = CurrentProject.Path & "\"
      End If
   Else
      If Len(psDirectory & "") > 0 Then
         'starting directory was specified, psDirectory
         sPathFile = psDirectory
      Else
         'psDirectory not specified, use FE directory
         sPathFile = CurrentProject.Path & "\"
      End If
   End If
         
   With Application.FileDialog(3) 'msoFileDialogFilePicker
      .Filters.Clear
      'use pFilter
      .Filters.Add pFilters & " Files" _
               , "*." & pFilters
      'add All files
       .Filters.Clear
       .Filters.Add "All Files", "*.*"
        .Filters.Add "Jpg", "*.Jpg*"
        .Filters.Add "Bmp", "*.Bmp"
        .Filters.Add "Png", "*.Png"
      
      If Len(psTitle) > 0 Then
         sStr = psTitle
      Else
         sStr = "Choose the Image you would like to import"
      End If
      .Title = sStr
      .InitialFileName = "\\1-PC\Database\Images\"
      .AllowMultiSelect = True
      
      If .Show = True Then
         sPathFile = .SelectedItems(1)
      Else
      MsgBox "Nothing Slelected!"
         Exit Function
      End If
   
   End With 'fDialog

   '-------- set return filename
   psReturnFilenameOnly = ""
   nPos = InStrRev(psPathFile, "\")
   If nPos > 0 Then
      psReturnFilenameOnly = Mid(psPathFile, nPos + 1)
   End If
      
   GetFile_Browse = sPathFile
Proc_Exit:
   On Error Resume Next
   Set fDialog = Nothing
   Exit Function
  
Proc_Err:
   MsgBox Err.Description, , _
        "ERROR " & Err.Number _
        & "   GetFile_Browse"

   Resume Proc_Exit
   Resume
End Function

Open in new window


Code that parses the path and returns just the filename:
Public Function AddLocalImagePath() As Variant
Dim strImagePath As String

    strImagePath = GetFile_Browse
    
    If Me.NewRecord Then
        Me!ProductImageLocalPath = strImagePath
         Me!ProductImageFileNm.Value = Dir$(strImagePath)
          DoCmd.GoToControl "sbfrmProductImages"
    Else
          DoCmd.GoToControl "sbfrmProductImages"
         DoCmd.GoToRecord , , acNewRec
        Me!ProductImageLocalPath = strImagePath
       Me!ProductImageFileNm.Value = Dir$(strImagePath)
    End If
    
End Function

Open in new window

0
Comment
Question by:Dustin Stanley
  • 3
4 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
Comment Utility
I think you might be better off with this sort of code:

 .Show
If .SelectedItems.Count > 0 Then
  sPath = .SelectedItems(1)
Else
  MsgBox "Nothing Slelected!"
  Exit Function
End If

Basically, instead of checking if the call to .Show returns anything, you instead check the SelectedItems collection to see if the user actually selected anything. If the user clicks Cancel in the dialog, the SelectedItems collection should contain nothing.
0
 

Author Comment

by:Dustin Stanley
Comment Utility
Ok that makes sense but I tried it and it still doesn't work. I have decided I don't need the full path so the my new parse code looks like this:

Public Function AddLocalImagePath() As Variant
Dim strImagePath As String

    strImagePath = GetFile_Browse
    
    If Me.NewRecord Then
         Me!ProductImageFileNm.Value = Dir$(strImagePath)
          DoCmd.GoToControl "sbfrmProductImages"
    Else
          DoCmd.GoToControl "sbfrmProductImages"
         DoCmd.GoToRecord , , acNewRec
       Me!ProductImageFileNm.Value = Dir$(strImagePath)
 End If
       
       Call ImageRequery
    
    
End Function

Open in new window


In the GetFile_Browse there just seems to be so much missing from it. I'm no expert but There is stuff missing. I know it!


Anyways the filedialog just returns the very first file in whatever folder I am in when I click cancel. I don't even click on any files to highlight them. If i just open up the filedialog and click cancel immediately it does the same thing.
0
 

Author Comment

by:Dustin Stanley
Comment Utility
Oh by the way the file name never appears in the control box until after the message box Nothing Selected.
0
 

Author Comment

by:Dustin Stanley
Comment Utility
It has to do with this line here:

Me!ProductImageFileNm.Value = Dir$(strImagePath)

Open in new window



This is what is doing it:

Dir$

Open in new window



This is so I can parse the file name when returned. What could be my options here?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now