filedialog....not copy if same file name exist !!!!

KBJ911

New member
Local time
Today, 15:55
Joined
Jun 30, 2016
Messages
4
HI

can help please>>>>

I have a click button in form

I need this button to function if a same file name exist to cancel

else

to copy to other place



code :

Private Sub Command130_Click()
Dim CopyDialog As Office.FileDialog
Dim SourceFile As Variant
Dim targetFile As Variant
targetFile = "\\ehaf-ned61919\kbj-temp-share\attach\"
ChDir Dir("*.*", vbDirectory)
Set CopyDialog = Application.FileDialog(msoFileDialogFilePicker)
With CopyDialog
.AllowMultiSelect = True
.Title = "Select File"
.Filters.Add "pdf fiiile", "*.pdf"

If .Show = False Then
MsgBox "You canceled"
End If
If Dir(sourcename) = (targetname) Then
MsgBox "same file name is already exist,,please rename and try again !!!"

Else
For Each SourceFile In .SelectedItems
FileCopy SourceFile, targetFile & Mid$(SourceFile, InStrRev(SourceFile, "\"))
MsgBox "??"
Next
End If
End With
End Sub


thank you
 
The idea in the revise code below is that if the file that would be created with a name of

Code:
targetFile & Mid$(SourceFile, InStrRev(SourceFile, "\"))

already exists then the user see the msgbox and the file isn't copied. I wonder if you couldn't present the duplicate file name in a input box and give the user a chance to rename it right away. You can use the Name procedure to rename a file.



Code:
Private Sub Command130_Click()

Dim CopyDialog As Office.FileDialog
Dim SourceFile As Variant
Dim targetFile As Variant
targetFile = "\\ehaf-ned61919\kbj-temp-share\attach\"
ChDir Dir("*.*", vbDirectory)
Set CopyDialog = Application.FileDialog(msoFileDialogFilePicker)
With CopyDialog
    .AllowMultiSelect = True
    .Title = "Select File"
    .Filters.Add "pdf fiiile", "*.pdf"
    
    If .Show = False Then
        MsgBox "You canceled"
    End If
     For Each SourceFile In .SelectedItems
        If Len(Dir(targetFile & Mid$(SourceFile, InStrRev(SourceFile, "\")))) <> 0 Then
            MsgBox "same file name is already exist,,please rename and try again !!!"
        Else
             FileCopy SourceFile, targetFile & Mid$(SourceFile, InStrRev(SourceFile, "\"))
        End If
     Next
End With

End Sub
 
yessss it works

now..can help plz ,,, I would to add anoher click button to :

Delete the target file after I chose it mistakenly !??
 
The simple way to delete a file is use the kill function. Some example of its use are here.

The following code I got from here shows how you can simplify the file dialog if you only want to choose one item.
Code:
Dim dialog As Object
Dim pickedfile As Boolean
Dim myfile As String
Set dialog = Application.FileDialog(msoFileDialogFilePicker)
With dialog
    .AllowMultiSelect = False
    .Title = "Please pick the file to convert."
    .Filters.Clear
    .Filters.Add "Text Files", "*.TXT"
    .Filters.Add "All Files", "*.*"
    pickedfile = False
    pickedfile = .Show
    If pickedfile Then
    myfile = .SelectedItems.Item(1)
    End If
End With

After the With dialog you would probably want to add:

Code:
.InitialFileName = "\\ehaf-ned61919\kbj-temp-share\attach\"

If you just want to delete the item immediately the add

Code:
Kill myfile

after the line
Code:
myfile = .SelectedItems.Item(1)
 
Choosing a file mistakenly is not allowed in Access ;P j/k

You'll have enough challenges without introducing that :)

Cheers (LOL)!
Goh
 

Users who are viewing this thread

Back
Top Bottom