Solved

copying file from one location to another using vb code

Posted on 2001-06-12
10
1,186 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
Comment Utility
you need to declare the filesystems object.

try

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

Hope this helps,

Sombell
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Use Copyfile in VB:

CopyFile source, destination[, overwrite]

Just

CopyFile source, destination

0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
I mean you don't need to declare filesystemobject
0
 
LVL 4

Expert Comment

by:nutwiss
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:thuannc
Comment Utility
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 49

Expert Comment

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

Author Comment

by:Yakeni
Comment Utility
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
Comment Utility
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
Comment Utility
I had assumed you already had the reference included.

(oh, and thats Sombell) :o)

happy to have helped
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now