chovis78
asked on
How to extract images from the data type attachment
Is it possible to extract the images or pictures that are in the attachement data type to a folder and give it a name?
ASKER
How are you executing this code?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
This is the reason why many developers don't store images IN the DB, they simply link to them.
At first, it seems cool when he images are in the DB, but eventually someone wants to "save" the image to disk.
If the image was linked, this would not be an issue because the image was never in the database to begin with...
Thus it never has to be extracted
<Climbing down from Soapbox...>
;-)
"extract the images or pictures that are in the attachment data type to a folder and give it a name?"
Give it a name when?
The extracted file will already be named what the original file was named.
Do you want to "RE"-Name it?
Do you want it to stop and ask you for a name for every file?
...or do you want some kind of incremental naming system?
At first you say: "PictureS", (Plural), then you say: "Give IT a name". (Singular)
So are you trying to extract multiple files, or just one specific file?
FWIW, I also found that same code nugget and adapted it for my needs.
fyed's looks a bit more "refined" (being a function and all...), so I'll not embarrass myself by posting mine here.
:-(
JeffCoachman
At first, it seems cool when he images are in the DB, but eventually someone wants to "save" the image to disk.
If the image was linked, this would not be an issue because the image was never in the database to begin with...
Thus it never has to be extracted
<Climbing down from Soapbox...>
;-)
"extract the images or pictures that are in the attachment data type to a folder and give it a name?"
Give it a name when?
The extracted file will already be named what the original file was named.
Do you want to "RE"-Name it?
Do you want it to stop and ask you for a name for every file?
...or do you want some kind of incremental naming system?
At first you say: "PictureS", (Plural), then you say: "Give IT a name". (Singular)
So are you trying to extract multiple files, or just one specific file?
FWIW, I also found that same code nugget and adapted it for my needs.
fyed's looks a bit more "refined" (being a function and all...), so I'll not embarrass myself by posting mine here.
:-(
JeffCoachman
As you will note, the subroutine also accepts a "Criteria" string, like you would find in DLOOKUP, DMAX, DMIN, ...
so if you only want to extract an attachment from a single record, you can do so based on some criteria string.
As Jeff noted, I don't generally include attachments in my databases either, also preferring to store the path and filename, but when I distribute a new front end and need to send some files to go with it, I use this technique. Then I delete all the records from this table and configure the application to compact on close.
so if you only want to extract an attachment from a single record, you can do so based on some criteria string.
As Jeff noted, I don't generally include attachments in my databases either, also preferring to store the path and filename, but when I distribute a new front end and need to send some files to go with it, I use this technique. Then I delete all the records from this table and configure the application to compact on close.
ASKER
Ok that works. Thanks, but is there a way to rename the files to say the id that goes with all the picutures. The reason is so I can match them up easily to the records when I link them back properly. Yes I know linking is the way to go but this project started off small and somehow grew bigger than I thought it would. Also what are your thoughts on having pictures within a SQL table since 2008 has no size limit on the database?
I'm not actually sure how to get the filename of the attachment but I'm sure there is a property associated with it that you can retrieve.
In the absence of that, I would imagine that you could extract the file to a folder that contains no other files, then use the DIR() function to get the name of the file, then use the VBA Name statement to change the files name from the original text to the ID number of the record containing the attachment. If I were going to do this, I would probably add a field to the table to store the original name of the attachment, so that I could retrieve it with its original name at some point in the future.
In the absence of that, I would imagine that you could extract the file to a folder that contains no other files, then use the DIR() function to get the name of the file, then use the VBA Name statement to change the files name from the original text to the ID number of the record containing the attachment. If I were going to do this, I would probably add a field to the table to store the original name of the attachment, so that I could retrieve it with its original name at some point in the future.
ASKER
Is that all in that sample database you shared or is that something else?
What is that LoadAttachment code you have, is that to attach them back into the table?
What is that LoadAttachment code you have, is that to attach them back into the table?
>>Is that all in that sample database you shared or is that something else?
No, the sample I posted does not have anything that would do the renaming of the file based on an ID field. If I were going to do that, I would probably do it as part of the upload process. I would select the file using the FileOpenSave API, then copy it to a new name that looks like the ID value of the record I'm about to attach it to. I would then attach it to the record and store the original file name to a field in that record as well.
>>What is that LoadAttachment code you have, is that to attach them back into the table?
Yes, that is for loading an attachment, although I have to admit that I have not used it in quite a while.
No, the sample I posted does not have anything that would do the renaming of the file based on an ID field. If I were going to do that, I would probably do it as part of the upload process. I would select the file using the FileOpenSave API, then copy it to a new name that looks like the ID value of the record I'm about to attach it to. I would then attach it to the record and store the original file name to a field in that record as well.
>>What is that LoadAttachment code you have, is that to attach them back into the table?
Yes, that is for loading an attachment, although I have to admit that I have not used it in quite a while.
ASKER
I think your LoadAttachment code is missing the function GetOpenFile not sure if you still have that? Was going to try and see if that will work.
GetOpenFile is part of code in the File Open/Save code on the following site:
http://www.mvps.org/access/api/api0001.htm
http://www.mvps.org/access/api/api0001.htm
ASKER
Man this would work beautiful if I could get the pictures to load back in the table. Not sure what i am doing wrong.
ASKER
Ok i got it to extract like i want and can load, but it only loads one at a time and adds a new record. Do you know of a way to add all of them and say match the name of the file to a record and insert the picture in that specific record?
I'll try to take a look tonight.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Open in new window