Link to home
Start Free TrialLog in
Avatar of WS
WSFlag for United Arab Emirates

asked on

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 = True
End Sub

Private 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 = True
   
End Sub

Open in new window


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.

Thank you.
getfilename1.png
getfilename2.png
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WS

ASKER

it works.

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WS

ASKER

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? User generated image
Avatar of 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.

Sorry for not explaining well.
OK.
The file permission error is completely separate issue - so don't blame your code for this.

/gustav
Avatar of 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?
Perhaps the path is listed (or not listed) in:

Settings, Center for Security etc., Settings for Center for Security etc., Trusted Paths

/gustav
Avatar of WS

ASKER

Thank you.

For the other error i think i should ask another question as that is something different i guess.

Thank you.
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

WS, for the permissions, if you are sure you have sufficient Windows privileges, perhaps:

File, Options, Trust Center, Trust Center Settings, Trusted Locations --> check: Allow Trusted Locations on my network

?

please let us know if this works, thanks

have an awesome day,
crystal
Avatar of WS

ASKER

Thank you Crystal, Allow Trusted Locations on my network is set to true like the it's checked but still the same. Anything else to check?
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
Avatar of 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
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?
Avatar of WS

ASKER

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.
Hard to guess without being there and exploring ... what about using a different drive letter? What is the N: drive mapped to?
Avatar of 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.
>>  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?
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
Avatar of 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).
how can i do that? how can i grant right?