open atachment with one click

aspen

Registered User.
Local time
Today, 02:46
Joined
Apr 4, 2012
Messages
24
Dear beloved members

I have a form based on a table containing two fields 'ID' and 'files' (an attachment field).Ihave build a form based on the table and each record contains one attachment (wmv. file) . can some one please help me with the coding To open the attachment file by clicking on the ID field of the form
say "Private Sub ID_Click()"

Thank you very much
 
Last edited:
I've found the FollowHyperlink method to be quite useful for opening files of various types. I don't know if it works for WMV's though.

Code:
Private Sub txtFullDocName_Click()
    
On Error GoTo Err_Proc

    Application.FollowHyperlink Me.txtFullDocName, , True

Exit_Proc:
    Exit Sub
Err_Proc:
    Select Case Err.Number
        Case 7971
            Resume Exit_Proc
        Case 490
            MsgBox "Make sure that the referenced document hasn't been moved.", vbOKOnly + vbInformation
            Resume Exit_Proc
        Case 94, 16388
            Resume Exit_Proc
        Case Else
            MsgBox Err.Number & "--" & Err.Description, vbCritical
            Resume Exit_Proc
    End Select
End Sub
 
Thanks Pat
I apreciate your willing ness to help
Can we make a code to open the file without using the hyper link field.
I mean the form has two fields, ID and Files (where files is the attachment field
So clicking on the ID field opens The attachment for each record
clicking on Record 1 ID opens record 1's attacment
record 2 id opens record 2's attachment
 
Did you try using the sample I posted? If you don't use the followhyperlink method, you'll need to know the name of the program that you want to open and open it specifically and pass in the name of the file you want to open. The followHyperlink method works with web pages and local files also. As long as some application is registerd on your computer to be the default for opening a specific file type, that program is executed. So, if your file is .xls, Excel gets opened, if it is .jpg, your photo application gets opened, etc.
 
Thanks Pat
Your hyper link method works fine. But Can we do it using two fields. The ID and an attacment field.
I tried the below code and when i click the ID it opens the attachment of the first record only. Even if i click the second record The first record is opened. Can you please help me change the code so it would open the attachment of the record thats being clicked. It would be a great help for many like me
Thank you
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)
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
 

Users who are viewing this thread

Back
Top Bottom