code to open attachment (1 Viewer)

aspen

Registered User.
Local time
Today, 05:29
Joined
Apr 4, 2012
Messages
24
Dear smig

Thanks
But i regret for being not familar with Vba .The macro builder is great and familar with
vba i started recently and my field is medicine.

The key field = ID
Name of the file = Text field
Binary data =? did you mean the Attachment field?

So Key field -'ID'- I have
Name of the file -'File name' (Text field) -I would create
Binary field 'Files' - I have (Its my attachment field name)

But Can you help me Changing the code and guide me if Field data type as i mentioned above is what you expected.

If And I should'nt say what i am about to say next, because as per forum tips Giving home work is unhealthy for the thread and so far i have been taking all kinds of precautions. so my friend please if its possible Make available a sample data base for us who will use the forum with too little vba knowledge

Else If " Please adjust the code to those three fields ( provided the fields are ok).
It would be a great pleasure if you wold send tommorow.
Its midnight to maldives
sweet dreams
Thank you in advance
 

aspen

Registered User.
Local time
Today, 05:29
Joined
Apr 4, 2012
Messages
24
Good day Every one
I Have a database with One table "Table1" and aform based on thar table. The table has two fields.
'ID' -Autonumber
'Files' -An attacment field.
I have a form build on that table
Can some one help me with a code to open the Attachment file by clicking on the 'ID" field.(each record has only one attach ment ). The files are stored in the database only.Not in a seperate folder or location in the PC. and I dont want use hyperlink metthod.
I would apreciate if some would help me with a code
Thanks

I am very new to vba. I wish I new enough to understand. so for those who may understand here are some tips I recieved as help
please see

Private Sub tbxFileName_Click()
Dim strFilePath
With Me.RecordsetClone
.Bookmark = Me.Bookmark
strFilePath = "C:\Temp\" & Me.tbxFileName
If Dir(strFilePath) <> "" Then VBA.Kill strFilePath
![Table1.MyDocs.FileData].SaveToFile strFilePath
VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus
End With
End Sub

another one

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim fldAttach As DAO.Field
Dim strTempDir As String
Dim strFileName as string
Dim strFilePath 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 dbs = CurrentDb
Set rst = dbs.OpenRecordset("Select * From [table1] Where [ID] = " & Me.ID) ' -- Table1 hold the files. Me.ID is the ID selected on the form
rst.MoveFirst
strFileName = rst.Fields("FileName") ' -- The FileName field in Table1 hold the name of the file
strFilePath = strTempDir & strFileName ' -- Append the name of the attached file to temp dir.
call KillFileFromTempFolder(strFilePath) ' -- Kill an old file if exist
Set fldAttach = rst.Fields("FileData") ' -- FileData field in Table1 hold The binary data of the file.
fldAttach.SaveToFile strFilePath ' -- Save the file
rst.Close ' -- close the rs and clean
Set rst = Nothing

VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus ' Use Windows Explorer to launch the file.
-------------------
Public sub KillFile(strFilePath as string)
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
end sub

another
rivate 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
 

aspen

Registered User.
Local time
Today, 05:29
Joined
Apr 4, 2012
Messages
24
Hi Smig

Thanks for your cooperation

I got the code from microsoft access forum

here is it for any one who might need in the future

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("SELECT * FROM Table1 WHERE ID=" & Me.ID)
'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

So lets asume its solved and

many many thanks
 

Users who are viewing this thread

Top Bottom