Hi Every body
I would apreciate if any one would be able to help me do this. I am quet sure this is posible though not siple.I have a database with just one table named Table1. It has two fields an "ID' field and 'Files' (wich is the name of the the attachment field). I have build a form based on that table and on the Forms 'ID's on click event I have put a cord
to open the attachment. It opens the attachment just by one click on the 'ID' field. But the problem is it only opens the attachment file of the first record even if i click on the second records 'ID'. Can some Body help me make it open The attachment for the record's 'ID' Thats being clicked. here is the cord its on an access blog.
Can some body please help me. I mainly use macros and very new to VBA.
Thanks
Private Sub ID_CLICK()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Const strTable = "Table1" '<- Replace this with your table with attachment
Const strField = "Files" '<- Replace this with the fieldname of your attachment
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strTable)
'rst.MoveNext ' Uncomment this line to go to the 2nd row in the Table.
OpenFirstAttachmentAsTempFile rst, strField
rst.Close
End Sub
Public Function OpenFirstAttachmentAsTempFile(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String) As String
Dim rstChild As DAO.Recordset2
Dim fldAttach As DAO.Field2
Dim strFilePath As String
Dim strTempDir As String
strTempDir = Environ("Temp") ' Get the Temp directory from the environment variable.
If Right(strTempDir, 1) <> "\" Then strTempDir = strTempDir & "\" ' Make sure the path always ends with a backslash.
Set rstChild = rstCurrent.Fields(strFieldName).Value ' the .Value for a complex field returns the underlying recordset.
strFilePath = strTempDir & rstChild.Fields("FileName").Value ' Append the name of the first (and only) attached file to temp dir.
If Dir(strFilePath) <> "" Then ' the file already exists--delete it first.
VBA.SetAttr strFilePath, vbNormal ' remove any file attributes (e.g. read-only) that would block the kill command.
VBA.Kill strFilePath ' delete the file.
End If
Set fldAttach = rstChild.Fields("FileData") ' The binary data of the file.
fldAttach.SaveToFile strFilePath
rstChild.Close ' cleanup
VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus ' Use Windows Explorer to launch the file.
End Function 'OpenFirstAttachmentAsTempFile
Thank you