Consulting

Results 1 to 8 of 8

Thread: VBA code to pull in data from SQL table

  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

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    I am guessing that you want current year, so below is what I think you need
    stSQL = stSQL & " WHERE Year([dDate]) =" & year(now())

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Not that great with SQL however from what i have looked at should:
    stSQL = "Select * from dData"
    Be
    stSQL = "Select Date from dData"
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post (you may need to click 'Go Advanced')

    Excel 365, Version 2403, Build 17425.20146

  4. #4
    VBAX Regular
    Joined
    Sep 2020
    Posts
    62
    Location
    Quote Originally Posted by JKwan View Post
    I am guessing that you want current year, so below is what I think you need
    stSQL = stSQL & " WHERE Year([dDate]) =" & year(now())

    Yes, that seemed to do the trick.

    Thanks so much!!

  5. #5
    VBAX Regular
    Joined
    Sep 2020
    Posts
    62
    Location
    Sorry, looks like I need to take this one step further. I'm looking to add an additional WHERE statement. So the data that gets returned should be Current Year AND have the department name = 3D. I know I'm close, but I can't seem to figure out the apostrophe or quote mark alignments. I bolded the line below that's giving me issues.


    Dim conn As ADODB.Connection
    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 [ADJ_Table].[ID]"
        stSQL = stSQL + " , [AdjProcessError].[ProcessErrorID]"
        stSQL = stSQL + " , [AdjustmentDate]"
        stSQL = stSQL + " , [WorkType]"
        stSQL = stSQL + " , [ReasonCode]"
        stSQL = stSQL + " , [FundNo]"
        stSQL = stSQL + " , [ShareholderAccountNumber]"
        stSQL = stSQL + " , [DescriptionofProblem]"
        stSQL = stSQL + " FROM [AdjProcessError] INNER JOIN [ADJ_Table] ON [AdjProcessError].RequestNbr = [ADJ_Table].ID"
        stSQL = stSQL & " WHERE (Year ([AdjustmentDate]) =" & Year(Now()) And ([AdjProcessError].[ProcessErrorDepartment]) =" & "'3D'"
        stSQL = stSQL + " ORDER BY [ADJ_Table].[ID] DESC"
    Set rs = conn.Execute(stSQL)
    Sheets("External Errors Detail").Select
        Range("A2:H700000").ClearContents
        Range("A2").CopyFromRecordset rs
    rs.Close
    Set rs = Nothing
        conn.Close
        Set conn = Nothing
    Last edited by Aussiebear; 08-31-2022 at 11:08 AM. Reason: Added code tags to supplied code

  6. #6
    VBAX Regular
    Joined
    Sep 2020
    Posts
    62
    Location
    Quote Originally Posted by JKwan View Post
    I am guessing that you want current year, so below is what I think you need
    stSQL = stSQL & " WHERE Year([dDate]) =" & year(now())
    Sorry, looks like I need to take this one step further. I'm looking to add an additional WHERE statement. So the data that gets returned should be Current Year AND have the department name = 3D. I know I'm close, but I can't seem to figure out the apostrophe or quote mark alignments. I bolded the line below that's giving me issues.


    Dim conn As ADODB.Connection
    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 [ADJ_Table].[ID]"
    stSQL = stSQL + " , [AdjProcessError].[ProcessErrorID]"
    stSQL = stSQL + " , [AdjustmentDate]"
    stSQL = stSQL + " , [WorkType]"
    stSQL = stSQL + " , [ReasonCode]"
    stSQL = stSQL + " , [FundNo]"
    stSQL = stSQL + " , [ShareholderAccountNumber]"
    stSQL = stSQL + " , [DescriptionofProblem]"
    stSQL = stSQL + " FROM [AdjProcessError] INNER JOIN [ADJ_Table] ON [AdjProcessError].RequestNbr = [ADJ_Table].ID"
    stSQL = stSQL & " WHERE (Year ([AdjustmentDate]) =" & Year(Now()) And ([AdjProcessError].[ProcessErrorDepartment]) =" & "'3D'"
    stSQL = stSQL + " ORDER BY [ADJ_Table].[ID] DESC"
    Set rs = conn.Execute(stSQL)
    Sheets("External Errors Detail").Select
    Range("A2:H700000").ClearContents
    Range("A2").CopyFromRecordset rs
    rs.Close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing
    Last edited by Aussiebear; 08-31-2022 at 11:09 AM. Reason: Added code tags to supplied code

  7. #7
    ..
    ..
    stSQL = stSQL & " WHERE (Year([AdjustmentDate]) =" & Year(Now()) & " And (([AdjProcessError].[ProcessErrorDepartment]) = '3D')"

  8. #8
    VBAX Regular
    Joined
    Sep 2020
    Posts
    62
    Location
    Quote Originally Posted by arnelgp View Post
    ..
    ..
    stSQL = stSQL & " WHERE (Year([AdjustmentDate]) =" & Year(Now()) & " And (([AdjProcessError].[ProcessErrorDepartment]) = '3D')"
    Looks like it needed an extra ) at the end, but I got it to work.

    Thanks!!
    Last edited by twmills; 09-01-2022 at 08:28 AM.

Posting Permissions

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