Solved

VB5 FileCopy

Posted on 1998-05-07
14
703 Views
Last Modified: 2010-08-05
Thanks in advance for helping me out.

I am having trouble using "FileCopy."  The specific file being copied is the file that listed in the textbox and it will always go to the same directory.  I assigned variable Destination to "c:test" and SourceFile to "file1.filename."  I am getting error number 56.

Dim SourceFile As String
Dim Destination As String

Private Sub File1_Click()
    txbFileName.Text = File1.filename
    SourceFile = File1.filename
   
End Sub

Private Sub cmdSend_Click()
    FName = txbFileName.Text
    FileCopy SourceFile, Destination
End Sub
0
Comment
Question by:LClos
  • 7
  • 5
  • 2
14 Comments
 
LVL 7

Expert Comment

by:kamall
ID: 1443391
LClos,
Your destination file name syntax is incorrect.
Use "c:\test" instead of "c:test".
Regards
0
 

Author Comment

by:LClos
ID: 1443392
Thanks for the response, but the "\" left out was only a typo in my submitted question.  I still get the error.  The error number is 53 not 56.  I can submit more code if needed.
0
 
LVL 7

Accepted Solution

by:
kamall earned 100 total points
ID: 1443393
Well LClos,
The error number 53 is "file not found".
Yes, the problem is that you are giving the source file as File1.FileName but you are not supplying the full pathname. You must write:
SourceFile = File1.Path & "\" & File1.FileName

Regards
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Expert Comment

by:kamall
ID: 1443394
kamall:
Almost, you missed the "\"

The code should be:
  SourceFile = File1.Path
  If Right$(SourceFile, 1) <> "\" Then
    SourceFile = SourceFile & "\"
  End If
  SourceFile = SourceFile & File1.Filename

0
 
LVL 6

Expert Comment

by:clifABB
ID: 1443395
kamall:
My apologies, you caught it.  :)
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1443396
Now, I have error 75 (access to path).  I plan on giving you the points since your answer helped me greatly.  And, I will increase them if you stick with me 'til I get this file to copy. You are more than welcome to let me know what kind of points you think this is worth.  Thanks.
0
 

Author Comment

by:LClos
ID: 1443397
ha ha ha ha ha..... clifABB,
You made me laugh, man.
I was faster :-)
Anyway, thanks for your contribution.

Well LClos, thank you for your kind offer regarding the points. I will leave this to you to decide.
Also, I will "stick" with you until we bring it to an end.

Now....
Look for the following regrding error #75 (from the VB help):

---------------------------------------------------------------
During a file- or disk-access operation (for example, Open, MkDir, ChDir, or RmDir), the operating system could not make a connection between the path and the filename.  This error has the following causes and solutions:  

The file specification is not correctly formatted.

A filename can contain a fully qualified (absolute) or relative path.  A fully qualified path starts with the drive name (if the path is on another drive) and lists the explicit path from the root to the file.  Any path that is not fully qualified is relative to the current drive and directory.

You attempted to save a file that would replace an existing read-only file.

Change the read-only attribute of the target file or save the file with a different filename.

You attempted to open a read-only file in sequential Output or Append mode.

Open the file in Input mode or change the read-only attribute of the file.
---------------------------------------------------------------

I think I can help you more if you post your code.
0
 
LVL 7

Expert Comment

by:kamall
ID: 1443398
I also suggest the following:
Before your FileCopy statement, write the following code to check the file names:

MsgBox SourceFile & chr(10) & Destination

This will show you the full path/filename of both source and destination files.

Also, I would like to know, where you define your destination file name? Is it a global variable and you assign it in your form load event for example?

Anyway, the code above will show you the real things.
I suggest you test with the code above and check the things before posting your code.
0
 
LVL 7

Expert Comment

by:kamall
ID: 1443399
Here is my code in its entirety:

Dim SourceFile As String
Dim Destination As String

Private Sub cboFileType_Click()
 Select Case cboFileType.ListIndex
    Case 0
        File1.Pattern = "*.est"
    Case 1
        File1.Pattern = "*.mod"
    Case 2
        File1.Pattern = "*.pat"
    End Select
End Sub

Private Sub cmdSend_Click()
    FName = txbFileName.Text
    MsgBox SourceFile & Chr(10) & Destination
    FileCopy SourceFile, Destination
End Sub

Private Sub Dir1_Change()
    File1.Path = Dir1.Path
    Label2.Caption = Dir1.Path
   
    If File1.filename <> "" Then
        txbFileName.Text = File1.filename
    Else
        txbFileName.Text = File1.Pattern
    End If
   
End Sub

Private Sub Drive1_Change()
    Dim Msg As String
   
    On Error Resume Next
   
    Screen.MousePointer = 11 ' Hour glass pointer
    Dir1.Path = Drive1.Drive
    Screen.MousePointer = 0
   
    If Err Then 'Dir1 tried to become a bad or unavailable drive
        Msg = "Error " & Err & ": " & Error$ & "."
        MsgBox Msg, 48, "Drive/Directory Error"
        Drive1.Drive = Dir1.Path
    End If
   
    On Error GoTo 0
End Sub

Private Sub File1_Click()
    txbFileName.Text = File1.filename
   SourceFile = File1.Path & "\" & File1.filename
   
End Sub

Private Sub Form_Load()
    cboFileType.AddItem "Establishment (*.est)"
    cboFileType.AddItem "Modification (*.mod)"
    cboFileType.AddItem "Paternity (*.pat)"
   
    File1.Path = "d:\temp\test"
    Dir1.Path = File1.Path
    Destination = "d:\temp"

    If File1.filename <> "" Then
        txbFileName.Text = File1.filename
    Else
        txbFileName.Text = File1.Pattern
    End If
End Sub

0
 

Author Comment

by:LClos
ID: 1443400
LClos,
Here is your mistake:
Destination = "d:\temp"
Destination is an existing directory in your D drive. You can not assign a destination file name the name of an existing directory.
For your code to work, you should change the following code:

Private Sub cmdSend_Click()
           FName = txbFileName.Text
           MsgBox SourceFile & Chr(10) & Destination
           FileCopy SourceFile, Destination
End Sub

TO:

Private Sub cmdSend_Click()
           FName = txbFileName.Text
           Destination = Destination & "\Myfile.xxx"
           MsgBox SourceFile & Chr(10) & Destination
           FileCopy SourceFile, Destination
End Sub

where "\Myfile.xxx" is the full path/name of your destination file that will be created (copied into).
0
 
LVL 7

Expert Comment

by:kamall
ID: 1443401
Wah Hoo!  Finally something copied!  But, I still have the problem of the name of the file.  How do I make sure the file name stays the same?

When I tried:  Destination = Destination & "\FName" it named the file FName.

thanks so much.  I am new to this, but you've probably guessed that by now.  


0
 

Author Comment

by:LClos
ID: 1443402
Its as easy as that:
Modify the
   Destination = Destination & "\Myfile.xxx"
To
   Destination = Destination & "\" & File1.FileName
0
 
LVL 7

Expert Comment

by:kamall
ID: 1443403
Excellent.  I am very appreciative of the help this person gave me.  They were very patient throughout the process.  I am very new to this and sometimes, the smallest of things will trip me up.  This person made it possible for me to go home on time for a change and not think about this problem in the middle of the night.
0
 

Author Comment

by:LClos
ID: 1443404
I am very glad for solving your problem and for your feeling LClos.
Have a nice day.
Best regards
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question