Papashep
New member
- Local time
- Today, 12:23
- Joined
- Oct 13, 2013
- Messages
- 15
Hi
I am trying to check if an attachment exists on a recordset2 record and if it does remove the field contents and then add a new attachment to the same record so I Only have the one attachment entry on the record. My code is giving me two problems:
1. The If statement If .Fields("Logo").Type = dbAttachment And Not IsNull(!Logo.value) This is failing in that currently the Logo field does not contain an attachment and within that code block, I then get my next problem
2. The statement .Fields("Logo").value.Attachments.Delete fails with the error code 438 Object doesn't support this property or method.
Can anyone help please, this is the first time I have tried to manage any attachments.
I am using Access 365 64bit on a WIndows 11 PC
Below is the function that I am calling
I am trying to check if an attachment exists on a recordset2 record and if it does remove the field contents and then add a new attachment to the same record so I Only have the one attachment entry on the record. My code is giving me two problems:
1. The If statement If .Fields("Logo").Type = dbAttachment And Not IsNull(!Logo.value) This is failing in that currently the Logo field does not contain an attachment and within that code block, I then get my next problem
2. The statement .Fields("Logo").value.Attachments.Delete fails with the error code 438 Object doesn't support this property or method.
Can anyone help please, this is the first time I have tried to manage any attachments.
I am using Access 365 64bit on a WIndows 11 PC
Below is the function that I am calling
Code:
Public Function GetLogo(CompID As Integer) As String
On Error GoTo ErrHandler
Dim db As DAO.Database
Dim rst As DAO.Recordset2
Dim fDialog As FileDialog
Dim sFilePath As String
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM Company WHERE Company.CompanyID =" & CompID)
' Set up the File Dialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Title = "Please select a Logo file"
.Filters.Clear
.Filters.Add "Image Files", "*.jpg; *.jpeg; *.png; *.bmp; *.gif"
' Show the dialog and get the file path
If .Show = -1 Then
sFilePath = .SelectedItems(1)
Else
MsgBox "You clicked Cancel in the file dialog box."
Exit Function
End If
End With
With rst
If Not .EOF Then
' Check if the Logo field contains attachments
If .Fields("Logo").Type = dbAttachment And Not IsNull(!Logo.value) Then
' Clear existing attachments
.Edit
.Fields("Logo").value.Attachments.Delete
.Update
End If
' Add the new attachment
.Edit
.Fields("Logo").value.Attachments.Add sFilePath
.Update
Else
MsgBox "No record found for CompanyID " & CompID
End If
End With
Exit Function
ErrHandler:
MsgBox "Error Line: " & Erl & " Error number " & Err.Number & ": " & Err.Description
Resume Next
End Function