Copying a file to a certain folder.

  • Thread starter Thread starter Dr. Hung
  • Start date Start date
D

Dr. Hung

I dont really know the best way to go about this. I have a button
called "add Image" which opens up a browse window where the user can
pick out the photo he wants to upload. I need this photo file then to
be copied and pasted to a folder, "Spares Photo Folder", which is on
the same drive as the database is saved so that the photo can be viewed
from different computers.

This is the code i have at the moment, but it allows a link to be made
to a photo on some computers desktop and therefore this wouldn't be
able to be viewed by other users on the intranet.

"Pic1" is an ole object type.

Private Sub Add_Image_Click()
On Error GoTo Err_Add_Image_Click

Me.Pic1.SetFocus
SendKeys "%IO", False
SendKeys "%FB", True
Me.Done.SetFocus

Exit_Add_Image_Click:
Exit Sub

Err_Add_Image_Click:
MsgBox Err.Description
Resume Exit_Add_Image_Click

End Sub
 
Hi Dr. Hung,

instead of storing an OLE image in the db, why not just store the path
and filename in a text field? Storing OLE images will make your
database bloat very quickly.

There is code you can use to render the images when you need them

once you have the filename, you can do this:

FileCopy source, destination

be sure to include the path on source and destination


Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 
When iuse the line of code:

FileCopy source, destination

What code should I use to copy the source name automatically into
"source" in the line above. I can set the destination easily enough.

I.e. how do i obtain the source address of a selected photo file?
 
Hi Dr. Hung,

do you have Browse code to navigate to your photo to pick it? That is
an easy time to determine the source.

"how do i obtain the source address of a selected photo file?"

how are you selecting it? how is it sored?

Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 
It is a .jpg file. At the moment i am inserting the link to the
pictures location. But this won't work if some one else tries to access
the link from a different computer. So i need the picture file to be
copied into a new folder called "spares photos" which will be stored on
the same drive as the database.

Currently my code is:

SendKeys "%IO", False
SendKeys "%FB", True

All this does is opens the insert object screen and automatically then
goes to the brouse screen. This code is behind my add image button.

What way should i try to do this??
 
Definitely stop using SendKeys!

VBA has built-in file-handling commands. The FileCopy statement will copy a
file from one location to another:

Syntax

FileCopy source, destination

The FileCopy statement syntax has these named arguments:

Part Description
source String expression that specifies the name of the file to be
copied. The source may include directory or folder, and drive.
destination String expression that specifies the target file name. The
destination may include directory or folder, and drive.

There's also the Name statement that will rename a file, directory or
folder:

Syntax

Name oldpathname As newpathname

The Name statement syntax has these parts:

Part Description
oldpathname String expression that specifies the existing file name and
location-may include directory or folder, and drive.
newpathname String expression that specifies the new file name and
location-may include directory or folder, and drive. The file name specified
by newpathname can't already exist.

Remarks

Both newpathname and oldpathname must be on the same drive. If the path in
newpathname exists and is different from the path in oldpathname, the Name
statement moves the file to the new directory or folder and renames the
file, if necessary. If newpathname and oldpathname have different paths and
the same file name, Name moves the file to the new location and leaves the
file name unchanged. Using Name, you can move a file from one directory or
folder to another, but you can't move a directory or folder.

If the folder doesn't already exist (you can check using Dir(foldername,
vbDirectory): that will be a zero-length string if foldername doesn't
exist), you can use the MkDir statement to create the path.
 
thanks for adding on, Doug!

Dr. Hung,

if the pictures re going to be in your database directory, you can use
this to get the path:

currentproject.path

Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 
Hi Crystal,

This is the code i am using at the moment. The error is occuring on the
"FileCopy Source,destination line". The error is saying

"Run-time error '75'
Path/File access error"


Private Sub Upload_Click()
On Error GoTo Err_Upload_Click
Dim Source As String, destination As String

Me.Pic1.SetFocus
SendKeys "%IO", False
SendKeys "%FB", True
Source = CurrentProject.Path
destination = "P:\My Documents\Original\Photos"
FileCopy Source, destination
Me.Done.SetFocus

Exit_Upload_Click:
Exit Sub

Err_Upload_Click:
MsgBox Err.Description
Resume Exit_Upload_Click

End Sub

Could you shed some light on my problem...
Thanks
 
Hi Dr. Hung,

as Doug said, you want to get away from using SendKeys...

what is the logic behind what you are doing with:

SendKeys "%IO", False
SendKeys "%FB", True

Let us help you find a VBA equivalent

in your code, Source and destination are paths ... they need to be Path
AND filename


Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 
As you can see i am only new to access from the state of my code.

Here is what i'm trying to achieve in a nutshell with this form.

I have this form set up at the moment with an oleobject control box in
it. I also have an upload button. When i press the upload button what i
want to happen is the user is automatically brought to a brouse window
to select the photo that he wishes to upload. This photo needs then to
be copied automatically to a folder on the same drive as the database
is stored.

I dont know really where to start with it..
Could you show me the best way to go about this..

Regards
Martin
 
You're not providing the name of the file to copy: since you've set it to
CurrentProject.Path, Source is strictly a folder name, not a file name.
 
Hi donald,
I dont know how to use the code you have given me. Do i leave in the
code i had already or do i make changes to it also. could you explain
what each line of the code does please so i can try and understand what
is happening.

Regards Martin
 
Hi Martin,

to Browse to a file, you can call the standard Windows File Open/Save
dialog box

here is a link to some code by Ken Getz
http://www.mvps.org/access/api/api0001.htm

If the directions on how to use it aren't clear, post back with your
questions

Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 
Hi Donald,

I still haven't got to the bottom of my problem. I just haven't yet got
the experience of writing such code. I would greatly appreciate if you
code help me by writing the code as you offered.

If you need any more details than previously provided above, just let
me know.

Kind Regards,
Martin
 
Hi Martin,

make a new general module with the Ken Getz's code (the lines with blue
shading)

How to Create a General Module

1. from the databae window, click on the Module tab
2. click on the NEW command button
3. type (or paste) the code in

once the code is in the module sheet, do
Debug,Compile from the menu

if there are no syntax/reference errors, nothing will appear to happen
-- this is good ;)


on the code for your Browse File button...

~~~~~~~~~~~~~~~~~

Dim strFilter As String
Dim strInputFileName as string

strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.XLS)", "*.XLS")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

if len(trim(strInputFileName)) = 0 then
'user did not pick anything
exit sub
end if

'now the code to do whatever you want with the file

~~~~~~~~~~~~~~~~~

change strFilter to be whatever you want. ie:

strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")

change the DialogTitle parameter...

DialogTitle:="Please tell Martin what file you want..."



Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
 

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

Back
Top