Excel VBA String (SQL) to Retrieve and Insert an Image Saved As Attachment in Access

samedi 2 mai 2015

Hi all and thanks for taking the time to even click this thread

I have a field [Screenshot] in an access table that is the data type "Attachment"

In this field I have saved a single screenshot image (.png or .jpeg)

What I want to do is insert that screenshot into an Excel spreadsheet based on a criteria ("ID") that is in Excel, sheet1 "A1" ...

Below is the VBA and therein the SQL string I use to query the Access Database; however, I can only get it to return the text (the screenshot's name in Access);

Code:

Sub GrabSS

loc = "C:\Database.accdb"

strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=" & loc & ""

IDString= """" & ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value & """"
strSQL = "SELECT [Screenshot] From [Tbl1] Where [ID] IN (" & IDString & ")"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
   
cn.Open strConnection
rs.Open strSql, cn, 3, 4

 With ThisWorkbook.Worksheets("Sheet2")
        For i = 0 To rs.Fields.Count - 1
            .Cells(1, i + 1) = rs.Fields(i).Name
        Next
        rs.MoveFirst
        .Cells(1, 1).CopyFromRecordset rs
  End With

End Sub

Basically, I'm wondering if this is even possible with the Attachment datatype; or whether I should be using a different datatype in Access to perform what I'm trying to do.

Thanks for any advice or help you can throw my way - :banghead:
Excel VBA String (SQL) to Retrieve and Insert an Image Saved As Attachment in Access

0 commentaires:

Enregistrer un commentaire

Labels