Solved

copying file from one location to another using vb code

Posted on 2001-06-12
10
1,195 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
[X]
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
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 52

Expert Comment

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

CopyFile source, destination[, overwrite]

Just

CopyFile source, destination

0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6180758
I mean you don't need to declare filesystemobject
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 52

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

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!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

732 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