Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS ACCESS FileDialog Cancel Button Problems Still Returning File Names

Posted on 2016-10-27
4
Medium Priority
?
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 41863023
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
ID: 41863037
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
ID: 41863043
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
ID: 41863070
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

636 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