0

I am trying to create a excel document where I can just write a date in a column in excel, and the date will update automatically in the data to the date in the column cell. Ex: if I change the date in a cell to 22/12/22 the data will change when running the query. I am quite new to VBA, so this might be a stupid question.. I am able extract the given dates when I write it manually in the query, however, I want to make it dynamic. This is the VBA query:

 Option Explicit
    
    Sub project()
    
    Dim cn As ADODB.connection Dim rs As ADODB.Recordset Dim SQL As String
    
    
    Dim server_name As String, database_name As String Let server_name = "xxx" Let database_name = "xxx"
    
        Set cn = New ADODB.connection
        Set rs = New ADODB.Recordset
        
        cn.ConnectionString = "Provider=SQLNCLI11;Server=" & server_name & _
            ";database=" & database_name & ";Integrated Security=SSPI;"
            
            
            SQL = "Select * FROM table_name where value_date = '" & ThisWorkbook.Sheets(1).Range("A1").Value & "' " & ""
    
            cn.Open
            rs.Open SQL, cn
            
            ThisWorkbook.Sheets(2).Range("A2").CopyFromRecordset rs
            
            cn.Close
            
            Set cn = Nothing
            
            
    
    End Sub

I have tried given the cell I am refering to a name, however it does not work.

Ike
  • 9,580
  • 4
  • 13
  • 29
  • [It's not working](http://idownvotedbecau.se/itsnotworking/) is no good starting point to help you. Do you get an error? Is the result not as expected? Reading [ask] and [repro] will help you to improve your question. – Ike Dec 22 '22 at 09:30
  • Do you want the data pulled from the SQL to get refreshed when giving your cell a new date in the sense of `where value_date = " & newGivenDate & ....` ? If so, you could work with the Worksheet_Change event like in: https://stackoverflow.com/a/415159/19353309 and then put the date in a variable to use in your SQL – Notus_Panda Dec 22 '22 at 09:32

0 Answers0