Solved

copying file from one location to another using vb code

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

Expert Comment

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

CopyFile source, destination[, overwrite]

Just

CopyFile source, destination

0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6180758
I mean you don't need to declare filesystemobject
0
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

864 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

25 Experts available now in Live!

Get 1:1 Help Now