Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

copying file from one location to another using vb code

Posted on 2001-06-12
10
1,192 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 50

Expert Comment

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

CopyFile source, destination[, overwrite]

Just

CopyFile source, destination

0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6180758
I mean you don't need to declare filesystemobject
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 50

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

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…
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 …
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…
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…

839 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