Hi, Allen.
The basic idea is of a recordset within a recordset, i.e. the multi-value
field is exposed as a recordset within each record of the main recordset.
Most excellent! That's a concept that most of us can easily deal with,
without having to learn a whole new object model. (Not that the Recordset2
class looks hard.)
Now I just have to figure out how to get these things nested further, so
we can have recursion.
That can be pretty sticky. Good luck with that!
To Ofer:
After working on Erik's code when I had more time last night, I realized
that I got the "no current record" error because I was attempting to edit an
empty Recordset. Doh! Erik's code worked for him because he already had
attachments added to that record before running the code. After looking at
it some more, I realized that it would be better to have an "add attachment"
function and an "edit attachment" function, so I created two separate
procedures. One is the cmdAddFile( ) sub for adding new attachments, and
the other is the EditFileAtch( ) sub for editing (or adding) attachments.
Both procedures use the new Recordset2 and Field2 classes.
In the examples I've listed below, the column with the Attachment data type
is named Atch (which I probably should have passed as a parameter to make it
generic, but here they are anyway). For editing existing attachments, an
ordinal number is passed to EditFileAtch( ) to indicate which attachment is
to be replaced with another. The ordinal number can be a number that's
higher than the current number of attachments, in which case, a new
attachment will be added.
A possible stumbling block is that when an attachment is edited and a new
value assigned, the order of the attachments might change as Jet
re-alphabetizes them. So if multiple attachments are being edited in a
loop, keep in mind that the index of a particular item before the loop may
easily change to another index, so be careful not to edit the wrong
attachment.
On other notes, while I was experimenting, I tried the form's RecordsetClone
class. This doesn't work in the Beta 2 version. And, while you now have
working procedures that will add or update Attachments in a record whether
you're using a bound form or not -- thanks for the example, Allen! -- I
wouldn't recommend creating multi-valued fields for a relational database.
However, I realize that Sharepoint lists have this capability and one may
not have any choice but to use these procedures to add to or edit a record
for an existing Sharepoint list.
And if you're one of those people who only uses the ADO library, then you
may use the Stream object to load the file into the field. As for the code
to do so, you're on your own.
As promised, the two procedures follow:
' Requires the Microsoft Office 2007 Access database engine Object Library,
ACEDAO.DLL.
Private Sub cmdAddFile(frm As Form, sFileName As String)
On Error GoTo ErrHandler
Dim rsEmployees As Recordset2
Dim rsPictures As Recordset2
Dim fld As Field2
Dim fOpenedRecSet As Boolean
Set rsEmployees = frm.Recordset ' Set the parent recordset.
' Set the attachment recordset.
Set rsPictures = rsEmployees.Fields("Atch").Value
fOpenedRecSet = True
Set fld = rsPictures.Fields("FileData") ' Field2 type to load file
into.
rsEmployees.Edit ' Put the parent record into edit mode.
rsPictures.AddNew
fld.LoadFromFile sFileName
rsPictures.Update
MsgBox "File added"
rsEmployees.Update
CleanUp:
If (fOpenedRecSet) Then
rsPictures.Close
fOpenedRecSet = False
End If
Set fld = Nothing
Set rsPictures = Nothing
Set rsEmployees = Nothing
Exit Sub
ErrHandler:
If (Err.Number = 3820) Then
MsgBox "Sorry. That's a duplicate of a file name " & _
vbCrLf & "that has already been added to this record.", _
vbInformation + vbOKOnly, "Duplicate!"
Else
MsgBox "Error in cmdAddFile( ) in " & Me.Name & " form." & _
vbCrLf & vbCrLf & "Error #" & _
Err.Number & vbCrLf & Err.Description, vbOKOnly, "Error!"
End If
Err.Clear
GoTo CleanUp
End Sub ' cmdAddFile( )
Private Sub EditFileAtch(frm As Form, num As Long, sFileName As String)
On Error GoTo ErrHandler
Dim recSetMain As Recordset2
Dim recSetAtch As Recordset2
Dim fld As Field2
Dim fOpenedRecSet As Boolean
Set recSetMain = frm.Recordset ' Set the parent recordset.
'----------------------------------------------------------------------
' Set the attachment recordset.
'----------------------------------------------------------------------
Set recSetAtch = recSetMain.Fields("Atch").Value
fOpenedRecSet = True
Set fld = recSetAtch.Fields("FileData") ' Field2 type to load file
into.
recSetMain.Edit ' Put the parent record into edit mode.
'----------------------------------------------------------------------
' Prevent a bad ordinal number from having been passed.
'----------------------------------------------------------------------
If (num < 1) Then
num = 1
End If
'----------------------------------------------------------------------
' Get accurate record count & move to approp. record.
'----------------------------------------------------------------------
If (Not (recSetAtch.BOF) And Not (recSetAtch.EOF)) Then
recSetAtch.MoveLast
recSetAtch.MoveFirst
recSetAtch.Move (num - 1)
End If
'----------------------------------------------------------------------
' Determine whether there are already attachments.
'----------------------------------------------------------------------
If ((recSetAtch.RecordCount > 0) And (num <= recSetAtch.RecordCount))
Then
recSetAtch.Edit ' Replace existing file.
Else
recSetAtch.AddNew ' Add new file.
End If
fld.LoadFromFile sFileName
recSetAtch.Update
MsgBox "File added"
recSetMain.Update
CleanUp:
If (fOpenedRecSet) Then
recSetAtch.Close
fOpenedRecSet = False
End If
Set fld = Nothing
Set recSetAtch = Nothing
Set recSetMain = Nothing
Exit Sub
ErrHandler:
If (Err.Number = 3820) Then
MsgBox "Sorry. That's a duplicate of a file name " & _
vbCrLf & "that has already been added to this record.", _
vbInformation + vbOKOnly, "Duplicate!"
Else
MsgBox "Error in EditFileAtch( ) in " & Me.Name & " form." & _
vbCrLf & vbCrLf & "Error #" & _
Err.Number & vbCrLf & Err.Description, vbOKOnly, "Error!"
End If
Err.Clear
GoTo CleanUp
End Sub ' EditFileAtch( )
HTH.
Gunny
See
http://www.QBuilt.com for all your database needs.
See
http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.