Results 1 to 2 of 2
  1. #1
    kdbailey is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    228

    Add Attachment

    Re: https://www.accessforums.net/program...ton-38798.html

    I am having an issue with this same code, I use Access 2007.

    Here are my 3 functions...

    My command button...

    Code:
    Private Sub cmdattach_Click()Dim claimnum As String, serialno As Double, rs As DAO.Recordset, db As DAO.Database
    
    
    claimnum = xx
    serialno = 1
    filepath = SelectFile()
    
    
    If filepath <> "" Then
        Set db = CurrentDb
        Set rs = db.OpenRecordset("Picture Table")
        With rs
            .AddNew
                AddAttachment rs, "Attach", filepath
                .Fields("Claim Number") = claimnum
                .Fields("Serial Number") = serialno
            .Update
            .Close
        End With
    End If
    
    
    End Sub
    My 2 public functions...



    Code:
    Public Function SelectFile() As String    Dim fd As Office.FileDialog
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .AllowMultiSelect = False
            .Title = "Please select file to attach"
            If .Show = True Then
                SelectFile = .SelectedItems(1)
            Else
                Exit Function
            End If
        End With
        Set fd = Nothing
    End Function
    
    Public Sub AddAttachment(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String, ByVal strFilePath As String)
     
    On Error GoTo AddAttachment_Err
    Dim rstChild As DAO.Recordset2
    Dim fldAttach As DAO.Field2
    Set rstChild = rstCurrent.Fields(strFieldName).Value
    rstChild.AddNew
    Set fldAttach = rstChild.Fields(m_strFieldFileData)
    fldAttach.LoadFromFile strFilePath
     rstChild.Update
    rstChild.Close
     Exit Sub
     
    AddAttachment_Err:
    ' Run-time error '3820': (occurs if the file with the same name is already attached)
    If Err.Number <> 3820 Then
     modUtility.ErrorMessage
    Else
     VBA.Interaction.MsgBox "File already attached!", vbInformation + vbOKOnly, modConstant.AppName
    End If
     Resume AddAttachment_End
     
    AddAttachment_End:
    End Sub
    Every time I run this code, the variable "m_strFieldFileData" is empty... I'm unsure how this variable is supposed to be filled as I got this code off of forums.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Programmatically add files into attachment field is a common enough topic. Search forum. Here is one: https://www.accessforums.net/forms/a...98/index2.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 13
    Last Post: 12-05-2013, 06:04 PM
  2. Replies: 16
    Last Post: 04-30-2012, 07:12 AM
  3. Add an attachment to form
    By j2curtis64 in forum Forms
    Replies: 2
    Last Post: 04-20-2011, 11:56 AM
  4. Add an Attachment
    By mwabbe in forum Forms
    Replies: 7
    Last Post: 10-18-2010, 09:54 AM
  5. attachment help!
    By ninachopper in forum Access
    Replies: 1
    Last Post: 08-01-2010, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums