I would like to insert a picture (.jpg,.png) into an Excel sheet from an attachment' field from Access.
My current code is something like this:
Sub InsertPicFromAccessDB()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
con.ConnectionString = ConStrAccess
con.Open
With rs
.ActiveConnection = con
.Source = "SELECT ProdutoFoto FROM tblProduto WHERE ProdutoFoto=2163150;"
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.Open
End With
shStockNovo.Range("A57").Value = rs.Fields(0).Value
rs.Close
con.Close
End Sub
It doesn't upload the image from the DB.
I already searched for other alternatives and one sugestion was using the ADODB.Sream. But it gave me an error on the "ADODB.Stream.Write" (with my research, no one could fix this problem). It was the "Run Time Error ‘3001’ Arguments are the wrong type, are out of acceptable range, or are in conflict with one another."
Code in Excel VBA to extract an image file from Access attachment field and save to folder then embed in Excel worksheet.
first set a reference for Microsoft DAO x.x Object Library or Microsoft Office x.x Access Database Engine Object Library
sample code
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset, rsP As Variant, strFile As String
Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase("C:\path\filename.accdb")
Set rs = db.OpenRecordset("SELECT ProdutoFoto FROM tblProduto WHERE ProdutoFoto=2163150")
Set rsP = rs.Fields("ProdutoFoto").Value
rsP.Fields("FileData").SaveToFile "C:\Path"
strFile = rsP.Fields("FileName")
shStockNovo.Range("A57").Select
shStockNovo.Pictures.Insert ("C:\path\" & strFile)