+ Reply to Thread
Results 1 to 3 of 3

Use VBA to return ODBC query to Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    01-31-2014
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    2

    Use VBA to return ODBC query to Excel

    Hello,

    I apologize if this question has been answered in other posts, but I looked through several other similar posts and either do not know how to implement the solutions to my case, or they didn't work for me. I am trying to use VBA code to run an ODBC query from our ERP system and return the results to Excel. I am not confident that I can effectively make the connection to ODBC from Excel either at this point, so I probably need my hand held more than most. Here is exactly what I am trying to automate:

    I open Excel and run a query called "Customer Sales History.dqy". This is an ODBC query that pulls data from a table called "sales_history_report_view". There are 3 user inputs required for the query - "customer_id", and then a beginning and ending "invoice_date". The data source is called P21_US and the server it connects to is called GT-SQL02. GT-SQL02 uses the SQL Server driver (Version 6.01.7601.17514). I just want to return this to Excel within VBA so that I can continue modifying the results within the VBA coding.

    Here's what I have been using to connect to the source. I think it works as it doesn't error out, but I haven't successfully returned any data yet so I can't be sure.

    Sub Connect_DSN()
    Dim adoConn As ADODB.Connection
    Dim sConnect As String

    sConnect = "P21_US"

    Set adoConn = New ADODB.Connection

    With adoConn
    .Open sConnect
    .DefaultDatabase = "P21_US"
    Debug.Print .ConnectionString
    Debug.Print .DefaultDatabase
    ' RetreiveSales <-- Procedure I am trying to run later to return the data
    .Close
    End With

    Set adoConn = Nothing

    End Sub


    Any help on this is greatly appreciated! I am still a noob when it comes to VBA syntax which is why I am so high-maintenance here.

    Thanks,
    Steve

  2. #2
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Use VBA to return ODBC query to Excel

    I presume you have added a reference to a suitable library eg adodb

    The connection string doesn't look to be correct for connection string. For the format: www.connectionstrings.com

    You will then need to execute your sql probably returning a recordset


    You obviously have a choice of what to do with the results but a good option is to use the range.copyfromrecordset

    I find a good way to do this is to create a class which opens the connection on the initialise event and closes it on terminate.

    Set up a global variable with NEW. It then means that open and closing the connection is managed safely otherwise you can (or at least used to) get problems with stuff going out of scope.


    click on the * Add Reputation if this was useful or entertaining.

  3. #3
    Registered User
    Join Date
    01-31-2014
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Use VBA to return ODBC query to Excel

    Thanks Tony. So here's what I came up with:

    Sub Connect_DSN()
    Dim adoConn As ADODB.Connection
    Dim sConnect As String

    sConnect = "Driver={SQL Server};Server=GT-SQL02;Database=P21_US;Trusted_Connection=Yes;"

    Set adoConn = New ADODB.Connection

    With adoConn
    .Open sConnect
    .DefaultDatabase = "P21_US"
    Debug.Print .ConnectionString
    Debug.Print .DefaultDatabase
    With ActiveSheet.QueryTables.Add(Connection:=sConnect, Destination:=Range("A1"))
    .ListObject.DisplayName = "Table_Customer_Sales_History"
    .Refresh BackgroundQuery:=False
    End With
    .Close
    End With

    Set adoConn = Nothing

    End Sub


    I am still getting an application or object-defined runtime error. I did add the Microsoft ActiveX Data Objects 2.8 Library already, so that's why the connection is even being made. How precisely do I execute this to retrieve the query I'm after?

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. msquery odbc --> excel repeats query on return
    By gry@ll.mit.edu in forum Excel General
    Replies: 0
    Last Post: 04-03-2006, 01:30 PM
  2. Excel query via ODBC cuts values
    By escelinen in forum Excel General
    Replies: 1
    Last Post: 01-23-2006, 01:40 AM
  3. [SOLVED] edit dqy file (Excel ODBC Query)
    By RalphIBower in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-09-2005, 06:55 PM
  4. [SOLVED] How do I update a ODBC query in Excel using pivotTables in VBA?
    By FCS in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-10-2005, 12:55 PM
  5. [SOLVED] Query Tables (excel odbc) memory problem
    By Gregory in forum Excel General
    Replies: 0
    Last Post: 09-20-2005, 12:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1