Help with VBA and attachments - recordset2 (1 Viewer)

TiagoDM

Registered User.
Local time
Today, 19:38
Joined
Apr 19, 2011
Messages
22
Hi. I have modifyed the code in http://www.access-programmers.co.uk/forums/showthread.php?t=169056 wich was suggested by HiTechCoach.

I have a subform and the goal is to every time a new record is inserted, a set of files (docx, xls, pdf, etc...) are attached to a field 'Docs Avaliacao'. Everything would work fine if the recordset in the subform detected the correct "new record" to attach files within, but everytime I test, it attaches the files to other record, or doesn't detect the record to do it.

If I put the code on a button inside the record line in the subform, works fine, but the goal here was everytime a new record was created, the files would go into that record's attachments.

Here's the code:

Code:
Option Explicit

Private Sub Form_AfterInsert()
On Error GoTo Err_AddImage
    Dim file As String
    Dim i As Integer
    Dim path As String
    Dim db As DAO.Database
    Dim rsParent As DAO.Recordset2
    Dim rsChild As DAO.Recordset2
    
    ' ======== Inicio do programa ==========
    path = "\\servidor\comum\modelos reais\" ' Diretorio de origem dos ficheiros
    ' ======== Ciclo For com o nº de ficheiros a implementar nos anexos do registo que se cria
    For i = 1 To 2
        Select Case i
            Case 1
                file = path & "Estruturação da definição de Objectivo.docx"
            Case 2
                file = path & "FD. 01 - Ficha de Departamento Cozinha.doc"
        End Select
    '=======
        Set db = CurrentDb
        Set rsParent = Me.Recordset
        
        Debug.Print rsParent.Fields("Cod Colaborador").Value 'a field to check on the person's code to see if the attachements are being put in the right place ... (and they are not)
    
        rsParent.Edit
    
        Set rsChild = rsParent.Fields("Docs Avaliacao").Value '"Docs Avaliacao" is the attachment field in the table
    
        rsChild.AddNew
        rsChild.Fields("FileData").LoadFromFile (file)
        rsChild.Update
        rsParent.Update
    
        Set rsChild = Nothing
        Set rsParent = Nothing
    
    Next i
    
Exit_AddImage:
        Set rsChild = Nothing
        Set rsParent = Nothing
        Exit Sub
    
Err_AddImage:
    If Err = 3820 Then
        MsgBox ("O ficheiro já existe") 'existing file
        Resume Next
    Else
        MsgBox "Erro!", Err.Number, Err.Description
        Resume Exit_AddImage
    End If
End Sub
If u need I can post pictures.

Thank you.
 

Users who are viewing this thread

Top Bottom