I have a form with 4 Attachment
controls. From the first one I select 3 PDF files. Form controls names:
PDF_Main
PDF_1
PDF_2
PDF_3
I want to move those 3 files selected from PDF_Main
to the other controls:
PDF_1
for the first file.PDF_2
for the second file.PDF_3
for the third file.
This my form:
Important: I don’t need to interact with any database table, just move those files files from the first Attachment
to the other Attachment
form fields.
This is the initial ChatGPT suggestion:
Private Sub btnDividirPDF_Click()
Dim ctlOriginal As Attachment
Dim rsOriginal As Recordset2
Dim rsDestino As Recordset2
Dim fieldNames As Variant
Dim i As Integer
fieldNames = Array("PDF_1", "PDF_2", "PDF_3")
' Configura el control de adjuntos original y abre el Recordset
Set ctlOriginal = Me.Controls("PDF")
Set rsOriginal = ctlOriginal.Value
' Asegurarse de que hay exactamente tres archivos
If rsOriginal.RecordCount = 3 Then
rsOriginal.MoveFirst
For i = 0 To 2
' Configurar el Recordset del control destino
Set rsDestino = Me.Controls(fieldNames(i)).Value
' Limpiar el destino antes de añadir nuevos archivos
Do While Not rsDestino.EOF
rsDestino.Delete
rsDestino.MoveNext
Loop
rsDestino.Close
' Añadir el archivo del original al destino
rsDestino.AddNew
rsDestino.Fields("FileData").Value = rsOriginal.Fields("FileData").Value
rsDestino.Fields("FileName").Value = rsOriginal.Fields("FileName").Value
rsDestino.Update
rsOriginal.MoveNext
Next i
Else
MsgBox "Debe haber exactamente tres archivos en el campo PDF.", vbExclamation
End If
' Limpieza
rsOriginal.Close
Set rsOriginal = Nothing
Set ctlOriginal = Nothing
End Sub
But it doesn’t work.