Avatar billede palund Nybegynder
26. november 2014 - 23:11 Der er 5 kommentarer

Upload og download af filer til access database

Jeg benytter følgende visual basic kode til at gemme f.eks et worddokument i en access database (2007):


    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim rstChld As DAO.Recordset2
    Dim fldAttach As DAO.Field2
   
    Set db = CurrentDb
    Set rst = db.OpenRecordset("tabFiler")
    rst.AddNew
   
        Set rstChld = rst.Fields("Fil").Value
       
        rstChld.AddNew
            Set fldAttach = rstChld.Fields("FileData")
            fldAttach.LoadFromFile Fil
        rstChld.Update
        rstChld.Close
       
    rst.Update
    rst.Close


Jeg vil imidlertid gerne have fat i filen igen i forbindelse med afsendelse af e-mails fra databasen. Hertil benytter jeg følgende kode:

    Set oLook = CreateObject("Outlook.Application")
    Set oMail = oLook.createitem(0)
   
    Set rsf = CurrentDb.OpenRecordset("SELECT * FROM tabFiler" + " WHERE " + "Nr=" + CStr(600))
    Fil = rsd!Fil
    With oMail
        .To = Til
        .body = Tekst
        .Subject = Emne
        .Attachments.Add (Fil)
        .Send
    End With

Jeg har derfor brug for at få fat i filen igen i sætningen .Attachments.Add (Fil)

mvh
Avatar billede terry Ekspert
27. november 2014 - 08:35 #1
Not a good idea to save files in the database, they fill far too much.
Better to just have the path/filename in the dB, this would also solve your problem on attaching the file to an email.
Avatar billede terry Ekspert
27. november 2014 - 08:44 #2
I cant recall ever using the LoadFromFile method but it seems there is another method SaveToFile you should be able to use.
http://msdn.microsoft.com/en-us/library/office/ff191852(v=office.15).aspx
Avatar billede palund Nybegynder
27. november 2014 - 10:34 #3
Thank you for your answer
I agree that the files will fill up in the database. On the other hand if a fil is deleted or the filename is changed you cannot access it anymore.

The solution you recommend will as I can see it restore the file to the disk and then you can use the .Attachments.Add (Fil)sentense with Fil as the name af the file just retored to the disk.

But maybe there is another way to get the file in the database without making the restore?
Avatar billede bvirk Guru
27. november 2014 - 11:03 #4
Vedr.#1 - det med at filer fylder mere i databasen galdt nok så meget OLE anvendelsen.
Fra:
https://support.office.com/en-us/article/Attach-files-and-graphics-to-the-records-in-your-database-d40a09ad-a753-4a14-9161-7f15baad6dbd

"* Access will compress your attached files unless those files are compressed natively. For example, JPEG files are compressed by the graphics program that created them, so Access does not compress them."
Avatar billede terry Ekspert
27. november 2014 - 11:44 #5
I agree that using OLE increased original size of files, using attachments is a much better method but it still fills more than just a path/filename.

I personally would still just store path/filename, of course there is a risk of the file being deleted/renamed but then there is also a risk of your dB being corrupted. So hopefully you make backups regularly :-)


"But maybe there is another way to get the file in the database without making the restore?"

To attach the file to the email I am quite sure you will need to, no matter which method you used to get file into dB, but I may be wrong.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester