How to insert an attachment into a table through form in ms access 2007

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fnemo
    New Member
    • Oct 2011
    • 6

    How to insert an attachment into a table through form in ms access 2007

    hi all,
    I would like to insert an attachment object into a table using a form. I have created a table with a field type attachment. But when i am inserting data into table , error is coming.
    The error is:
    Runtime error: -2147217887
    Cannot perform this operation.


    Code:
    Private Sub cmd_add_save_Click()
    
    Dim cmd As ADODB.Command, rs As ADODB.Recordset
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = CurrentProject.AccessConnection
    cmd.CommandType = adCmdUnknown
    
    Set rs = New ADODB.Recordset
    rs.Open "Purchase", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    With rs
        .AddNew ' create a new record
        .Fields(0) = Me.txt_add_prno
        .Fields(1) = Me.txt_add_prdesc
        .Fields(2) = Me.txt_add_prvalue
        .Fields(3) = Me.txt_pr_date
        .Fields(4) = Me.Purchase_PRNote ' Error occurs at this line. 
      
        .Update
    End With
    Set rs = Nothing
    End Sub
    Purchase_PRnote is the name of the textbox which contains the attachment.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Working with an Attachment Field in VBA is bo no means intuitive. I did not have much time, but I did have some success adding Attachments to an Attachment Field in a Table. It is not exactly what you are looking for, but it may point you in the right direction.
    Code:
    Dim dbs As DAO.Database
    Dim rs As DAO.Recordset
    Dim rs2 As DAO.Recordset
    
    Set dbs = CurrentDb
      
    Set rs = dbs.OpenRecordset("Purchase", dbOpenDynaset)
    
    Set rs2 = rs.Fields("PRNOTE").Value
    
    With rs
      .Edit
        .Fields(0) = Me.txt_add_prno
        .Fields(1) = Me.txt_add_prdesc
        .Fields(2) = Me.txt_add_prvalue
        .Fields(3) = Me.txt_pr_Date
          rs2.AddNew
            rs2.Fields("FileData").LoadFromFile Me!Purchase_PRNOTE
          rs2.Update
      .Update
    End With
    
    rs.Close
    Set rs = Nothing
    Set rs2 = Nothing

    Comment

    Working...