save file attachment (.htm extension) from access table (1 Viewer)

murray83

Games Collector
Local time
Today, 21:15
Joined
Mar 31, 2017
Messages
728
OK i give up, iv been going round google and here looking for and at all the way to save an attachment from a table which its attached to, but most of the time there for images

and i'm not that smart on all the syntax like rs this and etc, so could some one please point me to some easy dummy guide on how to save a file, all i have is one attachment and just want it to save to default location on the c drive

so this is some pusdeo code so i know it wont work like i have written out below but this is what i would like it to do

Code:
Private Sub cmdCredits_Click()


dim FileName As String 

'code here to save the html for credits to place on the PC so the credits will scroll good !!
DoCmd.RunSQL SELECT Field1 FROM table WebAttachment 
SAVE  file to = "C:\Users\[Curent User]\Credits\("FileName")

Me.Visible = False
DoCmd.OpenForm "html_Credits"
End Sub

then once that happens the attached copy of credits .htm file has been saved locally and then the form opens and the web form will show the credits
 

Isaac

Lifelong Learner
Local time
Today, 13:15
Joined
Mar 14, 2017
Messages
8,777
This is aircode but I believe this is generically how it's done, although I don't personally use Attachments type...I've heard too many bad things about them.

Aircode untested and may contain a mistake..

Code:
dim rsMain as dao.recordset
dim rsFile as dao.recordset
set rsMain = currentdb.openrecordset("select attachment column from table")
set rsFile = rsmain.fields("FileData")
rsFile.SavetoFile "path"
 

Isaac

Lifelong Learner
Local time
Today, 13:15
Joined
Mar 14, 2017
Messages
8,777
It looks like I may have that recordset a little bit backwards. Have a look here - it's pretty easy to follow, looks like
 

Isaac

Lifelong Learner
Local time
Today, 13:15
Joined
Mar 14, 2017
Messages
8,777
ok what is the name of your table, and the column that contains the attachment? is it just one record?
 

murray83

Games Collector
Local time
Today, 21:15
Joined
Mar 31, 2017
Messages
728
ok what is the name of your table, and the column that contains the attachment? is it just one record?
cheers

but i have tried again with a bit more thinking on my behalf and have no errors but then also have no saved file, boooo .

attached is picture of the table, in design view its called field1 which have also attached

again cheers

Code:
Private Sub cmdCredits_Click()
        Dim strPath As String
        Dim strPattern As String
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset2
        Dim rsA As DAO.Recordset2
        Dim fld As DAO.Field2
        Dim strFullPath As String
        
        strPath = "C:\Users\sup001.d002\Desktop\New folder"
        strPattern = ".htm"
        
        'Get the database, recordset, and attachment field
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset("WebAttachment")
        Set fld = rst("Field1")
        
        'Navigate through the table
        Do While Not rst.EOF
        
            'Get the recordset for the Attachments field
            Set rsA = fld.Value
            
            'Save all attachments in the field
            Do While Not rsA.EOF
                If rsA("FileName") Like strPattern Then
                    strFullPath = strPath & "\" & rsA("FileName")
                    
                    'Make sure the file does not exist and save
                    If Dir(strFullPath) = "" Then
                        rsA("FileData").SaveToFile strFullPath
                    End If
                    
                    'Increment the number of files saved
                    SaveAttachments = SaveAttachments + 1
                End If
                
                'Next attachment
                rsA.MoveNext
            Loop
            rsA.Close
            
            'Next record
            rst.MoveNext
        Loop
        
        rst.Close
        dbs.Close
        
        Set fld = Nothing
        Set rsA = Nothing
        Set rst = Nothing
        Set dbs = Nothing
 





Me.Visible = False
DoCmd.OpenForm "html_Credits"


End Sub
 

Attachments

  • attachment.png
    attachment.png
    5.1 KB · Views: 82
  • design view.png
    design view.png
    5.6 KB · Views: 75
  • what is attached.png
    what is attached.png
    17.3 KB · Views: 64

June7

AWF VIP
Local time
Today, 12:15
Joined
Mar 9, 2014
Messages
5,466
LIKE needs wildcard to be useful, otherwise might as well use = sign.

Include in the variable:

strPattern = "*.htm"

or concatenate

If rsA("FileName") Like "*" & strPattern Then

or use

If InStr(rsA("FileName"), strPattern) > 0 Then
 
Last edited:

Users who are viewing this thread

Top Bottom