Try it as is:
Code:
Private Sub Export_to_excel_Click()
Dim qdf As dao.QueryDef
Dim strPathUser As String
Dim strFilePath As String
Dim strFormName As String
strFormName = "Search_Result.xls" 'The name of the Workbook
strPathUser = Environ$("USERPROFILE") & "\Desktop" 'The path of user desktop
strFilePath = strPathUser & "\" & strFormName 'The absolute file path of the Workbook
On Error Resume Next
'Try to delete temporary querydef if exist
CurrentDb.QueryDefs.Delete "QTemp"
On Error GoTo ExitHere
'Append the temporary querydef in QueryDefs collection
Set qdf = CurrentDb.CreateQueryDef("QTemp", Me.RecordSource)
'Create the Excel file
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, qdf.Name, strFilePath, True
'There is no reason to delete the temporary querydef "QTemp" at this time.
'It will the next time that will run this procedure.
If MsgBox("Your choice has been exported successfully " & vbCrLf _
& "(as Worksheet '" & qdf.Name & "') in Workbook '" & strFormName & "'" _
& vbCrLf & "on: '" & strPathUser & "'" & vbCrLf & vbCrLf _
& "Do you want to open the workbook '" & strFormName & "' ?" _
, vbInformation + vbYesNo, _
"Export Search Results") = vbYes Then
Application.FollowHyperlink strFilePath, , True
End If
ExitHere:
If Err Then MsgBox "Error: " & Err & vbCrLf & Err.Description, vbExclamation
'Release the qdf variable
Set qdf = Nothing
End Sub