Why wont this code work

J

Josie

I am trying to copy any files, from 1 folder, whose names
commence with a 7 digit reference number on the form,
into a 2nd folder.
At filecopy I get a message Run time error 53 - file not
found.


Thanks in advance
Josie

Dim FromDir As String, ToDir As String
Dim Source As String, Target As String
Dim strFName As String


strFName = Left(Forms!Test![Ref], 7)

ToDir = DLookup("PropertyPath", "MyFirm") & "\" &
Forms!Property![txtRef]
FromDir = DLookup("PhotoPath", "MyFirm")

Source = Dir(FromDir & "\" & strFName & "*.jpg")
MsgBox Source - File name & folder OK

Target = ToDir & "\" & Source

MsgBox Target - File name & folder OK

Do While Source <> ""
FileCopy Source, Target

Source = Dir()
Loop
 
A

Alphonse Giambrone

Instead of copying the files within the Dir loop, copy all the file names
into an array, then loop the array to copy the files.
Basically, if you perform any other type of functionality other than
collecting the file names withing a Dir loop, it will no longer return the
expected result.
 
K

Ken Snell [MVP]

You must again preface Source with the path to Source. Dir returns only the
file name without the path. But FileCopy needs the full path for both "from"
and "to" arguments.

Source = FromDir & "\" & Dir(FromDir & "\" & strFName & "*.jpg")
 
J

Josie

Alphonse

Could you give me an code example of what you mean please

Thanks

Josie
-----Original Message-----
Instead of copying the files within the Dir loop, copy all the file names
into an array, then loop the array to copy the files.
Basically, if you perform any other type of functionality other than
collecting the file names withing a Dir loop, it will no longer return the
expected result.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


I am trying to copy any files, from 1 folder, whose names
commence with a 7 digit reference number on the form,
into a 2nd folder.
At filecopy I get a message Run time error 53 - file not
found.


Thanks in advance
Josie

Dim FromDir As String, ToDir As String
Dim Source As String, Target As String
Dim strFName As String


strFName = Left(Forms!Test![Ref], 7)

ToDir = DLookup("PropertyPath", "MyFirm") & "\" &
Forms!Property![txtRef]
FromDir = DLookup("PhotoPath", "MyFirm")

Source = Dir(FromDir & "\" & strFName & "*.jpg")
MsgBox Source - File name & folder OK

Target = ToDir & "\" & Source

MsgBox Target - File name & folder OK

Do While Source <> ""
FileCopy Source, Target

Source = Dir()
Loop


.
 
J

Josie

Ken

Thanks for your response. However if we use your code for
the source we get the same eror message.

On experimenting if we ammend the file copy line to
FileCopy FromDir & "\" & Source, Target

It does copy the first occurence of the file name but
does not loop to copy the rest

Any suggestions

Thanks in advance

Josie


-----Original Message-----
You must again preface Source with the path to Source. Dir returns only the
file name without the path. But FileCopy needs the full path for both "from"
and "to" arguments.

Source = FromDir & "\" & Dir(FromDir & "\" & strFName & "*.jpg")

--

Ken Snell
<MS ACCESS MVP>

I am trying to copy any files, from 1 folder, whose names
commence with a 7 digit reference number on the form,
into a 2nd folder.
At filecopy I get a message Run time error 53 - file not
found.


Thanks in advance
Josie

Dim FromDir As String, ToDir As String
Dim Source As String, Target As String
Dim strFName As String


strFName = Left(Forms!Test![Ref], 7)

ToDir = DLookup("PropertyPath", "MyFirm") & "\" &
Forms!Property![txtRef]
FromDir = DLookup("PhotoPath", "MyFirm")

Source = Dir(FromDir & "\" & strFName & "*.jpg")
MsgBox Source - File name & folder OK

Target = ToDir & "\" & Source

MsgBox Target - File name & folder OK

Do While Source <> ""
FileCopy Source, Target

Source = Dir()
Loop


.
 
K

Ken Snell [MVP]

You are not resetting the value of Target within the Do loop. Thus, you just
keep copying each file on top of the first one.
 
J

Josie

Ken

What code would I need to do that?

Josie
-----Original Message-----
You are not resetting the value of Target within the Do loop. Thus, you just
keep copying each file on top of the first one.

--

Ken Snell
<MS ACCESS MVP>

I am trying to copy any files, from 1 folder, whose names
commence with a 7 digit reference number on the form,
into a 2nd folder.
At filecopy I get a message Run time error 53 - file not
found.


Thanks in advance
Josie

Dim FromDir As String, ToDir As String
Dim Source As String, Target As String
Dim strFName As String


strFName = Left(Forms!Test![Ref], 7)

ToDir = DLookup("PropertyPath", "MyFirm") & "\" &
Forms!Property![txtRef]
FromDir = DLookup("PhotoPath", "MyFirm")

Source = Dir(FromDir & "\" & strFName & "*.jpg")
MsgBox Source - File name & folder OK

Target = ToDir & "\" & Source

MsgBox Target - File name & folder OK

Do While Source <> ""
FileCopy Source, Target

Source = Dir()
Loop


.
 
K

Ken Snell [MVP]

Try this:


Dim FromDir As String, ToDir As String
Dim Source As String, Target As String
Dim strFName As String


strFName = Left(Forms!Test![Ref], 7)

ToDir = DLookup("PropertyPath", "MyFirm") & "\" &
Forms!Property![txtRef]
FromDir = DLookup("PhotoPath", "MyFirm")

Source = Dir(FromDir & "\" & strFName & "*.jpg")
MsgBox Source - File name & folder OK



Do While Source <> ""
Target = ToDir & "\" & Source
MsgBox Target - File name & folder OK
FileCopy FromDir & "\" & Source, Target

Source = Dir()
Loop
--

Ken Snell
<MS ACCESS MVP>

Josie said:
Ken

What code would I need to do that?

Josie
-----Original Message-----
You are not resetting the value of Target within the Do loop. Thus, you just
keep copying each file on top of the first one.

--

Ken Snell
<MS ACCESS MVP>

I am trying to copy any files, from 1 folder, whose names
commence with a 7 digit reference number on the form,
into a 2nd folder.
At filecopy I get a message Run time error 53 - file not
found.


Thanks in advance
Josie

Dim FromDir As String, ToDir As String
Dim Source As String, Target As String
Dim strFName As String


strFName = Left(Forms!Test![Ref], 7)

ToDir = DLookup("PropertyPath", "MyFirm") & "\" &
Forms!Property![txtRef]
FromDir = DLookup("PhotoPath", "MyFirm")

Source = Dir(FromDir & "\" & strFName & "*.jpg")
MsgBox Source - File name & folder OK

Target = ToDir & "\" & Source

MsgBox Target - File name & folder OK

Do While Source <> ""
FileCopy Source, Target

Source = Dir()
Loop


.
 
J

Josie

Ken

Many thanks - it works exactly as I want - many thanks

Josie
-----Original Message-----
Try this:


Dim FromDir As String, ToDir As String
Dim Source As String, Target As String
Dim strFName As String


strFName = Left(Forms!Test![Ref], 7)

ToDir = DLookup("PropertyPath", "MyFirm") & "\" &
Forms!Property![txtRef]
FromDir = DLookup("PhotoPath", "MyFirm")

Source = Dir(FromDir & "\" & strFName & "*.jpg")
MsgBox Source - File name & folder OK



Do While Source <> ""
Target = ToDir & "\" & Source
MsgBox Target - File name & folder OK
FileCopy FromDir & "\" & Source, Target

Source = Dir()
Loop
--

Ken Snell
<MS ACCESS MVP>

Ken

What code would I need to do that?

Josie
-----Original Message-----
You are not resetting the value of Target within the
Do
loop. Thus, you just
keep copying each file on top of the first one.

--

Ken Snell
<MS ACCESS MVP>

I am trying to copy any files, from 1 folder, whose names
commence with a 7 digit reference number on the form,
into a 2nd folder.
At filecopy I get a message Run time error 53 - file not
found.


Thanks in advance
Josie

Dim FromDir As String, ToDir As String
Dim Source As String, Target As String
Dim strFName As String


strFName = Left(Forms!Test![Ref], 7)

ToDir = DLookup("PropertyPath", "MyFirm") & "\" &
Forms!Property![txtRef]
FromDir = DLookup("PhotoPath", "MyFirm")

Source = Dir(FromDir & "\" & strFName & "*.jpg")
MsgBox Source - File name & folder OK

Target = ToDir & "\" & Source

MsgBox Target - File name & folder OK

Do While Source <> ""
FileCopy Source, Target

Source = Dir()
Loop


.


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top