Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with code for copying file to new location and renaming 2

Status
Not open for further replies.

Moss100

Technical User
Aug 10, 2004
579
0
16
GB
Hello,

I am using the code below and have the following problems...

If the file exists and I say I don't want to overwrite the code continues to execute.

Also the files are copying but not retaining their correct file extentions.

I am just learning coding so I would much appreciate and advice in regard to modifying the code to make it better in any areas.

Me.txt_File_Selected is a text box on my form containing the full source path and file name i.e. c:\temp\dog.jpg
Me.txtfile is a text box on my form containing the destination path and file name i.e. c:\temp\dog



Many thanks Mark


Code:
   Dim fso As Object
   Dim sourceFile As String
   Dim targetFile As String
   Dim answer As Boolean

  Set fso = CreateObject("Scripting.FileSystemObject")
    sourceFile = Me.txt_File_Selected
    targetFile = Me.txtfile
    
  If fso.FileExists(sourceFile) = False Then
  MsgBox "The source file can not be found - please search for the file again"
  Exit Sub
  End If


  If fso.FileExists(targetFile) = True Then
     answer = MsgBox("File already exists in this location. " _
        & "Are you sure you want to continue? If you continue " _
        & "the file at destination will be over written!", _
            vbInformation + vbYesNo)
      If answer = vbNo Then
        Exit Sub
        End If
        End If
      
fso.CopyFile sourceFile, targetFile
Set fso = Nothing
MsgBox "The file has been Added"
 
Would this work for you?

Code:
    If Dir(txtfile) <> "" Then
        yesno = MsgBox("File exists.  Overwrite/Replace?", vbYesNo, "File Exists")
        If yesno = vbNo Then
            GoTo exitsub
        End If
    End If

Also, instead of using setting up with fso, you could do the file copying with this line instead.
[tt]
FileCopy SourceFile, DestinationFile ' Copy source to target.
[/tt]
 
Thank you.

I am not sure which to use.

Is FSO better or is FileCopy??

I am using the code below which is now working, but do you think the FileCopy method is better?

Many thanks Mark

Code:
Private Sub btn_Save_File_DblClick(Cancel As Integer)

   Dim fso As Object
   Dim sourceFile As String
   Dim targetFile As String
   Dim answer As Boolean

  Set fso = CreateObject("Scripting.FileSystemObject")
   sourceFile = Me.txt_File_Selected
   targetFile = Me.txt_File_Save_Name_Full
    
 If fso.FileExists(sourceFile) = False Then
  MsgBox "The SOURCE file can NOT be found - please search for the file again"
  Exit Sub
 End If

 If fso.FileExists(targetFile) = True Then
     answer = MsgBox("File already exists in this location. " _
        & "Are you sure you want to continue? If you continue " _
        & "the file at destination will be over written!", _
            vbInformation + vbYesNo)
      If answer = vbNo Then
      MsgBox "The SOURCE file can NOT be found - please search for the file again"
      Exit Sub
      End If

End If
      
fso.CopyFile sourceFile, targetFile

Set fso = Nothing

MsgBox "The file has been Added"

End Sub
 
When i choose 'No' as the answer below, the code still continues to copy - can anyone see whats wrong... Thanks Mark

Code:
If fso.FileExists(targetFile) = True Then
     answer = MsgBox("File already exists in this location. " _
        & "Are you sure you want to continue? If you continue " _
        & "the file at destination will be over written!", _
            vbInformation + vbYesNo)
      If answer = vbNo Then
      MsgBox "The SOURCE file can NOT be found - please search for the file again"
      Exit Sub
      End If

End If
 


Code:
Dim answer [red]As Boolean[/red][green]   '<== Here is your problem

'Try this instead:[/green]
If fso.FileExists(targetFile) = True Then[blue]
     If vbNo = [/blue]MsgBox("File already exists in this location. " _
        & "Are you sure you want to continue? If you continue " _
        & "the file at destination will be over written!", _
            vbInformation + vbYesNo) [blue]Then[/blue]
        MsgBox "The SOURCE file can NOT be found - please search for the file again"
        Exit Sub
      End If
End If

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
FYI,
Boolean can only hold True/False, which is either -1/0 or 1/0 or "anything but 0"/0

If you do this:
Code:
Debug.Print "vbYes - " & Val(vbYes)
Debug.Print "vbNo - " & Val(vbNo)
Debug.Print "vbCancel - " & Val(vbCancel)
you get:[tt]
vbYes - 6
vbNo - 7
vbCancel - 2
[/tt]
That's why your [tt]answer[/tt] was always True (vbNo = 7 and 7 <> 0) :)

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top