VBA attachments procedure - hopelessly stuck (1 Viewer)

eddix99

Registered User.
Local time
Today, 19:43
Joined
Jul 7, 2016
Messages
32
I've been trying for days to write a procedure to attach a simple file to an existing table.

The code for creating the temp table is working and the data has been pulled in using a SQL command.

The third subroutine creates two recordset entities and I then want to loop through the parent recordset and attach the same file to each record in the temptable.

Code:
Sub objAtt1() 'Attaches a generic file to the temporary table

Dim Db As DAO.Database
Dim recSet As DAO.Recordset2
Dim recSet2 As DAO.Recordset2
Dim objFld As DAO.Field2



Set Db = CurrentDb
Set recSet = Db.OpenRecordset("tbltemptbl") 'A - Set the parent recordset
Set objFld = recSet.Att1
Set recSet2 = objFld.Value 'B - Set the child recordset

recSet.Edit 'C

Do Until recSet.EOF

    recSet2.AddNew
    recSet2.Fields("FileData").LoadFromFile "C:\Temp\GenericAttachment_1.pdf" 'C
    recSet2.Update
    recSet.Update
    recSet.MoveNext 'Need this to prevent endless loop

Loop


End Sub

when I execute this code I get one attachment loaded into the first record, but then get a Run time error '3420' - invalid object or no longer set.

I reckon my issue lies with how I fit both recordsets together in the loop.

As you can probably tell, my VBA experience is somewhat lacking. I hope someone with much more experience can give me a much needed pointer.
 

JHB

Have been here a while
Local time
Today, 12:13
Joined
Jun 17, 2012
Messages
7,732
You update the first recordset, try by removing from your code.
Code:
recSet.Update
 

eddix99

Registered User.
Local time
Today, 19:43
Joined
Jul 7, 2016
Messages
32
Thanks JHB, but this didn't solve my issue. I require both of these statements (and in the correct order) in order to successfully attach a document to the first record. As you know, my problem starts once I try to move on to the next record.
 

JHB

Have been here a while
Local time
Today, 12:13
Joined
Jun 17, 2012
Messages
7,732
Sorry I've read your post again and discovered that what I thought you were doing was not correct.
Can you explain a little more:
Why are you creating a temp table?
Are you adding data to recordset no.1 (recSet) or no: 2. (recSet2)?
What data are you adding and where does it come from?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:13
Joined
May 7, 2009
Messages
19,230
i think this will do it.

Code:
Sub objAtt1()

Dim db As DAO.Database
Dim recSet As DAO.Recordset2
Dim recSet2 As DAO.Recordset2
Dim objFld As DAO.Field2


Set Db = CurrentDB
Set recSet = Db.OpenRecordSet("tblTemptbl", dbOpenDynaset)

With recSet
	If Not (.BOF And .EOF) Then .MoveFirst
	While Not .EOF
		Set objFld = .Fields("Att1")
		Set recSet2 = objFld.Value

		.Edit
		
		recSet2.AddNew
		recSet2.Fields("FileData").LoadFromFile "C:\Temp\GenericAttachment_1.pdf"
		recSet2.Update
		recSet2.Close

		.Update

		.MoveNext
	Wend
	.Close
End With
Set objFld = Nothing
Set recSet2 = Nothing
Set recSet = Nothing
End Sub
 

eddix99

Registered User.
Local time
Today, 19:43
Joined
Jul 7, 2016
Messages
32
arnelgp,

This solved the issue perfectly. Thank you so much :)
 

Users who are viewing this thread

Top Bottom