How can I Set the File Dialog Box Filter to the Last Filter the User Selected?

Hello,

My company's MS Access VBA software uses Azure Blob Storage and allows users to upload files from their computer to our blob storage. I have a user that requested for the file dialog box filter to default to what they last selected. Currently the filter always defaults to Image files and usually this user uploads PDFs. Is there anyway to get the filter to default to the last filter the user selected? Please see attached file to view the current code for the file dialog.

Thanks in advance!
File_Dialog_Code.PNG
SpaceCoastLifeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:
You can set the property InitialFileName :

fd.InitialFileName = "d:\folder\subfolder" & "\"

Open in new window

Martin LissOlder than dirtCommented:
If you attach your workbook I can add code that will do what you want.
Sam JacobsDirector of Technology Development, IPMCommented:
Maybe write the index of the user's selection to a file (along the lines of a cookie), and then reading it back the next time ... ?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

SpaceCoastLifeAuthor Commented:
Maybe write the index of the user's selection to a file (along the lines of a cookie), and then reading it back the next time ... ?

Sam,

Thank you for your response. I was thinking something along these lines, but the problem is...How do I access the index of the user's selection?
Sam JacobsDirector of Technology Development, IPMCommented:
You can get it from the .FilterIndex property of the dialog.
SpaceCoastLifeAuthor Commented:
You can get it from the .FilterIndex property of the dialog.

I see, and how would I go about retrieving that from the below code? I see that I can pass a filterIndex parameter to the ahtCommonFileOpenSave() function.

    Dim strFilePath As String
    Dim strFilter As String
    
    strFilter = ahtAddFilterItem(strFilter, "Image Files (*.jpg, *.jpeg, *.png, *.gif, *.tif)", "*.jpg;*.jpeg;*.png;*.gif;*.tif")
    strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt, *.doc, *.docx, *.rtf)", "*.txt;*.doc;*.docx;*.rtf")
    strFilter = ahtAddFilterItem(strFilter, "Data Files (*.csv, *.pps, *.ppt, *.pptx)", "*.csv;*.pps;*.ppt;*.pptx")
    strFilter = ahtAddFilterItem(strFilter, "Page Layout Files (*.pdf)", "*.pdf")
    strFilter = ahtAddFilterItem(strFilter, "Spreadsheet Files (*.xls, *.xlsx)", "*.xls;*.xlsx")
    
    
    ' Prompt the user to pick the location and file name.
    strFilePath = ahtCommonFileOpenSave( _
        Filter:=strFilter, _
        OpenFile:=False)

Open in new window

Sam JacobsDirector of Technology Development, IPMCommented:
Sorry, I'm not familiar with the ahtCommonFileOpenSave()  function.
Can you use a regular FIle Picker dialog? Something along these lines ...

Dim SelectedFilter As Integer

Sub Macro1()

strFile = GetFile()

MsgBox "The selected filter was #" & SelectedFilter

End Sub

Function GetFile() As String
 
With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Image Files (*.jpg, *.jpeg, *.png, *.gif, *.tif)", "*.jpg;*.jpeg;*.png;*.gif;*.tif"
        .Filters.Add "Text Files (*.txt, *.doc, *.docx, *.rtf)", "*.txt;*.doc;*.docx;*.rtf"
        .Filters.Add "Data Files (*.csv, *.pps, *.ppt, *.pptx)", "*.csv;*.pps;*.ppt;*.pptx"
        .Filters.Add "Page Layout Files (*.pdf)", "*.pdf"
        .Filters.Add "Spreadsheet Files (*.xls, *.xlsx)", "*.xls;*.xlsx"
    'Show the dialog box
    If .Show = -1 Then
        GetFile = .SelectedItems.Item(1)
    Else
        GetFile = ""
    End If
    SelectedFilter = .FilterIndex
End With

End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SpaceCoastLifeAuthor Commented:
Can you use a regular FIle Picker dialog?

Unfortunately I cannot, I tried using the regular File Picker dialog but it throws an error on some of our servers and we don't want to exert the resources to figure out why for something small like this.

Your solution is very helpful, so I thank you for that, but I think as a work-around I am going to make a pseudo "All Files" filter which will display all of the files that we allow our users to upload and then they can filter further from there with our other options.

Dim strFilePath As String
Dim strFilter As String
    
strFilter = ahtAddFilterItem(strFilter, "All Files (*.jpg, *.jpeg, *.png, *.gif, *.tif, *.txt, *.doc, *.docx, *.rtf, *.csv, *.pps, *.ppt, *.pptx, *.pdf, *.xls, *.xlsx)", _
        "*.jpg;*.jpeg;*.png;*.gif;*.tif;*.txt;*.doc;*.docx;*.rtf; *.csv;*.pps;*.ppt;*.pptx; *.pdf; *.xls;*.xlsx")
    strFilter = ahtAddFilterItem(strFilter, "Image Files (*.jpg, *.jpeg, *.png, *.gif, *.tif)", "*.jpg;*.jpeg;*.png;*.gif;*.tif")
    strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt, *.doc, *.docx, *.rtf)", "*.txt;*.doc;*.docx;*.rtf")
    strFilter = ahtAddFilterItem(strFilter, "Data Files (*.csv, *.pps, *.ppt, *.pptx)", "*.csv;*.pps;*.ppt;*.pptx")
    strFilter = ahtAddFilterItem(strFilter, "Page Layout Files (*.pdf)", "*.pdf")
    strFilter = ahtAddFilterItem(strFilter, "Spreadsheet Files (*.xls, *.xlsx)", "*.xls;*.xlsx")
    
    
 ' Prompt the user to pick the location and file name.
 strFilePath = ahtCommonFileOpenSave( _
        Filter:=strFilter, _
        OpenFile:=False)

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.