UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Clearing .initialfilename Before Executing Code, Access 2016 VBA    
 
   
Avoraightu
post Mar 6 2020, 11:26 AM
Post#1



Posts: 3
Joined: 24-October 19



Hello! I have been scouring the Internet for a solution but with no luck.

I have 2 command buttons on a from, each opens a FileDialog box to view files associated with the selected record, each has their own .InitialFileName (starting directory) which is hard coded.

If you click between the two buttons the starting directory never comes up as the one specified, it always defaults to the last path used.

In order to get the code to work properly I must click the button, cancel the selection and then re-click the button, which brings up the correct path.

Is there a way that I can "clear" the .InitialFileName before the new FileDialog box opens?

Thanks in Advance... reading.gif
Go to the top of the page
 
cheekybuddha
post Mar 6 2020, 11:28 AM
Post#2


UtterAccess Moderator
Posts: 13,042
Joined: 6-December 03
From: Telegraph Hill


welcome2UA.gif

Please show your code.

--------------------


Regards,

David Marten
Go to the top of the page
 
Avoraightu
post Mar 6 2020, 11:35 AM
Post#3



Posts: 3
Joined: 24-October 19



Private Sub ViewAutopsyFiles_Click()

Dim fd As FileDialog
Dim fileNamePath As String, fileExtension As String, fileName As String

If fileNamePath = "" Then
Set fd = Application.FileDialog(msoFileDialogOpen)
Dim FileChosen As Integer
FileChosen = fd.Show
fd.Title = "Choose File to Open"
fd.InitialView = msoFileDialogViewPreview
fd.InitialFileName = "G:\ENGINEERING\PU Autopsy\"
fd.Filters.Clear
fd.Filters.Add "All files", "*.*"
fd.FilterIndex = 1
fd.ButtonName = "Choose this file"
If FileChosen <> -1 Then 'didn't choose anything (clicked on CANCEL)
MsgBox "No file was selected!", vbExclamation
Else
fileNamePath = fd.SelectedItems(1)
fileName = Dir(fileNamePath)
fileExtension = Right$(fileName, Len(fileName) - InStrRev(fileName, "."))

'Open the file
Application.FollowHyperlink fileNamePath

End If
Set fd = Nothing
End If

End Sub

The code works properly except for the fact that the starting directory always defaults to the previous path?
Go to the top of the page
 
cheekybuddha
post Mar 6 2020, 11:41 AM
Post#4


UtterAccess Moderator
Posts: 13,042
Joined: 6-December 03
From: Telegraph Hill


Try setting all the properties before opening the FileDialog:
CODE
Private Sub ViewAutopsyFiles_Click()

  Dim fd As FileDialog
  Dim fileNamePath As String, fileExtension As String, fileName As String
  Dim FileChosen As Integer

  If fileNamePath = "" Then
    Set fd = Application.FileDialog(msoFileDialogOpen)
    fd.Title = "Choose File to Open"
    fd.InitialView = msoFileDialogViewPreview
    fd.InitialFileName = "G:\ENGINEERING\PU Autopsy\"
    fd.Filters.Clear
    fd.Filters.Add "All files", "*.*"
    fd.FilterIndex = 1
    fd.ButtonName = "Choose this file"
    FileChosen = fd.Show
    If FileChosen <> -1 Then 'didn't choose anything (clicked on CANCEL)
      MsgBox "No file was selected!", vbExclamation
    Else
      fileNamePath = fd.SelectedItems(1)
      fileName = Dir(fileNamePath)
      fileExtension = Right$(fileName, Len(fileName) - InStrRev(fileName, "."))

'     Open the file
      Application.FollowHyperlink fileNamePath

    End If
    Set fd = Nothing
  End If

End Sub


hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
ADezii
post Mar 6 2020, 11:42 AM
Post#5



Posts: 3,089
Joined: 4-February 07
From: USA, Florida, Delray Beach


You could try setting the InitialFileName Property to a Modular Level Form Variable and modify according to which Button was clicked.
Go to the top of the page
 
Avoraightu
post Mar 6 2020, 01:18 PM
Post#6



Posts: 3
Joined: 24-October 19



Attached File  ViewCode.png ( 92.97K )Number of downloads: 0


Thank you for the quick response that will save me a few grey hairs!

By moving fd.show further down I am able to get it to open correctly vs the other button where I made no change and the issue continues.

It is interesting though that the "ButtonName" doesn't change until you actually click on a file which is somewhat counter intuitive. IMHO
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    11th July 2020 - 05:48 AM