GEt SQL data from Excel VBA

Posted on 2010-01-05
Last Modified: 2012-11-18
I've done a bit of googling but i can't find anything that seems usable.

We have a SQL Database and i'm trying to get Table Information from it.

Server Name:      QCSVR02
Password ect.:    Windows Authentication
DataBase Name: QCLIVE
Table Name:        Quick Corporate$Salesperson_Purchaser

If its any help we also use MS Dynamics Nav (Navision)
There are several Columns that i would need to filter by, but that would be optional.

Any starting points?

I'm reasonably good with VBA, not so much in SQL
Question by:bromy2004
    LVL 20

    Accepted Solution

    You said youre home at VBA

    So here goes, create a new module, paste my code, and call it in any way you prefere.

    I paste from row 2 to allow for VBA macro to write column names (hard coded).

    Sub DataExtract()
    ' Create a connection object.
    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection
    ' Provide the connection string.
    Dim strConn As String
    'Use the SQL Server OLE DB Provider.
    strConn = "PROVIDER=SQLOLEDB;"
    'Connect to the Pubs database on the local server.
    'Use an integrated login.
    strConn = strConn & " INTEGRATED SECURITY=sspi;"
    'Now open the connection.
    cn.Open strConn
    ' Create a recordset object.
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    With rs
        ' Assign the Connection object.
        .ActiveConnection = cn
        ' Extract the required records.
        .Open "SELECT * FROM [Quick Corporate$Salesperson_Purchaser]"
        ' Copy the records into cell A1 on Sheet1.
        Sheet1.Range("A2").CopyFromRecordset rs
        ' Tidy up
    End With
    Set rs = Nothing
    Set cn = Nothing
    End Sub

    Open in new window

    LVL 10

    Author Comment

    Thanks Martin,

    However it didn't populate any data.
    There is definitely data in the table (28 Rows)
    and the Field Names are populated correctly.

    LVL 10

    Author Comment

    I should clarify.
    the field names in the rs variable are visible and are correct.
    nothing was populated in Excel
    LVL 10

    Author Comment

    Got it.

    RecordCount doesn't populate for some reason.
    i used rs.MoveNext

    For anyone else who wants to know.
    See attached Code.
    Function DataExtract(Optional Location As Location) As SalesRep
    Dim cn        As ADODB.Connection
    Dim i         As Integer
    Dim strConn   As String
    Dim ws        As Worksheet
    Dim rs        As ADODB.Recordset
    Dim MyArray() As SalesRep
    Dim strSQL    As String
    strSQL = "SELECT [CODE],[NAME],[GLOBAL DIMENSION 1 CODE] FROM [Quick Corporate$Salesperson_Purchaser] Where [Global Dimension 2 Code] <>''"
    If Location = Nothing Then
    strSQL = strSQL & " and [GLOBAL DIMENSION 1 CODE] = " & Location
    End If
    'Create Connection
    Set cn = New ADODB.Connection
    'Open Connection
    cn.Open strConn
    'New Worksheet
    Set ws = ActiveWorkbook.Worksheets.Add
    'Load Records
    Set rs = New ADODB.Recordset
    'Populate Array
    With rs
      .ActiveConnection = cn
      .Open "SELECT * FROM [Quick Corporate$Salesperson_Purchaser]"
      While Not rs.EOF
      i = i + 1
      ReDim Preserve MyArray(1 To i)
        MyArray(i).Location = LocationEnum(.Fields("Global Dimension 1 Code").Value)
        MyArray(i).Num = .Fields("Code").Value
        MyArray(i).Name = .Fields("Name").Value
    End With
    Set rs = Nothing
    Set cn = Nothing
    End Function

    Open in new window

    LVL 20

    Expert Comment

    by:Marten Rune
    Glad to help

    Good you figured it out.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now