Full Version: Attache PDF to a file
UtterAccess Forums > Microsoft® Access > Access Forms
sacha90
Hello All
Is there way to attache/Link a PDF to a spesific record (let me explain a bit more)
After entering arecord i will scan the related document (example an invoice), the i would like to link that scan document to the record (and so on) for other records enetered
freakazeud
Hi,
sure...you can use this API code to browse to the file. It will return the full string path and name to the external file. You can then use that to populate a control on your form which is bound to a text datatype table field. If you want to open the file later on at runtime then either use the Shell(), FollowHyerplink(), or ShellExecute() methods.
Access 2007 will handle this much nicer with the new attachment data type which enables you to actually embed and attach multiple files to one record. For now you need to implement a workaround as earlier mentioned.
HTH
Good luck
sacha90
Thanks freakazeud

Maybe like me you keep sample database (that I have found or peope like you provided me) just for the future

might you and example for this one (please)

Edited by: sacha90 on Wed Nov 22 14:07:55 EST 2006.
Edited by: sacha90 on Wed Nov 22 14:10:22 EST 2006.
freakazeud
Hi,
Oput together a quick sample...check attached (it is in Acc 2000 format...you didn't specify what version you are using).
HTH
Good luck
sacha90
THATS IT MAN, THAT IS IT yayhandclap.gif notworthy.gif sad.gif
WILL KEEP THIS FOR THE FURTURE PROJECT I MIGHT HAVE THIS I HOW I LEARN, I HAVE LEARNED A LOT THIS WAY
AGAIN THANK YOU
freakazeud
You're welcome. thumbup.gif
Good luck on future projects!
Determined
Thanks freakazued!
That is just what I was also looking for, a great help!
freakazeud
You're welcome.
Glad it could help you, too.
Good luck on future projects!
swinkelman
Hello,
I have two questions regarding the code in your sample database. I am new to VB so I want to be sure I understand correctly.
1. Am I correct to think that doing it this way will not embed the file? I would like to follow the code, but need to be sure it won’t dramatically increase our file size.
2. How do I change the code so that it is looking for “All” file types? Right now it will only seek .pdf’s and there may be several types of files that we want to link to. The code currently has a line: strFilter = ahtAddFilterItem(strFilter, "*.PDF") Is is a simple modify to this line to look for all files?
Your help is greatly appreciated!
freakazeud
Hi,
yes you are correct. This sample will NOT embed external files but rather just store the full string path and name of the file in a text data type table field. This is the preferred method in all versions prior to Access 2007.
For the filter part just set the attribute to all files e.g.:
trFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
...
HTH
Good luck
swinkelman
Excellent! This is most helpful. Thanks for your quick response.
freakazeud
You're welcome.
Good luck on future projects!
swinkelman
Sorry, another question has developed...
Is there a way to upload a file through Access? Here is what we are trying to do...
We have a database for quotes done in Access and would like to have all supporting documents linked to the quote.
We want to be able to upload the documents so that the user doesn't have access to them once they are in a network directory. We only want them to be able to access the uploaded files only through the database.
Is there a way to do this? (Note that our database is not set up to be web based so we are limited).
freakazeud
Hi,
can you define "upload" a little more? Where do you want to move the files to and where from?
HTH
Good luck
swinkelman
We have several account managers that will create a document stored on their laptops and will need to be moved to a network directory that holds all our quote activity support documents. In addition, they may need to move other files already stored in a different network directory.
For security reasons, either they will not have access to directly to the quote activity folder or they will not know where the quote files are being stored.
We would like them to be able to open the Access database, and copy supporting quote documents into the quote activity folder. This will require a copy function that can be executed through Access. Obviously, the last part of this is to have access to the supporting documents within the Access environment which the earlier posts in this thread addresses.
So my question is, is there a way to create a control that would (1) drill down to a file to select it and (2) copy the file into our quote activity folder. I've seen this done 1,000 times on web based programs (i.e., uploading a picture into a photo gallery for public sharing). But our application is not web based.
HAs I'm defining our needs, I suppose it would be even better to have the upload ability as I described and at the same time, generate the link to the uploaded file for future access (without having to browse to it as the "Attache PDF to a file" program works).
Wow, I'm not asking for much...just the world! :-) Seriously, I've done a lot of research on this & haven't been able to find anything like I need. Perhaps this isn't something that can be done in Access?
freakazeud
Hi,
the file moving wouldn't be a problem but the user would need access to the directories. You can use the internal FileCopy() method or the File system object's CopyFile() method. But as mentioned I doubt you can move a file to somewhere you do not have permissions to.
HTH
Good luck
JediHobbes
Time to resurrect the dead a bit on this one:) In my database, files are "attached" to comments, and you can have multiple comments per client. When a user "attaches" a file to a comment, it is copied to a directory on the server and renamed during the copy to the unique identifier of the comment it is attached to. When you click the open button, the file is copied into the MDE's path (CurrentProject.Path) and renamed during copy to the original name. It is here that I am stumped, as Shell borks out on me. Here's the code:

CODE

nbsp;   strNewFile = CurrentProject.Path + "\" + cmtFileBox.Value
    Shell strNewFile, vbMaximizedFocus


I'm getting a run-time error 5, "Invalid procedure call or argument". The value of strNewFile in my test is "C:\Documents and Settings\TestBed11\Desktop\test attachment.txt" - could the spaces in the path and file name be affecting Shell? ShellExecute doesn't seem to exist (might be missing a reference?), and I haven't tried FollowHyperlink, as I'm unsure as to whether I'd need to change my path to file://, replace the spaces with %20, and change all the back-slashes to forward slashes.

A final thing is this: according to the help file, Shell is run asynchronously, so does it stand that I cannot have my "Kill strNewFile" after the Shell command? I can always have a cleanup routine on the form load event that wipes out all files outside of a predetermined list (MDE, ICO and other support files) from the CurrentProject.Path, unless there is a smarter way? One thing I *did* think of is building a BAT file that launches the document and deletes the file (BAT files execute synchronously) and then using Shell to launch the BAT (would never need to cleanup the bat, as it's a minimal size and could merely be overwritten each time an attachment is opened). Bit overcomplicated, but it'd probably work. Suggestions?
JediHobbes
I implemented my batch file "solution" above. For those that are interested, here's my code to copy the file across from the network drive (note: uses CopyFileProgress from this post) and rename it during copy, create a batch file (and delete it if it exists, using Allen Browne's FileExists() function at his site) and then silently execute the batch file. The batch file, in turn, launches the now-renamed attachment, and then deletes it after the associated program has quit. Note: this uses VBA, a small piece of WScript, and BAT scripting - three languages in one solution is not an elegant solution, but it works, is unobtrusive and is invisible to the user.
First, I created this Windows Script (comes with XP, which is the only environment I roll out to with this database) to silently execute the batch file, and saved it as "iexec.vbs" in the same network directory as the attachments:
CODE
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

Othen wrote this for my "Open Attachment" Click event handler:
CODE
Dim NewFile As String
Dim BATHandle As Integer
   If Len(cmtFileBox.Value) > 1 Then
    NewFile = CurrentProject.Path + "\" + cmtFileBox.Value
    CopyFileProgress AttachDir + CStr(cmtIDBox.Value), NewFile
    If FileExists(CurrentProject.Path + "\openattach.bat") Then Kill CurrentProject.Path + "\openattach.bat"
    BATHandle = FreeFile
    Open CurrentProject.Path + "\openattach.bat" For Output As #BATHandle
     Print #BATHandle, "@echo off"
     Print #BATHandle, """" + NewFile + """"
     Print #BATHandle, "DEL /F /Q " + """" + NewFile + """"
    Close #BATHandle
    Shell "wscript.exe " + """" + AttachDir + "iexec.vbs" + """" + " " + """" + CurrentProject.Path + "\openattach.bat" + """"
   Else
    MsgBox "There is no file attached to this comment.", , "Error"
   End If

Like I said - it's not pretty, but it works:)
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.