0

I have some code that will insert attachments to a newly added record in a database:

If Attachfile = True Then
            Me.Job_RecordsTableAdapter.Fill(Me.Database_Job_RecordsDataSet.Job_Records)
            ' Form1.JobRecordsBindingSource.AddNew()

            Me.DataGridView1.Sort(Me.DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Descending)

            Dim fd As OpenFileDialog = New OpenFileDialog()
            Dim strFileName As String

            fd.Title = "Open File Dialog"
            fd.InitialDirectory = "C:\"
            fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
            fd.FilterIndex = 2
            fd.RestoreDirectory = True
            fd.Multiselect = True


            If fd.ShowDialog() = DialogResult.OK Then



                For Each file As String In fd.FileNames

                    strFileName = file

                    Dim dbe As New DBEngine
                    Dim db As Database = dbe.OpenDatabase(Fpath)
                    Dim rstRecord As Recordset = db.OpenRecordset( _
                            "SELECT * FROM [Job Records] WHERE [Job Record]=" & DataGridView1.Rows(0).Cells(0).Value, _
                            RecordsetTypeEnum.dbOpenDynaset)
                    rstRecord.Edit()
                    Dim rstAttachments As Recordset2 = rstRecord.Fields("Attachments").Value
                    rstAttachments.AddNew()
                    Dim AttachmentData As Field2 = rstAttachments.Fields("FileData")
                    AttachmentData.LoadFromFile(strFileName)
                    rstAttachments.Update()
                    rstAttachments.Close()
                    rstRecord.Update()
                    rstRecord.Close()
                    db.Close()

                Next
            End If
        End If

This code works perfectly for what I need it to do. But I need some code to do the reverse and allow me to open the files from the database, in a very similar fashion to how Access will let you open the files if you double click on the attachment field.

I have searched and not really found any examples of this, everyone either links to a microsoft page which has been removed, or gives VBA code for inside access to dump all the attachments, which isn't what I want.

Does anyone have any ideas?

LBPLC
  • 1,570
  • 3
  • 27
  • 51

1 Answers1

1

To retrieve an attachment you want to use the .SaveToFile method of an Access DAO Field2 object, similar to the .LoadFromFile method in your question's example code. See my other answer here for details.

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Thanks, however I require multiple files to be viewed, not really saved, and a pop up window to select which file in the database entry you want to open. Its starting to look like I might have to open the access database to do this. – LBPLC Sep 29 '15 at 06:07