You can use your favorite social network to register or link an existing account:
Or use your email address to register without a social network:
Sign in with these social networks:
Or enter your username and password
Forgot your password?
Yes, please link my existing account with for quick, secure access.
No, I would like to create a new account with my profile information.
' Module level constants used in these examplesConst m_strFieldFileName As String = "FileName" ' The name of the attached fileConst m_strFieldFileType As String = "FileType" ' The attached file's extensionConst m_strFieldFileData As String = "FileData" ' The binary data of the file' -------------------------------------------------------------------------' Sub/Func : AddAttachment' Purpose : Saves the attachments at the current row of the open Recordset' Arguments: rstCurrent - The recordset open at the current row to save' : strFieldName - The name of the attachment field' : strFilePath - The full path to the file to attach' Comments : User must call .AddNew or .Edit on the incoming Recordset' : and then Recordset.Update when this returns to commit changes' -------------------------------------------------------------------------Sub AddAttachment(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String, ByVal strFilePath As String) Const CALLER = "AddAttachment" On Error GoTo AddAttachment_ErrorHandler Dim rstChild As DAO.Recordset2 Dim fldAttach As DAO.Field2 If Dir(strFilePath) = "" Then ' the specified file does not exist! MsgBox "The specified input file does not exist: " & vbCrLf & strFilePath, vbCritical, "File not found" Exit Sub End If Set rstChild = rstCurrent.Fields(strFieldName).Value ' the .Value for a complex field returns the underlying Recordset. rstChild.AddNew ' add a new row to the child Recordset Set fldAttach = rstChild.Fields(m_strFieldFileData) ' set the DAO.Field2 object to the field that holds the binary data. fldAttach.LoadFromFile strFilePath ' store the file's contents in the new row. rstChild.Update ' commit the new row. rstChild.Close ' close the child Recordset. Exit SubAddAttachment_ErrorHandler: 'Check for Run-time error '3820': (occurs if the file with the same name is already attached) 'You cannot enter that value because it duplicates an existing value in the multi-valued lookup or attachment field. 'Multi-valued lookup or attachment fields cannot contain duplicate values. Debug.Print "Error # " & Err.Number & " in " & CALLER & " : " & Err.Description If Err.Number <> 3820 Then MsgBox Err.Description, VbMsgBoxStyle.vbCritical, "Error # " & Err.Number & " in " & CALLER Debug.Assert False ' always stop here when debugging Else MsgBox "File of same name already attached", VbMsgBoxStyle.vbCritical, "Cannot attach file" End If Exit SubEnd Sub 'AddAttachment
' -------------------------------------------------------------------------' Sub/Func : SaveAttachments' Purpose : Saves the attachments at the current row of the open Recordset' Arguments: rstCurrent - The recordset open at the current row to save' : strFieldName - The name of the attachment field' : strOutputDir - The folder to put the files in (e.g. "C:\Foo\")' -------------------------------------------------------------------------Sub SaveAttachments(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String, ByVal strOutputDir As String) Const CALLER = "SaveAttachments" On Error GoTo SaveAttachments_ErrorHandler Dim rstChild As DAO.Recordset2 Dim fldAttach As DAO.Field2 Dim strFilePath As String If Right(strOutputDir, 1) <> "\" Then strOutputDir = strOutputDir & "\" Set rstChild = rstCurrent.Fields(strFieldName).Value ' The .Value for a complex field returns the underlying Recordset. While Not rstChild.EOF ' Loop through all of the attached files in the child Recordset. strFilePath = strOutputDir & rstChild.Fields(m_strFieldFileName).Value 'Append the name of the attached file to output directory. If Dir(strFilePath) <> "" Then ' The file already exists--delete it first. VBA.SetAttr strFilePath, vbNormal ' Remove any flags (e.g. read-only) that would block the kill command. VBA.Kill strFilePath ' Delete the file. End If Set fldAttach = rstChild.Fields(m_strFieldFileData) ' The binary data of the file. fldAttach.SaveToFile strFilePath rstChild.MoveNext ' Go to the next row in the child Recordset to get the next attached file. Wend rstChild.Close ' cleanup Exit SubSaveAttachments_ErrorHandler: Debug.Print "Error # " & Err.Number & " in " & CALLER & " : " & Err.Description MsgBox Err.Description, VbMsgBoxStyle.vbCritical, "Error # " & Err.Number & " in " & CALLER Debug.Assert False ' always stop here when debugging Resume NextEnd Sub 'SaveAttachments
' -------------------------------------------------------------------------' Sub/Func : TestAddRemoveAndSave' Purpose : Test AddAttachment(), RemoveAttachment(), and SaveAttachments()' -------------------------------------------------------------------------Sub TestAddRemoveAndSave() Dim dbs As DAO.Database Dim rst As DAO.Recordset Const strTable = "Table1" Const strField = "Files" ' Attachment field in Table1 Set dbs = CurrentDb Set rst = dbs.OpenRecordset(strTable) ' Add a new row and an attachment rst.AddNew AddAttachment rst, strField, "C:\Windows\Media\chimes.wav" rst.Update rst.MoveLast ' Add another attachment to the last row rst.Edit AddAttachment rst, strField, "C:\Windows\Media\chord.wav" rst.Update ' Remove the first attachment from the last row rst.Edit RemoveAttachment rst, strField, "chimes.wav" rst.Update If Dir("C:\Foo\", vbDirectory) = "" Then MkDir "C:\Foo" SaveAttachments rst, strField, "C:\Foo\" rst.CloseEnd Sub 'TestAddRemoveAndSave
Comments: (5) Collapse
Somewhat similar content with a more step by step approach in this tutorial on my site (if anyone is interested): www.access-freak.com/tutorials.html
Just curious, do you save VBA code snippets in a library on-line?
AEAIJ, do you mean other than here? Did you notice the 'code' tag on the blog? If you filter to that you should be able to search all the code snippets the Access team has published here.
I read that the email forms will enable access to extract the information from a returned email and insert it automatically into the access database in the appropriate tables. Will this also include the attachments added to this email form? Will it be possible to automatically insert them in the attachment field of a given table? Thanks
Sorry Gilad, in 2007 our email data collection feature doesn't yet have support for attachments.
Comments: (loading) Collapse