Can't reference in VBA sub, a folder set by a file dialog function on a form...and text box value DISAPPEARS!

Peter_W

New Member
Joined
Apr 2, 2018
Messages
31
Hello all,

I have what I thought was easy, but obviously doing something wrong, as a non-programmer. I've checked the Access board here for similar issues too:

First, I am setting up a folder from a button, with a small sub and function on the button click. First issue is, the folder will correctly show in the text box, and then it later DISAPPEARS on from the text box! I have never seen a text box value cleared, without some user action or sub code.

Public Sub cmdBrowserButton_Click()

Dim strChoice As String
strChoice = FolderSelection
If Len(strChoice) > 0 Then

Me.txtForBrowser.Value = strChoice

Else
Close
End If

End Sub


Public Function FolderSelection() As String
Dim objFD As Object
Dim strOut As String

strOut = vbNullString
'msoFileDialogFolderPicker = 4
Set objFD = Application.FileDialog(4)
If objFD.Show = -1 Then
strOut = objFD.SelectedItems(1)
End If
Set objFD = Nothing
FolderSelection = strOut
End Function


My hope is, after the folder is PERMANENTLY set in the form's text box, I can then later on use it when I export a query out to Excel, per this code:

'Set the Folder path for Exporting Query to Excel

strPath = Forms![frmMainMenu]![txtForBrowser].Value


So, the sub that should export the query, cycles through okay, but the destination template file (xltm), where I'm hoping to see the exported data, is untouched and unchanged.

I've tried many different versions of the above code snips, but think the above are the closest to being good. I'm confounded...why is the folder properly populated in the form text box, and then disappears??

Thanks in advance for any response, PW
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Peter_W

New Member
Joined
Apr 2, 2018
Messages
31
I have been able to save the folder in the form now, through trial and error. I think I am okay for the most part now. No need for any response now, thank you.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,174
Office Version
  1. 365
Platform
  1. Windows
@Peter_W
While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered elsewhere.
 

Watch MrExcel Video

Forum statistics

Threads
1,114,133
Messages
5,546,128
Members
410,731
Latest member
keobongmacao
Top