This should fix it from happening again.....
Function QueryData1(Src, OutputRange)
Dim Sht as Excel.WorkSheet
Dim cntStudConnection As ADODB.Connection
Set cntStudConnection = New ADODB.Connection
cntStudConnection.Provider = "Microsoft.Ace.OLEDB.12.0"
cntStudConnection.Open "C:\Users\Hala\Documents\BBB\Dummydata1.accdb"
Set rstNewQuery = New ADODB.Recordset
rstNewQuery.Open Source:=Src, ActiveConnection:=cntStudConnection
OnError ResumeNext
set Sht = Thisworkbook.Sheets("TempSheet101")'<<-- THIS CHECKS IF THE SHEET ALREADY EXISTS
on error goto 0
Application.DisplayAlerts = False '<<--THIS STOPS A STUPID MESSAGE POPPING UP WHEN YOU DELETE A SHEET
If Not Sht is nothing then Sht.Delete '<<-- DELETE THE SHEET
Application.DisplayAlerts = True
Set Sht = thisworkbook.Sheets.Add
Sht.Name = "TempSheet101"'<<--LET NAME THE SHEET HERE SO WE CAN DELETE IT EASY LATER
Sht.Range(OutputRange).CopyFromRecordset rstNewQuery
Set rstNewQuery = Nothing
cntStudConnection.Close
Set cntStudConnection = Nothing
End Function
Bookmarks