Solved

copying file from one location to another using vb code

Posted on 2001-06-12
10
1,194 Views
Last Modified: 2007-11-27
'I basically want to copy a file to another location using vb code..... yes very simple but I cant get
it to work.

'Here is so code I am currently using.. Im storing the file name and location in a string and was expecting
to copy this to another location.

Public function CopyFile()

Dim strOldFile As String
Dim strFileDest As String

With frmAssessment.dlgImage
   .FileName = ""
   .ShowOpen
End With

strFileDest = frmAssessment.dlgImage.FileName

FileSystemObject.CopyFile strFileDest, "c:\database\"

Call RenameFile(strOldFileName, strNewFileName)  

End function

'If you have anyother ideas how I can copy a file to a new location plz reply. The File system object bring up a error message (object required) error 424'  
0
Comment
Question by:Yakeni
10 Comments
 
LVL 3

Accepted Solution

by:
sombell earned 50 total points
ID: 6180754
you need to declare the filesystems object.

try

dim fso as new filesystemobject
fso.copyfile strFileDest,"c:\database\"

Hope this helps,

Sombell
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6180756
Use Copyfile in VB:

CopyFile source, destination[, overwrite]

Just

CopyFile source, destination

0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6180758
I mean you don't need to declare filesystemobject
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:nutwiss
ID: 6180769
Yakeni, yes, as sombell points out, the 'FileSystemObject' is an object like any other, you need to explicitly create it, it is not an intrinsicly global object in VB (such as 'Debug' or 'VB' or 'Printer')
0
 
LVL 3

Expert Comment

by:sombell
ID: 6180773
what CopyFile in VB ? what copyfile would that be ?

This code:

Private Sub Form_Load()
copyfile "c:\test.doc", "c:\test1.doc"
End Sub

fails at compile time.
0
 
LVL 1

Expert Comment

by:thuannc
ID: 6180782
You can try FileCopy function in FileSystem class (build in VB)
Syntax:
FileCopy SrcPath,DstPath
sample:
FileCopy "C:\test.txt","c:\Data"
Some related usefull function in that class
Name --> remname file
Dir --> navigate files and folders
Kill --> delete file
....
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6180799
ya, it's my careless, i mean "FileCopy" as thuannc posted. Apologies.
0
 

Author Comment

by:Yakeni
ID: 6180891
Thanks for that> I didnt know you had to declare a filesystemobject. Your code was close.

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
0
 
LVL 3

Expert Comment

by:sombell
ID: 6180930
actually, you dont need to do that....

if you add the reference to the microsoft scripting runtime to the project then

Dim fso as new FileSystemObject
will work, and then you dont need the CreateObject.

Its more efficient to do it this way as it uses Earlu Binding.

CreateObject uses Late Binding

But both will work.

Regards.

Eombell
0
 
LVL 3

Expert Comment

by:sombell
ID: 6180939
I had assumed you already had the reference included.

(oh, and thats Sombell) :o)

happy to have helped
0

Featured Post

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.

Question has a verified solution.

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

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

685 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