What causes the cell to be refreshed? The connection string will be dependent on what database you are using. The below example uses Access and needs a reference to Microsoft ActiveX Dataobjects 2.8
The data is put in cell(1,1) on sheet1. The where clause is taken from textbox1 on sheet2:
Private Sub CommandButton1_Click()
Dim sqlQuery As String
sqlQuery = "SELECT * FROM myTable WHERE " & Worksheets("sheet2").TextBox1.Text
fetchData "C:\file_databases\myDatabase.accdb", Worksheets("sheet1").Cells(1, 1), sqlQuery
End Sub
Private Function fetchData(databaseName As String, targetRange As Range, sqlQuery As String)
Dim connection As New ADODB.connection
Dim records As New ADODB.Recordset
Dim connectionString As String
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & databaseName & ";"
connection.Open connectionString
records.Open sqlQuery, connection
targetRange.CopyFromRecordset records
records.Close
connection.Close
End Function