I have a button on a form with the code below sitting in the form. It used to execute without a problem. Suddenly when clicking the button I now get this run time error:
Run-time error 91: Object Variable or With block variable not set in procedure cmdImportEDD_Click, line 0
I have tried commenting out individual lines in the code to find the problem. I ended up determening that the FileDialog part seems to be a problem somehow. However, after having added the If.. then
part to it last time, the code worked again, but today the error is back.
To be clear, the error appears before the VBA code is executed (hence line 0) and Compile yields no errors either!
What is happening here that I'm not getting?
Private Sub cmdImportEDD_Click()
On Error GoTo cmdImportEDD_Click_Error
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
fDialog.AllowMultiSelect = False
fDialog.InitialFileName = GetDownloadFolder
fDialog.Show
If fDialog.SelectedItems.Count < 1 Then
Exit Sub
End If
Debug.Print fDialog.SelectedItems(1)
'Replace the selected file with the current one
Dim sOldFile As String
sOldFile = strTARGET_EDD_SALESFILE
Dim oFSO As FileSystemObject
Set oFSO = New FileSystemObject
oFSO.DeleteFile sOldFile
oFSO.MoveFile Source:=fDialog.SelectedItems(1), Destination:=sOldFile
'Perform the update
DoCmd.SetWarnings False
DoCmd.OpenQuery "qry_app_EDD", acViewNormal, acAdd
DoCmd.SetWarnings True
MsgBox "The data has been successfully imported!", vbOKOnly Or vbInformation, "Import Data: EDD"
On Error GoTo 0
Exit Sub
cmdImportEDD_Click_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdImportEDD_Click, line " & Erl & "."
End Sub
The code for GetDownloadFolder:
Function GetDownloadFolder() As String
Dim objShell
Dim objFolder
Dim objFolderItem
Dim temp
Const DESKTOP = &H10&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(DESKTOP)
Set objFolderItem = objFolder.Self
temp = objFolderItem.Path
temp = Left(temp, Len(temp) - 7) & "Downloads" '<--- I believe this is the download folder
GetDownloadFolder = temp
End Function
Having canceled out the OnError I now get a debug error on the line Set objFolderItem = objFolder.Self