Results 1 to 8 of 8

Thread: VBA code to pull in data from SQL table

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Sep 2020
    Posts
    62
    Location

    VBA code to pull in data from SQL table

    Hello,

    Using VBA code in excel, I'm looking to extract data from a SQL table. But I just need it to pull current year information from it. The code works fine without the WHERE statement, but that brings is the entire table when I'm just looking for current year stuff. When I add in the WHERE statement, I'm getting the following error message:

    Run-time error '-2147217900 (80040e14)':
    'now' is not a recognized built-in function name.


    Sub UpdateData()
    Dim conn As ADODB.Connection
    Sheets("Input").Visible = True
        Sheets("Input").Select
        Range("A2:AE100000").ClearContents
    Dim stFund, stYear As String
    Dim rs As ADODB.Recordset
        Dim stSQL As String
        Set conn = New ADODB.Connection
    conn.Open xConn
        Set rs = New ADODB.Recordset
    stSQL = "Select * from dData"
        stSQL = stSQL & " WHERE Year([dDate]) = year(now())"
        Set rs = conn.Execute(stSQL)
        Range("A2").CopyFromRecordset rs
    rs.Close
    Set rs = Nothing
        conn.Close
        Set conn = Nothing
    Sheets("Input").Visible = False
    End Sub
    Last edited by Aussiebear; 08-31-2022 at 05:51 AM. Reason: Added code tags to supplied code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •