Try this code as a test, it should connect to the database and put all the results into a new sheet, just scrap your old code for now, put it somewhere else.



Private Sub CommandButton1_Click()
 
Call Module1.QueryData1("SELECT UserName,Password FROM data", "A1:B100")

End Sub

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

Set Sht = thisworkbook.Sheets.Add
Sht.Range(OutputRange).CopyFromRecordset rstNewQuery

Set rstNewQuery = Nothing
cntStudConnection.Close
Set cntStudConnection = Nothing

End Function