Reputation: 3
I'm trying to move data from a table1 in database 1 to table2 in database 2. Table 1 has the same fields as table 2. Table 1 has data and Table 2 don't have.
rsDenuncia is the recordset of table 1 from database 1 regNuevoDenuncia is the recorset of table 2 from database 2
I instantiate a child recordset of rsDenuncia that is named rsODenuncia and it works because I can retrieve data using msgboxes.
Set rsODenuncia = rsDenuncia.Fields("pdfAdjunto").Value
MsgBox "Nombre el archivo: " & rsODenuncia("FileName").Value
MsgBox "Tipo de archivo: " & rsODenuncia.Fields("FileType").Value
MsgBox "Data del archivo: " & rsODenuncia.Fields("FileData").Value
But when I try to instantiate the recordset of table 2 (that is an empty table but with same fields as table 1) is gives a error. "No current record"
Set regONuevoDenuncia = regNuevoDenuncia.Fields("pdfAdjunto").Value
Is there a way to instantiate regONuevoDenuncia without using the Value method so I can add new data?
Thanks in advance.
Upvotes: 0
Views: 848
Reputation: 123829
You need to create the new parent record with regNuevoDenuncia.AddNew
before you can open the child recordset for its attachments. The following code is a minimal example:
Option Compare Database
Option Explicit
Sub CopyRecordsWithAttachments()
Dim cdb As DAO.Database
Dim rsDenuncia As DAO.Recordset2, rsODenuncia As DAO.Recordset2
Dim regNuevoDenuncia As DAO.Recordset2, regONuevoDenuncia As DAO.Recordset2
Set cdb = CurrentDb
cdb.Execute "DELETE FROM NuevoDenuncia", dbFailOnError ' clear previous test data, if any
Set rsDenuncia = cdb.OpenRecordset("Denuncia", dbOpenSnapshot)
Set regNuevoDenuncia = cdb.OpenRecordset("NuevoDenuncia", dbOpenDynaset)
Do Until rsDenuncia.EOF
regNuevoDenuncia.AddNew
regNuevoDenuncia!ID = rsDenuncia!ID
Set rsODenuncia = rsDenuncia.Fields("pdfAdjunto").Value
Set regONuevoDenuncia = regNuevoDenuncia.Fields("pdfAdjunto").Value
Do Until rsODenuncia.EOF
regONuevoDenuncia.AddNew
regONuevoDenuncia!FileName = rsODenuncia!FileName
regONuevoDenuncia!FileData = rsODenuncia!FileData
regONuevoDenuncia.Update
rsODenuncia.MoveNext
Loop
regONuevoDenuncia.Close
Set regONuevoDenuncia = Nothing
rsODenuncia.Close
Set rsODenuncia = Nothing
regNuevoDenuncia.Update
rsDenuncia.MoveNext
Loop
regNuevoDenuncia.Close
Set regNuevoDenuncia = Nothing
rsDenuncia.Close
Set rsDenuncia = Nothing
Set cdb = Nothing
Debug.Print "Terminado."
End Sub
Upvotes: 0
Reputation: 4312
You can't reference that field, in the manner you are, in the recordset for DB2 until the recordset is populated. Once you add records to DB2, then the error will go away. Without seeing all your code, I suggest you do something like:
If not regNuevoDenuncia.eof then
Set regONuevoDenuncia = regNuevoDenuncia.Fields("pdfAdjunto").Value
Upvotes: 0