MS Access - VBA - Take file name from Source to Destination path in File Dialog Picker
Hey,
I have a form that have two text boxes as Source and Target. The Source take the source path and the destination textbox xopy the file to the destination path. Copying file is working well, i want that when the user copy the file from Source to destination in file dialog picker it also take the file name when the destination is open from source with it's extension. Is there a way to achieve this?
Private Sub txtSource_Click() Dim Dialog As FileDialog Dim Selected As Long Set Dialog = FileDialog(msoFileDialogFilePicker) With Dialog .AllowMultiSelect = False .InitialFileName = Nz(Me!txtSource.Value) .Title = "Select file to copy" Selected = .Show If Selected <> 0 Then Me!txtSource.Value = .SelectedItems.Item(1) End If End With Cancel = TrueEnd SubPrivate Sub txtTarget_Click()Dim Dialog As FileDialog Dim Selected As Long Set Dialog = FileDialog(msoFileDialogSaveAs) With Dialog .AllowMultiSelect = False .InitialFileName = Nz(Me!txtTarget.Value) .Title = "Name saved file" Selected = .Show If Selected <> 0 Then Me!txtTarget.Value = .SelectedItems.Item(1) TargetFile = .SelectedItems.Item(1) If Not IsNull(Me!txtSource.Value) Then FileCopy Me!txtSource.Value, Me!txtTarget.Value Me.txtTarget = Split(TargetFile, ":")(1) End If ' Application.FollowHyperlink Me.txtTarget End If End With Cancel = TrueEnd Sub
Also as shown in first image to copy file the file name is test, i want that when the other target location open there file name test should come automatically with extension also.
Any error pop up if you can guide me in this: it's run time error says "Permission Denied contact Administrator", i am the administrator of the server and the write permission is set to yes. When i manually copy the file it's working but when i try to copy using this VBA code it's giving this error. Although considering the file gets copied but still it's poping up this error, do you have any idea about this?
As you can see in the image the file is copied as i have write permission. Also when i manually tried to copy its working without any error.Why is this error coming?
WS
ASKER
@Mate, the issue was that the initial file name was empty and i wanted the file name to be the one from source in the file dialog, the solution which Gustav mention start working as in the target location i was taking the target name file, it should be the initial filename from Source.
I asked a question, i was close just a bit problem which Gustav solution solved.
OK.
The file permission error is completely separate issue - so don't blame your code for this.
/gustav
WS
ASKER
yeah but why is this coming just in Access ? like if i copy manually it doesn't appear. The code which i mention is the only code im using to copy. Is there anything other than copying also?
Gustav Brock
Perhaps the path is listed (or not listed) in:
Settings, Center for Security etc., Settings for Center for Security etc., Trusted Paths
you're welcome. I notice you are writing to N:\ ... did you also do as Gustav suggested and set N:\ as a trusted location? same dialog box -- "Add new location" -- and when you set that, be sure to check "Subfolders of this location are also trusted", if you want to be able to use any file on the drive
WS
ASKER
yes the server is mapped to N drive. i try setting as Gustav said and it show this, why is this not getting trusted location here??? Kindly Look at the attach image. error1.png
crystal (strive4peace) - Microsoft MVP, Access
apparently you are blocked from using the root of N:
make a directory and use that instead. Perhaps something like N:\temp
then the file would be N:\temp\test3.pdf
... or maybe you already have a folder you can use?
I am the Administrator of this server so how could i be blocked from using root? i tries with directory also as N:\GLO which is there and still it gives the same error. Is there anything i should change in server ? The permission for this N:\ are set to Read,Write and Execute. Yes it's not allowed to modify because in that case user can delete the file also which is a huge mess. But as the write permission and other are enable for this folder it should work and mark as trusted.
crystal (strive4peace) - Microsoft MVP, Access
Hard to guess without being there and exploring ... what about using a different drive letter? What is the N: drive mapped to?
WS
ASKER
N drive is mapped to Server Address like \\163.xx.xx.x\D, first is the server address and in the server there is a folder D which is mapped to my PC.
can you not use a local location instead then? do you have any local drives or paths that automatically synchronize to the server?
Gustav Brock
Even if you are administrator, you should normally not run as such. Thus, the shared folder for this purpose should be granted rights for "Users" (or "Domain Users" if on a domain).
/gustav
WS
ASKER
@Crystal, As it is mapped to my local PC as N:\ so it's a local path which is synchronize as i add something here it is there also on server, isn't it?
@Gustav,
the shared folder for this purpose should be granted rights for "Users" (or "Domain Users" if on a domain).
Any error pop up if you can guide me in this: it's run time error says "Permission Denied contact Administrator", i am the administrator of the server and the write permission is set to yes. When i manually copy the file it's working but when i try to copy using this VBA code it's giving this error. Although considering the file gets copied but still it's poping up this error, do you have any idea about this?
Thank you Gustav.