SimoneRene
Registered User.
- Local time
- Today, 23:30
- Joined
- Mar 15, 2017
- Messages
- 58
Hi!
When you click a button on my form the code currently opens a file viewer and copies the file selected by the user to a new folder. The original file path is displayed in a text box which is bound to a cell in my table.
I want the text box (and corresponding cell in the table) to only display the file name(not the whole file path).
Do I need to use SafeFileName or FileName as String? Not sure how or where to put either of these in the code.
Also is there a way of only displaying the file name in the text box if there are no duplicates?
If the user tries to copy a file with a duplicate name an error message pops up and the file is not copied(which is great) but the file(and currently whole file path) is still displayed in the text box.
Thanks in advance for any help! I'm new to access!
When you click a button on my form the code currently opens a file viewer and copies the file selected by the user to a new folder. The original file path is displayed in a text box which is bound to a cell in my table.
I want the text box (and corresponding cell in the table) to only display the file name(not the whole file path).
Do I need to use SafeFileName or FileName as String? Not sure how or where to put either of these in the code.
Also is there a way of only displaying the file name in the text box if there are no duplicates?
If the user tries to copy a file with a duplicate name an error message pops up and the file is not copied(which is great) but the file(and currently whole file path) is still displayed in the text box.
Code:
Private Sub Button_Click()
Dim CopyDialog As Office.FileDialog
Dim SourceFile As Variant
Dim targetFile As Variant
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim fNameFile As String
FileViewerTxt = ""
'Add File Destiation
targetFile = "X:\Folder\"
ChDir Dir("*.*", vbDirectory)
Set CopyDialog = Application.FileDialog(msoFileDialogFilePicker)
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With CopyDialog
With fDialog
.AllowMultiSelect = False
.Title = "Select File"
.Filters.Add "pdf File", "*.pdf"
If .Show = Fasle Then
MsgBox "File Upload Canceled"
End If
For Each SourceFile In .SelectedItems
If Len(Dir(targetFile & Mid$(SourceFile, InStrRev(SourceFile, "\")))) <> 0 Then
MsgBox "Same File Name Already Exists, Please Rename and Try Again!"
End If
For Each varFile In .SelectedItems
FileViewerTxt = FileViewerTxt & varFile & vbCrLf
Next
FileCopy SourceFile, targetFile & Mid$(SourceFile, InStrRev(SourceFile, "\"))
Next
End With
End With
End Sub
Thanks in advance for any help! I'm new to access!