Delete Attachment in Record - Run-Time Error 438

TheBigEasy

Registered User.
Local time
Today, 10:42
Joined
Dec 12, 2019
Messages
19
Hello Friends!

I've been investigating a method to in-cooperate images to a database I created for tracking manufactured product defects. Thankfully, I stumbled upon Access Jitsu's lesson for using VBA & DAO to replicate the built-in functionality of Access's attachment dialog.

http://accessjitsu.com/2015/10/03/vba-for-working-with-the-access-attachment-data-type

I enjoy creating a database as 'user friendly' as possible so seeing that he used command buttons to perform the tasks made me interested to give it a go. I've attached a link to my database, @ the bottom of this post, for reference. No matter how many times I zipped it, I couldn't get it small enough to upload to this site. My apologies.

The 'Add Image' button/functionality works fantastic. Unfortunately, the 'Delete Image' button/functionality decided it wanted to make my day a little harder. When executing the command, I'm getting Run-Time Error 438 on this line in the attached code below:
strFileName = Me.subfrmAttachmentData2.txtFileName In addition, when I click on the 'Save Image' button, an error box opens immediately stating that the expression OnClick you entered as the event property setting produced the following error: Method or data member not found. In an effort to keep this thread as short as possible, please reference code in the attached database if you wouldn't mind. :)

Code:
Private Sub cmdDelete_Click()
On Error GoTo SubError
    Dim rsParent As DAO.Recordset
    Dim rsAttachment As DAO.Recordset2
    Dim SQL As String
    Dim strFileName As String
    
    Me!subfrmAttachmentData2.txtFileName = strFileName
    
    If MsgBox("Are you sure you want to delete " & strFileName & "?", vbQuestion + vbYesNo, _
        "Delete file?") = vbNo Then
        Exit Sub
    End If
    
    'find the record in the attachments table we are currently on
    SQL = "SELECT Document FROM Attachments WHERE RecordID = " & Me.txtRecordID
    Set rsParent = CurrentDb.OpenRecordset(SQL, dbOpenDynaset)

    If rsParent.RecordCount = 0 Then
        MsgBox "There was a problem locating the selected record", _
            vbCritical + vbOKOnly, "Error"
        GoTo SubExit
    Else
        Set rsAttachment = rsParent!Document.Value
        If rsAttachment.RecordCount <> 0 Then
            rsAttachment.FindFirst "FileName='" & strFileName & "'"
            If rsAttachment.NoMatch Then
                MsgBox "There was a problem locating the attached document", _
                    vbCritical + vbOKOnly, "Error"
                GoTo SubExit
            Else
                rsAttachment.Delete
                subfrmAttachmentData2.Requery
            End If
        End If
    End If
    
SubExit:
On Error Resume Next
    If Not rsParent Is Nothing Then
        rsParent.Close
        Set rsParent = Nothing
    End If
    Exit Sub
    
NoDocFound:
    MsgBox "No document found in attachment", vbCritical + vbOKOnly, _
        "An error occurred"
    GoTo SubExit

SubError:
    MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical + vbOKOnly, _
        "An error occurred"
    GoTo SubExit
    
End Sub

Database can be accessed via the following link - https://drive.google.com/open?id=1QLp-Q4ryjisWKOzcS8pA5Tm7HbaRDRP0

Thanks in advance for your help!
 
When referring to controls on a sub form, the code construct should be in the form
me.subFormName.Form.ControlName

Try
strFileName = Me.subfrmAttachmentData2.Form.txtFileName
 
Thank you Cronk! Not sure why this code worked for Master Jitsu and not me but, nevertheless, you nailed it.
 

Users who are viewing this thread

Back
Top Bottom