Search code examples
vbams-access

msoFileDialogFilePicker not displaying


I'm having issues with trying to display the file picker using the FileDialog property in MS Access 2016. I've tried both early and late binding but the file picker never displays. No errors are detected nor messages displayed. When I try to debug it line by line, the .show doesn't trigger the form. I've also tried If .show=-1 in lieu of .show but that does not work either. I've tried removing the library reference to Microsoft Office 16.0 Object Library, and even using late binding the window still does not display. Any ideas as to what is going wrong, and how to remedy it? I'm adding both early and late binding examples below.

Public Sub FP_EarlyBinding()

Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
  With fd

    .AllowMultiSelect = True
    .Show

    For Each vrtSelectedItem In .SelectedItems
        Debug.Print vrtSelectedItem
    Next vrtSelectedItem

  End With
End Sub


Public Sub FP_LateBinding()

Const msoFileDialogFilePicker As Long = 3
Dim fd As Object

Set fd = Application.FileDialog(msoFileDialogFilePicker)
  With fd

    .AllowMultiSelect = True
    .Show

    For Each vrtSelectedItem In .SelectedItems
        Debug.Print vrtSelectedItem
    Next vrtSelectedItem

  End With
End Sub

--------------EDIT-------------------------
Per comments, I was instructed to not to do this in a class module. I've since edited the code, as below. This still does not allow for the form to appear.

This is in a standard module:

Public Function filePicker() As Variant
Dim fd As FileDialog
Dim sFiles$
Dim vrtSelectedItem As Variant

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .AllowMultiSelect = True
    .Show
    For Each vrtSelectedItem In .SelectedItems
        sFiles = sFiles & vrtSelectedItem & ","
    Next vrtSelectedItem '-----------loops again to attach other files
End With

filePicker = sFiles
End Function

I call this procedure from a class module:

Public Sub Test()
Dim vFileList As Variant, vFile As Variant

vFileList = Split(filePicker(), ",")
For Each vFile In vFileList
    Attachments.Add vFile
Next vFile
End Sub

-------------FINAL EDIT---------------------
Turns out the problem here was the Access install...I went to the installation directory and found MSACCESS.EXE, right-clicked, and repaired.


Solution

  • What you have looks almost good.

    I recommend you ALWAYS, but ALWAYS ALWAYS ALWAYS put option explicit t the start of your code modules.

    eg this:

    Option Compare Database
    Option Explicit
    

    With above, your code does not compile. I mean, after you type in your code, I assume you do from the code menu a debug->compile. I will do that 100's of time in a typical day (after editing code). so, make that a habit.

    Also, to set the default for NEW code modules (setting does not effect existing), set this option in the VBA code editor: tools->Options

    enter image description here

    So, your code snip looks ok, but, with above option explicit, then you have to declare all variables (and bonus is compile will cast incorrect spellings or miss typed variable names).

    So, your code like this should work fine:

    Public Sub FP_LateBinding()
    
      Const msoFileDialogFilePicker As Long = 3
      Dim fd As Object
      
      Set fd = Application.FileDialog(msoFileDialogFilePicker)
      With fd
      
        .AllowMultiSelect = True
        .Show
        Dim vrtSelectedItem   As Variant
        
        For Each vrtSelectedItem In .SelectedItems
            Debug.Print vrtSelectedItem
        Next vrtSelectedItem
      
      End With
    End Sub
    

    Now, it also not clear how you are test/running the above. But, we assume you create a new code module (NOT class module)

    You will then type/paste in above. Save it

    do a debug->compile from the VBA editor menu - does it compile ok? (and if other errors in other places - fix those first).

    Now, to run it, place cursor anywhere inside of that code stub, hit f5.

    Or you can type in the name of the above sub in the debug window like this:

    FP_LateBinding
    

    So your posted code - looks really nice! - all good. Try the above option explcit. And of course now in your example, add the declare for vrtSelectedItem

    Edit: ============================================================

    Now, of course if this is a class module, then just like code in a form/report, you can't just hit f5 in the code module, nor can you JUST type in the name of the sub.

    In fact, if you place your cursor in the code and hit F5, then you get this:

    enter image description here

    So, it not that the code or file does not appear, you get the above - a big WHOPPER of a difference issue.

    And if it is a class module as opposed to a regular code module?

    Then to test or use the code, you have to write in test code into a REGULAR code module. You can't use the debug/VBA editor to JUST run a class.

    This not different then creating any other object.

    So, if we create a new class module, paste in above code, say we save the class code module as MyClassTest ?

    Then you have to write code in another standard code module like this:

      Sub Test1()
      
         Dim clsTest As New MyClassTest
         
         
         clsTest.FP_LateBinding
         
         
         
      End Sub
    

    So, you can't run class code (even code in a forms code behind module), or any custom class module, you have to FIRST create a instance.

    Now it is "possbile" that you used VBA, VB5, VB6. And in fact used "early" versions of Access (before access 2000 - 21 years ago).

    you will find by default, it is possbile to use + call class module code without first creating a instance of the class module. This ability was and is for keeping compatibility for pre access 2000 VBA, in which class code modules did not require to be delcared as a new instance. This so called "base" class instance thus was possbile.

    it turns out, that if you been importing code for the last 20 years from previous verisons of Access? This flag setting - and option STILL exists in Access 2019 - over 20 years later!!!!

    I doubt that you are/did import code from such older versions of Access, but it is in fact still possible to set a class module to NOT require a instance of the class to be created in code. However, I don't recommend doing this, despite the fact that this is still possible.