I know placing attachments in a database is risky and everyone says do not do it, however that is how the powers want it done. That being said, my listbox contains multiple fields and 1 of those shows there is an attachment in the database, however it does not allow to open that attachment because this is just a listbox of items. What I need is to doubleclick on that line item, which I have it selected already and on a doubleclick, open that attachment. Here is the code I have in my form and I get an error:
The error is on.........set rs = me.recordsetclone
It says: run time 7951 you entered an expression that has an invalid reference to the recordset clone property
VB.NET:
Private Sub ListQuality_DblClick(Cancel As Integer)
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
OpenFirstAttachmentAsTempFile rs, ".pdf"
End Sub
Public Sub OpenFirstAttachmentAsTempFile(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName 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 Sub
The error is on.........set rs = me.recordsetclone
It says: run time 7951 you entered an expression that has an invalid reference to the recordset clone property