Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 03-05-2012, 10:18 AM   #1
TiagoDM
Newly Registered User
 
Join Date: Apr 2011
Posts: 22
Thanks: 3
Thanked 0 Times in 0 Posts
TiagoDM is on a distinguished road
Help with VBA and attachments - recordset2

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.

TiagoDM is offline   Reply With Quote
Reply

Tags
attachment automatically , attachments

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA for Attachments? SweetSolutions Modules & VBA 8 04-01-2013 05:09 AM
Attachments? NewShoes Tables 4 01-19-2012 06:27 AM
Question Attachments austie333 General 1 07-08-2010 07:53 PM
Attachments Henley12 Forms 5 01-29-2010 12:09 PM
Attachments cmatni Modules & VBA 2 01-26-2007 06:09 AM




All times are GMT -8. The time now is 02:56 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World