Invalid reference to the parent property (1 Viewer)

ellenr

Registered User.
Local time
Today, 00:06
Joined
Apr 15, 2011
Messages
397
I am using a bit of code that allows user to browse for an address of a file to attach to an email. It works beautifully unless the user cancels the browse operation, after which an error message pops up: "The expression you entered has an invalid reference to the parent property." How to avoid the runtime error message?

Code:
Private Sub Command40_Click()
On Error GoTo Err_Command40_Click


  Dim Myfile As Office.FileDialog
  Set Myfile = Application.FileDialog(msoFileDialogFilePicker)
  Dim FileAddress As String
  With Myfile
    .Filters.Clear
    .AllowMultiSelect = False
    .Show
    FileAddress = .SelectedItems(1)
  End With
        If Len(AttachmentLocation) <> 0 Then
            AttachmentLocation = AttachmentLocation & "; " & FileAddress
        Else
            AttachmentLocation = FileAddress
        End If
If Me.Dirty Then Me.Dirty = False
Me.Refresh
    
Exit_Command40_Click:
    Exit Sub


Err_Command40_Click:
    MsgBox err.Description
    Resume Exit_Command40_Click
    
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:06
Joined
Oct 29, 2018
Messages
21,455
If you comment out the error handler, which line is causing the error?

Just as a guess, try modifying your code as follows:
Code:
If .Show Then
    FileAddress = .SelectedItems(1)
End If
Hope that helps...
 

ellenr

Registered User.
Local time
Today, 00:06
Joined
Apr 15, 2011
Messages
397
Thanks so much! Working great now. Not sure why previous code that worked for several years suddenly needed your expertise, but glad to have it. BTW, I did have to make one other change in case someone else runs into this. I had to add Microsoft Office 16.0 object library to my references.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:06
Joined
Oct 29, 2018
Messages
21,455
Thanks so much! Working great now. Not sure why previous code that worked for several years suddenly needed your expertise, but glad to have it. BTW, I did have to make one other change in case someone else runs into this. I had to add Microsoft Office 16.0 object library to my references.
Hi. Glad to hear you got it sorted out. Good luck with your project.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:06
Joined
Sep 21, 2011
Messages
14,238
Thanks so much! Working great now. Not sure why previous code that worked for several years suddenly needed your expertise, but glad to have it. BTW, I did have to make one other change in case someone else runs into this. I had to add Microsoft Office 16.0 object library to my references.
So have you recently upgraded Access?
 

Users who are viewing this thread

Top Bottom