Solved

VB5 FileCopy

Posted on 1998-05-07
14
705 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Acccess - Update Field in Table when report is printed 6 47
Saving history changes to sub form 4 48
Hide vba in gp 7 104
Dinamic report to Crosstab query 9 33
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

861 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